thebrightshadow thebrightshadow - 2 years ago 101
Android Question

Sqlite query in Android for auto increment and default values

I want to be able to add data in sqlite using android.
The column Id should have primary key and should be an auto increment field.
And status should have a default value of 0. Wasn't sure if this is the way to do it for sqlite.

public static final String DATABASE_NAME="tododata";
public static final String TABLE_NAME="todolist";
public static final int KEY_ID= 0;
public static final String KEY_TITLE="Title";
public static final String KEY_DESCRIPTION="Description";
// public static final Date KEY_DATE = ;
public static final int KEY_STATUS = 0;

public static final int DATABASE_VERSON=1;

public DataHandler(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSON);
}

@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("create table "+TABLE_NAME+"(Id integer primary key not null autoincrement 1 , title text not null, description text not null, date date not null, status int default 0);");
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("drop table if exists "+TABLE_NAME);
}

Answer Source

This should be the correct syntax:

CREATE TABLE table_name (Id INTEGER PRIMARY KEY AUTOINCREMENT, title TEXT NOT NULL, description TEXT NOT NULL, date DATE NOT NULL, status INTEGER DEFAULT 0)
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download