• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Am I Going Insane?

You would tell me if I was crazy right?
I started a project with an sqlite database, made some really good progress, but when I went to build it out, the database was not created and it crashed after pushing one of the buttons. So I started over and didn't copy anything, but typed it out. The build still didn't work. so I stripped it down to two classes, the database helper and the main(blank) activity. Still the database is not getting created. Please save my sanity and my hair.

Java:
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class DatabaseHelper extends SQLiteOpenHelper
{
    public static final String DATABASE_NAME="Tracker.db";

    public static final String ONE_TABLE="TableOne";
    public static final String COL_ONE="ColOne";
    public static final String COL_TWO="ColTwo";

    public static final String TABLE_TWO="TableTwo";
    public static final String COL_THREE="ColThree";

    public static final String TABLE_THREE="TableThree";
    public static final String COL_FOUR="ColFour";

    public static final String TABLE_FOUR="Four";
    public static final String COL_FOUR_DATE="FourDate";
    public static final String COL_FOUR_LOCATION="FourLocation";
    public static final String COL_FOUR_INTENSITY="FourIntensity";
    public static final String COL_FOUR_KIND="FourKind";
    public static final String COL_FOUR_SPAN="FourSpan";

    public DatabaseHelper(Context context)
    {
        super(context, DATABASE_NAME, null, 1);
        SQLiteDatabase db=this.getWritableDatabase();

    }
    @Override
    public void onCreate(SQLiteDatabase db)
    {
        db.execSQL("CREATE TABLE "+TABLE_ONE +" (ColOne TEXT, ColTwo TEXT)");
        db.execSQL("CREATE TABLE "+TABLE_TWO +" (ColThree TEXT)");
        db.execSQL("CREATE TABLE "+TABLE_THREE +" (ColFour TEXT, ColFive TEXT)");
        db.execSQL("CREATE TABLE "+TABLE_FOUR +" (FourDate TEXT, FourLocation TEXT, FourIntensity TEXT, FourKind TEXT, FourSpan TEXT)");
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
    {
        db.execSQL("DROP TABLE If Exists "+TABLE_ONE);
        db.execSQL("DROP TABLE If Exists "+TABLE_TWO);
        db.execSQL("DROP TABLE if Exists "+TABLE_THREE);
        db.execSQL("DROP TABLE If Exists "+TABLE_FOUR);
        onCreate(db);
    }
}

and

Java:
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

public class MainActivity extends AppCompatActivity
{
    DatabaseHelper DB;
    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        DB=new DatabaseHelper(this);
    }
}

Thanks for the help!
 
You're not going insane, but I reckon your onCreate() isn't being called, and here's why:

"SQLiteOpenHelper onCreate() and onUpgrade() callbacks are invoked when the database is actually opened, for example by a call to getWritableDatabase(). The database is not opened when the database helper object itself is created."

https://stackoverflow.com/questions/21881992/when-is-sqliteopenhelper-oncreate-onupgrade-run
Thanks LV426!
In my case, I had tried to run on my tablet(Voyager); I went through the data file and could not find the project, so that's when I knew the database was not created. Do you think there is a version of it somewhere? I'm running an an AMD platform, so I have no choice but to build and put it on the tablet. I've uninstalled it every time.
Should I use getReadableDatabase instead of writable?
 
No you need to call the getWritableDatabase() method on your DatabaseHelper class instance. That will cause the onCreate() method to be invoked, which will create all your database tables.
First remove this line from your DatabaseHelper class constructor

Code:
SQLiteDatabase db=this.getWritableDatabase();

The correct way of creating and using your DatabaseHelper class is as follows: In your MainActivity class use this code -

Code:
DB=new DatabaseHelper(this);
SQLiteDatabase database = DB.getWritableDatabase();

This should cause your onCreate() method to be invoked. To verify this, put a breakpoint in that method.
 
No you need to call the getWritableDatabase() method on your DatabaseHelper class instance. That will cause the onCreate() method to be invoked, which will create all your database tables.
First remove this line from your DatabaseHelper class constructor

Code:
SQLiteDatabase db=this.getWritableDatabase();

The correct way of creating and using your DatabaseHelper class is as follows: In your MainActivity class use this code -

Code:
DB=new DatabaseHelper(this);
SQLiteDatabase database = DB.getWritableDatabase();

This should cause your onCreate() method to be invoked. To verify this, put a breakpoint in that method.
No you need to call the getWritableDatabase() method on your DatabaseHelper class instance. That will cause the onCreate() method to be invoked, which will create all your database tables.
First remove this line from your DatabaseHelper class constructor

Code:
SQLiteDatabase db=this.getWritableDatabase();

The correct way of creating and using your DatabaseHelper class is as follows: In your MainActivity class use this code -

Code:
DB=new DatabaseHelper(this);
SQLiteDatabase database = DB.getWritableDatabase();

This should cause your onCreate() method to be invoked. To verify this, put a breakpoint in that method.
Thanks! I'll give it a whirl...
 
Back
Top Bottom