lundi 1 février 2016

How to execute SQLite database within android app

I am having a problem using SQLite and creating a database within my android app. I was hoping someone here could point out something that I am doing wrong when simply trying to create the database. Here is the code:

Inside DBContract class:

public final class DBContract {

public DBContract() { }    

public static abstract class DBEntry implements BaseColumns {

    public static final String TABLE_WORKOUTS = "Workouts";
    public static final String ENTRY_ID = "id";
    public static final String ENTRY_NAME = "Exercise";
    public static final String ENTRY_DESCRIPTION = "Description";
    public static final String ENTRY_SETS = "Sets";
    public static final String ENTRY_REPS = "Reps";
    public static final String ENTRY_WEIGHT = "Weight";


}

private static final String TEXT_TYPE = "TEXT";
private static final String COMMA_SEP = ",";
private static final String SQL_CREATE_ENTRIES =
        "CREATE TABLE " + DBEntry.TABLE_WORKOUTS + " (" +
                DBEntry.ENTRY_ID + " INTEGER PRIMARY KEY," +
                DBEntry.ENTRY_NAME + TEXT_TYPE + COMMA_SEP +
                DBEntry.ENTRY_DESCRIPTION + TEXT_TYPE + COMMA_SEP +
                DBEntry.ENTRY_SETS + TEXT_TYPE + COMMA_SEP +
                DBEntry.ENTRY_REPS + TEXT_TYPE + COMMA_SEP +
                DBEntry.ENTRY_WEIGHT + TEXT_TYPE  + " )";

private static final String SQL_DELETE_ENTRIES =
        "DROP TABLE IF EXISTS " + DBEntry.TABLE_WORKOUTS;


public static class DBHelper extends SQLiteOpenHelper {

    public static final int DATABASE_VERSION = 1;
    public static final String DATABASE_NAME = "DigiBottle.db";

    public DBHelper(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }

    public void onCreate(SQLiteDatabase db) {
        db.execSQL(SQL_CREATE_ENTRIES);
    }

    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        // This database is only a cache for online data, so its upgrade policy is
        // to simply to discard the data and start over
        db.execSQL(SQL_DELETE_ENTRIES);
        onCreate(db);
    }

    public void onDowngrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        onUpgrade(db, oldVersion, newVersion);
    }
  }
}

And this is what I do to try to access the database:

public class MainActivity extends AppCompatActivity {

private BluetoothAdapter mBluetoothAdapter;
private BroadcastReceiver mReceiver;
private BluetoothSocket mmSocket;

private static int REQUEST_ENABLE_BT = 1;
private int hc05Index;
private String hc05Address;
private int numOfReadBytes;
private int readBuffer;
private ArrayList<String> deviceNameList = new ArrayList<String>();
private ArrayList<String> deviceAddressList = new ArrayList<String>();

DBContract.DBHelper mDBHelper = new DBContract.DBHelper(getBaseContext());

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
    SQLiteDatabase db = mDBHelper.getWritableDatabase();

NOTE: I cut off the rest of the onCreate() function of the main activity as to not add unnecessary code to have to scroll through.

So inside the Main Activity I create an instance of my DBHelper class and inside the onCreate function of the main activity I call the getWriteableDatabase() function to either get an existing database or create a new one if it hasn't been created. It is my understanding that this function will call the onCreate() function of my DBHelper class which should execute the Create Table SQL Command that is written and create the database. For some reason the getWriteableDatabase() function is forcing the app to close.

Any help would be greatly appreciated!

Thanks in advance!!

Aucun commentaire:

Enregistrer un commentaire