shlist

share and manage lists between multiple people
Log | Files | Refs

DBHelper.java (2177B)


      1 package drsocto.shlist;
      2 
      3 import android.content.Context;
      4 import android.database.Cursor;
      5 import android.database.sqlite.SQLiteDatabase;
      6 import android.util.Log;
      7 
      8 /**
      9  * Created by David on 7/12/2015.
     10  */
     11 public class DBHelper {
     12     private String dbName;
     13     private SQLiteDatabase theDB;
     14     private Context theContext;
     15 
     16     public DBHelper(String name, Context context) {
     17         dbName = name;
     18         theContext = context;
     19     }
     20 
     21     public void openOrCreateDB() {
     22         theDB = theContext.openOrCreateDatabase(dbName, theContext.MODE_PRIVATE, null);
     23         theDB.execSQL("CREATE TABLE IF NOT EXISTS device(id VARCHAR not null, phone_number int not null)");
     24         theDB.execSQL("CREATE TABLE IF NOT EXISTS my_lists(id VARCHAR not null, name VARCHAR not null, date int)");
     25         // TODO: create the rest of the tables here as well, can we check the return of that command?
     26         // ie if that creates the table then create everything else? Or should we create when the tables are new.
     27     }
     28 
     29     public void deleteDB() { theContext.deleteDatabase(dbName);  }
     30 
     31     public void closeDB() {
     32         theDB.close();
     33     }
     34 
     35     public String getDeviceID() {
     36         Cursor resultSet = theDB.rawQuery("SELECT id FROM device", null);
     37         if(resultSet.moveToFirst()) {
     38             Log.i("DBHelper", "Returning a value from getDeviceID()");
     39             return resultSet.getString(resultSet.getColumnIndex("id"));
     40         } else {
     41             Log.i("DBHelper", "Returning empty string from getDeviceID()");
     42             return null;
     43         }
     44     }
     45 
     46     public void setDeviceID(String deviceID, String phoneNumber) {
     47         Log.d("DBHelper", "Added Entry To device: " + deviceID + " - " + phoneNumber);
     48         String query = "insert into device VALUES(?,?)";
     49         theDB.execSQL(query, new String[] {deviceID, phoneNumber});
     50     }
     51 
     52     public void addList(String listID, String listName) {
     53         Log.d("dbhelper", "Added Entry To My Lists: " + listID + " - " + listName);
     54         String query = "insert into my_lists VALUES(?,?,?)";
     55         theDB.execSQL(query, new String[] {listID, listName, ""});
     56     }
     57 
     58 }