我正在尝试在 android 上创建一个数据库,当我运行时出现异常:
Database - Failure 1 (near "integer" : syntax error) on 0x1ac028 when preparing 'create table list1 (id integer Primary key autoincrement, task text not null, check integer not null)'
我不明白为什么会这样。这是我的代码:
String dbName="projectDatabase";
String tableName="List1";
String TableStructure="create table " + tableName +" (id integer Primary key autoincrement, task text not null, check integer not null)";
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button cb = (Button) findViewById(R.id.creatbutton);
cb.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
try{
SQLiteDatabase ldb = openOrCreateDatabase(dbName, Context.MODE_PRIVATE, null);
ldb.execSQL(TableStructure);}catch(Exception e){
Toast.makeText(DsTest2Activity.this, "Failed", Toast.LENGTH_LONG).show();
}
Intent list = new Intent(DsTest2Activity.this,List.class);
startActivity(list);
}
});
}
最佳答案
check
是一个 SQLite 关键字。您不能将其用作表名或列名。
将您的列名称更改为其他名称,您的错误就会消失。
String TableStructure="create table " + tableName +" (id integer Primary key autoincrement, task text not null, checknum integer not null)";
有关当前 SQLite 关键字的完整列表,请查看 here .
关于安卓数据库异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13498454/