Setting a default value for a column in SQLite

I have a SQLite table and want to set it with the default value for a while. How can i do this?

The following is the code for my DatabaseHelper code:

import android.content.Context; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteOpenHelper; public class DatabaseHelper extends SQLiteOpenHelper { private static final String DATABASE_NAME="iFall.db"; public static final String TIME="time"; //public static final String STATUS="status"; //public static final String MINES="loadmines"; //public static final String OPENTILE="openTile"; public DatabaseHelper(Context context) { super(context,DATABASE_NAME,null,1); } @Override public void onCreate(SQLiteDatabase arg0) { // TODO Auto-generated method stub //CREATE TABLE minesweeper(_id INTEGER PRIMARY KEY AUTOINCREMENT,userId TEXT arg0.execSQL("CREATE TABLE finalP(_id INTEGER PRIMARY KEY AUTOINCREMENT,time TEXT);"); } @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { // TODO Auto-generated method stub db.execSQL("DROP TABLE IF EXISTS finalP"); onCreate(db); } } 

I want to set the default value to 50 for the column time.

+7
source share
1 answer

I believe you can do this:

 arg0.execSQL("CREATE TABLE finalP(_id INTEGER PRIMARY KEY AUTOINCREMENT,time TEXT DEFAULT \'50\');"); 

and it should work fine.

See sqlite docs for column restrictions. http://www.sqlite.org/syntaxdiagrams.html#column-constraint

+16
source

All Articles