There will be two tables: lists and items :
Usualy, in android, SqlLite database is used. Here is the code i wrote for creating the database :
//create lists table
private static final String LISTS_TABLE_CREATE =
"create table lists ("
+"_id integer primary key autoincrement, "
+" listName text not null"
+" );";
//create items table
private static final String ITEMS_TABLE_CREATE =
"create table list_items ("
+"_id integer primary key autoincrement, "
+" isdone boolean not null DEFAULT false,"
+" title text not null,"
+" list_id integer,"
+" quantity integer not null DEFAULT 1,"
+" FOREIGN KEY(list_id) REFERENCES lists(_id)"
+" );";
private final Context context;
private DatabaseHelper DBHelper;
private SQLiteDatabase db;
public DBAdapter(Context ctx)
{
this.context = ctx;
DBHelper = new DatabaseHelper(context);
}
private static class DatabaseHelper extends SQLiteOpenHelper
{
DatabaseHelper(Context context)
{
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db)
{
try {
db.execSQL(LISTS_TABLE_CREATE );
db.execSQL(ITEMS_TABLE_CREATE );
}
catch (Exception e){
e.printStackTrace();
}
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
{/*
Log.w(TAG, "Upgrading database from version " + oldVersion
+ " to "
+ newVersion + ", which will destroy all old data");*/
db.execSQL("DROP TABLE IF EXISTS " + ITEMS_TABLE_NAME);
db.execSQL("DROP TABLE IF EXISTS " + LISTS_TABLE_NAME);
onCreate(db);
}
}

No comments:
Post a Comment