Sam M.
Sam M.

Reputation: 3

SQLLITEDATABASE crash-oncreate not called ? db.getreadabledatabase/getwritabledatabase crash

I'm a beginner at Android and SQLite as well. I have been trying to build a database which retrieves and shows entries like items, price and time. I have looked through the all the other topics but I just can't seem to find why it crashes. I have tried a LOT of ways to make it work, but this is the code I am trying to use:

public class data extends SQLiteOpenHelper {
    private static final String DATABASE_NAME ="items.db";
    private static final String t="database created";
    private static final int DATABASE_VERSION=1;
    Context ct;
    SQLiteDatabase db;
    public data (Context ctx) {
        super(ctx, DATABASE_NAME, null, DATABASE_VERSION);
        ct=ctx;

    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL("CREATE TABLE  record (_id primary key autoincrement,TIME text,ITEM text,TYPE text,AMT text)");
        int duration = Toast.LENGTH_LONG;
        Toast toast = Toast.makeText(ct, t, duration);
        toast.show();
    }

    @Override
    public void onUpgrade(SQLiteDatabase db,int oldVersion,int newVersion) {
        db.execSQL("DROP TABLE IF EXISTS"+TABLE_NAME);
        onCreate(db);
    }

}

here is the class which I use to run the data class object

public class mainscreen extends Activity {
    private data Data;
    private static String[] cols={"_id","TIME","ITEM","TYPE","AMT"};
    private static String ORDER_BY = TIME + "DESC";
    private static int[] TO={R.id.textView5,R.id.textView3,R.id.textView,R.id.textView4,R.id.textView2};

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.mainscreen);
        Data = new data(this);

    }

    public void tryit(View view)
    {
        time t=new time();
        data test =new data(this);
        add(t.tostring(),"tomato", "200", "Food");

        // Cursor cursor = getd();
        // show(cursor);
    }


    private void add(String t,String item, String price,String type) {
        SQLiteDatabase db = Data.getWritableDatabase();
        db.execSQL("insert into "+TABLE_NAME+" values(null,'" + t + "','" + item + "','" + type + "','" + price + "')");
    }
}

As of now I'm simply trying to get the database to show some entries I add using add();

  1. It does not seem to run OnCreate at all with the Data class (I used toast to try and see if the onCreate runs at all)
  2. Trying to use a SQLLITEDatabase object for getreadabledatabase/getwritabledatabase causes a crash
  3. Even the simple execSQL("create******") method above is causing a crash.
  4. The worst part is it ACTUALLY worked 2-3 times yesterday. It showed me 5-8 entries using the listview just as I wanted, and is crashing now sob.

I just cant seem to find the problem!

I'm not too sure how to get a stackdump/trace so if needed please tell me how to get it. I have marked out the cursor/adapters as there is no point getting that to work if the database doesn't work.

I use tryit() by a button to start the insert...and it crashes at

SQLiteDatabase db = Data.getWritableDatabase();

I think the whole problem is because for some reason the database is not getting created, but even using the plain db.execSQL statement whether in onCreate or by using a db object in data class and execSQL at constructor crashes the app.

Upvotes: 0

Views: 323

Answers (1)

isma3l
isma3l

Reputation: 3853

Your create statement is wrong, you need to add the INTEGER type to the _id, try with this:

CREATE TABLE  record (_id integer primary key autoincrement,TIME text,ITEM text,TYPE text,AMT text)

More: https://www.sqlite.org/autoinc.html

Upvotes: 2

Related Questions