3

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();
    }
HeatfanJohn
  • 7,143
  • 2
  • 35
  • 41
titan
  • 664
  • 6
  • 18
  • Please post your LOGCAT to see what error is happening. Please checkout the link in my answer, I always use a database helper class to manage the required SQLite database maintenance items (create database, upgrade database, create table(s), etc). – HeatfanJohn May 04 '13 at 20:40

2 Answers2

2

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.

Simon Dorociak
  • 33,374
  • 10
  • 68
  • 106
  • In SQLite when you declare "Id integer Primary Key" , SQLite itself makes it auto incremental, thats why i am sending null value. – titan May 05 '13 at 10:26
  • @ShoaibKhan NO. You are wrong. you cannot pass null as PK!If you want to autoincrement, you need to define autoincrementing. Your actual column doesn't provide autoincrementing. You have to define it like `id integer primary key autoincrement` and then your insert have to be like `INSERT INTO DIARY VALUES(mood,weather,text,strDate);` – Simon Dorociak May 05 '13 at 13:45
  • Thanks, i got it. I was having issues earlier due to this reason. – titan May 05 '13 at 18:33
  • @ShoaibKhan so if it works, can you accept asnwer as working one? thanks – Simon Dorociak May 05 '13 at 21:51
0

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

HeatfanJohn
  • 7,143
  • 2
  • 35
  • 41