android数据库异常

时间:2012-11-21 17:09:39

标签: android sqlite

我正在尝试在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);
            }
        });
    }

3 个答案:

答案 0 :(得分:3)

check是一个SQLite关键字。您不能将其用作表或列名称。

将您的列名更改为其他名称,您的错误就会消失。

String TableStructure="create table " + tableName +" (id integer Primary key autoincrement, task text not null, checknum integer not null)";

要获得完整的SQLite关键字列表,请查看here

答案 1 :(得分:1)

要直接使用关键字作为列名,请引用它:

..., "check" integer not null)

答案 2 :(得分:-1)

下午好,请输入您的代码_id integer primary key autoincrement, ...,例如:

CREATE TABLE tabela(_id integer primary key autoincrement, task text not null, check integer not null);