· 8 years ago · Mar 09, 2018, 05:46 AM
1
2import java.io.File;
3
4import android.content.Context;
5import android.database.sqlite.SQLiteDatabase;
6import android.database.sqlite.SQLiteOpenHelper;
7import android.util.Log;
8
9public class DbHelper extends SQLiteOpenHelper {
10
11 Context context;
12
13 private static final String DATABASE_NAME = "names";
14 private static final int DATABASE_VERSION = 1;
15
16 // Database creation sql statement
17 private static final String DATABASE_CREATE = "create table if not exists "+DATABASE_NAME+" (_id integer primary key autoincrement, " +
18 "name text not null);";
19
20 public DbHelper(Context context) {
21 super(context, DATABASE_NAME, null, DATABASE_VERSION);
22
23 }
24
25 // Method is called during creation of the database
26 @Override
27 public void onCreate(SQLiteDatabase database) {
28
29 database.execSQL(DATABASE_CREATE);
30 }
31
32 // Method is called during an upgrade of the database, e.g. if you increase
33 // the database version
34 @Override
35 public void onUpgrade(SQLiteDatabase database, int oldVersion,
36 int newVersion) {
37 Log.w(DbHelper.class.getName(),
38 "Upgrading database from version " + oldVersion + " to "
39 + newVersion + ", which will destroy all old data");
40 database.execSQL("DROP TABLE IF EXISTS names");
41 onCreate(database);
42
43 }
44}