Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

android database exception

Tags:

android

sqlite

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);
            }
        });
    }
like image 203
Mohamed Saleh Avatar asked Apr 20 '26 21:04

Mohamed Saleh


1 Answers

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.

like image 110
Barak Avatar answered Apr 23 '26 10:04

Barak



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!