您好我使用此代码创建数据库,创建表和插入值。但它显示The application(process com.data)has stopped unexpectedly
。
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//This Button object get button name
Button button1 = (Button) findViewById(R.id.button1);
button1.setOnClickListener(new OnClickListener()
{
public void onClick(View v)
{
try
{
//This EditText Get Textboxs Values
EditText edittxt1 = (EditText)findViewById(R.id.editText1);
EditText edittxt2 = (EditText)findViewById(R.id.editText2);
EditText edittxt3 = (EditText)findViewById(R.id.editText3);
//This is Create Database in you Data Folder
db = openOrCreateDatabase( "aahussain.db", SQLiteDatabase.CREATE_IF_NECESSARY , null );
// This String Create table in database
String CREATE_CONTACTS_TABLE = "CREATE TABLE if not exists info (name VARCHAR(255),Email VARCHAR(255),phone VARCHAR(255))";
db.execSQL(CREATE_CONTACTS_TABLE);
//This is String Insert Data in your Table
String Query = "insert into info (name,Email,phone) values ('" + edittxt1.getText() + "','" + edittxt2.getText() + "','" + edittxt3.getText() + "')";
db.execSQL(Query);
}
catch(Exception e)
{ // This is Toast Catch your Exceptions
Toast.makeText(MainActivity.this, "ERROR "+e.toString(), Toast.LENGTH_LONG).show();
} // now DB Close
db.close();
}
});
}
答案 0 :(得分:0)
SQL3支持5种类型的数据类型:
NULL。该值为NULL值。
INTEGER。该值是有符号整数,存储为1,2,3,4,6或8个字节,具体取决于值的大小。
REAL。该值是浮点值,存储为8字节IEEE浮点数。
TEXT。该值是一个文本字符串,使用数据库编码(UTF-8,UTF-16BE或UTF-16LE)存储。
BLOB。该值是一个数据块,完全按照输入的方式存储。
请更改您的创建数据库语法,如下所示:
String CREATE_CONTACTS_TABLE = "CREATE TABLE if not exists info (_id integer primary key autoincrement, name text, Email text, phone text)";
您必须以写入模式打开数据库以插入任何值。
关闭onStop()
活动的数据库。
改变这个:
db = openOrCreateDatabase( "aahussain.db", SQLiteDatabase.CREATE_IF_NECESSARY , OPEN_READWRITE );
改变这个:
//This EditText Get Textboxs Values
EditText edittxt1 = (EditText)findViewById(R.id.editText1);
EditText edittxt2 = (EditText)findViewById(R.id.editText2);
EditText edittxt3 = (EditText)findViewById(R.id.editText3);
button1.setOnClickListener(new OnClickListener()
{
public void onClick(View v)
{
try
{
//This is Create Database in you Data Folder
db = openOrCreateDatabase( "aahussain.db", SQLiteDatabase.CREATE_IF_NECESSARY , null );
// This String Create table in database
String CREATE_CONTACTS_TABLE = "CREATE TABLE if not exists info (name VARCHAR(255),Email VARCHAR(255),phone VARCHAR(255))";
db.execSQL(CREATE_CONTACTS_TABLE);
//This is String Insert Data in your Table
String Query = "insert into info (name,Email,phone) values ('"
+ edittxt1.getText().toString() + "','" + edittxt2.getText().toString() + "','" + edittxt3.getText().toString() + "')";
db.execSQL(Query);
}
我建议你阅读android教程。