· 8 years ago · Jul 02, 2018, 10:36 PM
1package utils;
2
3import android.content.Context;
4import android.database.sqlite.SQLiteDatabase;
5import android.database.sqlite.SQLiteOpenHelper;
6import android.util.Log;
7
8public class NoteDatabaseHelper extends SQLiteOpenHelper {
9
10 public static final String DATABASE_NAME = "todotable.db";
11 public static final int DATABASE_VERSION = 1;
12 public static final String TABLE_NOTE = "note";
13 public static final String COLUMN_ID = "_id";
14 public static final String COLUMN_TITLE = "title";
15 public static final String COLUMN_TEXT = "text";
16
17 private static final String DATABASE_CREATE = "create table "
18 + TABLE_NOTE
19 + "("
20 + COLUMN_ID + " integer primary key autoincrement, "
21 + COLUMN_TITLE + " title not null, "
22 + COLUMN_TEXT + " text not null"
23 + ");";
24
25 public NoteDatabaseHelper(Context context) {
26 super(context, DATABASE_NAME, null, DATABASE_VERSION);
27 }
28
29 public String getTableName(){
30 return TABLE_NOTE;
31 }
32
33 public String getColumnId(){
34 return COLUMN_ID;
35 }
36
37
38
39 // Method is called during creation of the database
40 @Override
41 public void onCreate(SQLiteDatabase database) {
42 database.execSQL(DATABASE_CREATE);
43 }
44
45 // Method is called during an upgrade of the database,
46 // e.g. if you increase the database version
47 @Override
48 public void onUpgrade(SQLiteDatabase database, int oldVersion,
49 int newVersion) {
50 Log.w(NoteDatabaseHelper.class.getName(), "Upgrading database from version "
51 + oldVersion + " to " + newVersion
52 + ", which will destroy all old data");
53 database.execSQL("DROP TABLE IF EXISTS " + TABLE_NOTE);
54 onCreate(database);
55
56 }
57}