· 10 years ago · Aug 25, 2016, 02:46 PM
1public class DBHelper extends SQLiteOpenHelper {
2
3 public static final int DATABASE_VERSION = 1;
4
5 public static final String DATABASE_NAME = "reminder_database";
6
7 public static final String TASKS_TABLE = "tasks_table";
8
9 public static final String TASK_TITLE_COLUMN = "task_title";
10 public static final String TASK_DATE_COLUMN = "task_date";
11 public static final String TASK_PRIORITY_COLUMN = "task_priority";
12 public static final String TASK_STATUS_COLUMN = "task_status";
13 public static final String TASK_TIME_STAMP_COLUMN = "task_time_stamp";
14
15 private static final String TASKS_TABLE_CREATE_SCRIPT = "CREATE TABLE "
16 + TASKS_TABLE + " (" + BaseColumns._ID
17 + " INTEGER PRIMARY KEY AUTOINCREMENT, " + TASK_TITLE_COLUMN + " TEXT NOT NULL, "
18 + TASK_DATE_COLUMN + " LONG, " + TASK_PRIORITY_COLUMN + " INTEGER, "
19 + TASK_STATUS_COLUMN + " INTEGER, " + TASK_TIME_STAMP_COLUMN + " LONG);";
20
21
22 public static final String SELECTION_STATUS = DBHelper.TASK_STATUS_COLUMN + " = ?";
23
24 private DBQueryManager queryManager;
25 private DBUpdateManager updateManager;
26
27 public DBHelper(Context context) {
28 super(context, DATABASE_NAME, null, DATABASE_VERSION);
29 queryManager = new DBQueryManager(getReadableDatabase());
30 updateManager = new DBUpdateManager(getWritableDatabase());
31 }
32
33 @Override
34 public void onCreate(SQLiteDatabase db) {
35 db.execSQL(TASKS_TABLE_CREATE_SCRIPT);
36 }
37
38<?xml version="1.0" encoding="utf-8"?>
39<batabase xmlns:android="http://schemas.android.com/apk/res/android"
40 version = "1">
41 <table name = "tasks_table">
42 <column name = "_id" type = "INTEGER" property = "PRIMARY KEY AUTOINCREMENT"/>
43 <column name = "task_title" type = "TEXT" property = "NOT NULL"/>
44 <column name = "task_date" type = "LONG" property = ""/>
45 <column name = "task_priority" type = "INTEGER" property = ""/>
46 <column name = "task_status" type = "INTEGER" property = ""/>
47 <column name = "task_time_stamp" type = "LONG" property = ""/>
48 </table>
49</batabase>
50
51public class OptionsTable {
52
53 // Database table
54 public static final String TABLE_OPTIONS = "table_options";
55 public static final String COLUMN_ID = "_id";
56 public static final String COLUMN_DB_VERSION = "db_version";
57
58
59
60 // Database creation SQL statement
61 private static final String DATABASE_CREATE =
62 "create table "
63 + TABLE_OPTIONS
64 + "("
65 + COLUMN_ID + " integer primary key autoincrement, "
66 + COLUMN_DB_VERSION + " text "
67 + ");";
68
69 public static void onCreate(SQLiteDatabase database) {
70 database.execSQL(DATABASE_CREATE);
71 }
72
73 public static void onUpgrade(SQLiteDatabase database, int oldVersion,
74 int newVersion) {
75 Log.w(PacketTable.class.getName(), "Upgrading database from version "
76 + oldVersion + " to " + newVersion
77 + ", which will destroy all old data");
78 database.execSQL("DROP TABLE IF EXISTS " + TABLE_OPTIONS);
79 onCreate(database);
80 }
81
82}
83
84public class DBHelper extends SQLiteOpenHelper {
85
86 private static final String TAG = DBHelper.class.getSimpleName();
87
88 public static final String DATABASE_NAME = "application.db";
89
90 public DBHelper(Context context) {
91 super(context, DATABASE_NAME, null, 1);
92 }
93
94 @Override
95 public void onCreate(SQLiteDatabase db) {
96 OptionsTable.onCreate(db);
97 }
98
99 @Override
100 public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
101 OptionsTable.onUpgrade(db, oldVersion, newVersion);
102 }
103}