Mohamed Saleh
Mohamed Saleh

Reputation: 37

android database exception

I am trying to create a database on android and when I run an exception is raised:

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)'

I don't understand why this is happening. This is my code:

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);
            }
        });
    }

Upvotes: 3

Views: 110

Answers (3)

CL.
CL.

Reputation: 180270

To use a keyword directly as a column name, quote it:

..., "check" integer not null)

Upvotes: 1

Rodolfo Faquin
Rodolfo Faquin

Reputation: 830

Good afternoon, put into your code _id integer primary key autoincrement, ..., for exemple:

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

Upvotes: -1

Barak
Barak

Reputation: 16393

check is an SQLite keyword. You cannot use it as a table or column name.

Change your column name to something else and your error will go away.

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

For a complete list of curent SQLite keywords, look here.

Upvotes: 3

Related Questions