0

I'm try to create a new database and table in my constructor class, but I find in this line error:

db.execSQL("CREATE TABLE IF NOT EXIST tblApp ( _ID INTEGER PRIMARY kEY AUTOINCREMENT UNION, Title TEXT )",null);

and crashed my app.

This is my class:

package ir.rezvania.modirbash;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
import android.widget.Toast;

public class db {
    Context ctx;
    Cursor crs;
    private SQLiteDatabase db;
    public db(Context ctx){
       this.ctx=ctx;
        db = ctx.openOrCreateDatabase("`dbApp`", Context.MODE_PRIVATE, null);
        db.execSQL("CREATE TABLE IF NOT EXIST `tblApp` ( _ID Integer Primary key autoincrement union, Title Text )",null);
    }
    public void Insert(String FILDS,String VALUES){
        db.execSQL("INSERT INTO `tblApp` ("+FILDS+")VALUES("+VALUES+");");
    }
    public void Update(String ID,String COLUMN,String VALUE){
        db.execSQL("UPDATE `tblAPP` SET"+COLUMN+"="+VALUE+"WHERE _ID="+ID+";");
    }
    public void Delete(String ID){
        db.execSQL("DELETE FROM `tblApp` WHERE _ID="+ID+";");
    }
    public Cursor Show(){
        crs=db.rawQuery("SELECT * FROM `tblApp`",null);
        return crs;
    }

    public void finalize(){
        db.close();
    }
}
Shivam Kumar
  • 1,892
  • 2
  • 21
  • 33
Rezvania
  • 59
  • 6

2 Answers2

0

Your table creation fails, because you're using the wrong wording. It's IF NOT EXISTS you need to use. For simplicity you should use caps for all keys. Also you don't need to use ` since tblApp is not a keyword, union doesn't make any sense where you put it and AUTOINCREMENT is not needed.

db.execSQL("CREATE TABLE IF NOT EXISTS tblApp (_ID INTEGER PRIMARY KEY, Title TEXT)", null);
tynn
  • 38,113
  • 8
  • 108
  • 143
-1

In SQLite a column declared INTEGER PRIMARY KEY will autoincrement. There is no autoincrement keyword in SQLite, that is why you are getting an error. You can find out more on SQLite FAQ.

Zeeshan Khan
  • 553
  • 7
  • 11