Question

I am making a diary application in android which is supposed to get some data from text fields.

When I run my app on the emulator, it gets successfully installed but as soon as I give input in the fields and I tap save option in the menu, emulator prompts that diary app has stopped working.

I am not able to find database folder in my app in emulator's file explorer which means my database is not making.

Here is my SQLite connection making and insertion in table code that i am writing in save item.

    if(item.getItemId()==R.id.save){

    EditText et=(EditText)findViewById(R.id.mood);
        String mood=et.getText().toString();
        et= (EditText)findViewById(R.id.weather);
        String weather=et.getText().toString();
        et= (EditText)findViewById(R.id.Text);
        String text=et.getText().toString();
        Date date= new Date();
        SimpleDateFormat sdfDate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String strDate = sdfDate.format(date);

        SQLiteDatabase db= openOrCreateDatabase("DiaryDatabase",MODE_PRIVATE,null);
        db.execSQL("CREATE TABLE IF NOT EXIST DIARY ('Mood VARCHAR' , 'Weather VARCHAR' , 'Text VARCHAR' , 'Time VARCHAR' , 'Id INTEGER PRIMARY KEY');");
        db.execSQL("INSERT INTO DIARY VALUES(mood,weather,text,strDate,NULL);");
        db.close();
    }
Was it helpful?

Solution

I think your both e.q. DDL and DML statement is incorrect. Try to replace yours with following:

String createQuery = "CREATE TABLE IF NOT EXIST DIARY ("
+ "id integer primary key, "
+ "mood text, "
+ "weather text, "
+ "content text, "
+ "time text" + ")";

Here:

INSERT INTO DIARY VALUES(mood,weather,text,strDate,NULL);

Your origin create statement create PK column as last column and here in your insert statement you are trying to insert NULL as PK that is not allowed and it doesn't make sence since PK is unique identifier of each row.

OTHER TIPS

You should create a SQLite helper class extending from SQLiteOpenHelper to perform all of the database related items such as creating the database and creating the required tables.

Check out this example and use it as a guide:

http://www.vogella.com/articles/AndroidSQLite/article.html

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top