· 6 years ago · Dec 12, 2019, 11:52 PM
1public class MyDBAdapter {
2 public static final String DB_FILEPATH = "/data/data/com.sharingexpensesapp/databases/SharingExpensesAppSQLite.db";
3 public static final int DATABASE_VERSION = 1;
4 public static final String DATABASE_NAME = "SharingExpensesAppSQLite";
5
6 public static final String TABLE_NAME = "currency";
7 private static final String TAG_CURRENCYID = "currencyID";
8 private static final String TAG_CURRENCYNAME = "currencyName";
9 private static final String TAG_CODE = "code";
10 private static final String TAG_SYMBOL = "symbol";
11
12 private static final String SQL_CREATE =
13 "CREATE TABLE IF NOT EXISTS `currency` (\n" +
14 " `currencyID` INTEGER PRIMARY KEY AUTOINCREMENT,\n" +
15 " `currencyName` varchar(20) NOT NULL,\n" +
16 " `code` varchar(4) NOT NULL,\n" +
17 " `symbol` varchar(4) NOT NULL) ";
18
19 private static final String SQL_DELETE_ENTRIES =
20 "DROP TABLE IF EXISTS " + TABLE_NAME;
21
22 private Context mContext;
23 private MyDBHelper mDbHelper;
24 private SQLiteDatabase mSqliteDatabase;
25
26 public MyDBAdapter(Context context) {
27 this.mContext = context;
28 mDbHelper = new MyDBHelper(context, DATABASE_NAME, null, DATABASE_VERSION);
29
30 }
31 public void open(){
32 mSqliteDatabase = mDbHelper.getWritableDatabase();
33 }
34
35 public class MyDBHelper extends SQLiteOpenHelper {
36
37 public MyDBHelper(Context context, String name, SQLiteDatabase.CursorFactory factory, int version){
38 super(context, name, factory, version);
39 }
40
41 @Override
42 public void onCreate(SQLiteDatabase db){
43 String query = SQL_CREATE;
44 db.execSQL(query);
45 try {
46 db.execSQL(new String(data.getBytes(), "UTF-8"));
47 } catch (UnsupportedEncodingException e) {
48 e.printStackTrace();
49 }
50 }
51
52 @Override
53 public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion){
54 String query = "DROP TABLE IF EXISTS students;";
55 db.execSQL(query);
56 onCreate(db);
57 }
58 }
59
60 public void onCreate(SQLiteDatabase db) {
61 db.execSQL(SQL_CREATE);
62 //adding data ...
63 db.execSQL(data);
64
65 }
66
67 public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
68 db.execSQL(SQL_DELETE_ENTRIES);
69 //db.execSQL(data);
70 onCreate(db);
71 }
72
73 public void onDowngrade(SQLiteDatabase db, int oldVersion, int newVersion) {
74 onUpgrade(db, oldVersion, newVersion);
75 }