thebrightshadow
thebrightshadow

Reputation: 31

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

Upvotes: 1

Views: 1622

Answers (2)

Alok Mishra
Alok Mishra

Reputation: 2034

Your Query should be like in this format :-

sqliteDatabase.execSQL("create table tableName ("+COL1_NAME+" integer primary key  autoincrement, "+COL2_NAME+" text , "+COL3_NAME+" text , "+COL4_NAME+" text)");

Upvotes: 0

MatPag
MatPag

Reputation: 44813

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)

Upvotes: 1

Related Questions