-1

I created a SQLite database and I want to insert some initial data into it. so I put the insert "into query" in the "onCreate" method. but it does not insert any values into database and it is empty after create!

here is my code:

package com.example.db;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

public class MySQLiteHelper extends SQLiteOpenHelper {

  public static final String TABLE_GAME_LIST = "steps";
  public static final String COLUMN_ID = "_id";
  public static final String COLUMN_STATUS = "status";
  public static final String COLUMN_HELP_COUNT = "helpCount";
  public static final String COLUMN_EAREASED = "earased";
  public static final String COLUMN_SPEED = "speed";

  private static final String DATABASE_NAME = "earaser.db";
  private static final int DATABASE_VERSION = 1;

  // Database creation sql statement
  private static final String DATABASE_CREATE = "create table "
      + TABLE_GAME_LIST + "(" + COLUMN_ID
      + " integer, " 
      + COLUMN_STATUS
      +" integer, "
      + COLUMN_HELP_COUNT
      +" integer, "
      + COLUMN_EAREASED
      +" string, "
      + COLUMN_SPEED
      +" integer"
      + " );";


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

  @Override
  public void onCreate(SQLiteDatabase database) {
      database.execSQL(DATABASE_CREATE);


      database.execSQL("INSERT INTO "+TABLE_GAME_LIST+"("+COLUMN_ID + ","
                + COLUMN_STATUS + ","
                        + COLUMN_HELP_COUNT + ","
                                + COLUMN_EAREASED + ","
                                        + COLUMN_SPEED + ") VALUES(14, 1, 2, "+"third"+", 5)");
  }

  @Override
  public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
      Log.w(MySQLiteHelper.class.getName(),
        "Upgrading database from version " + oldVersion + " to "
            + newVersion + ", which will destroy all old data");
        db.execSQL("DROP TABLE IF EXISTS " + TABLE_GAME_LIST);
        onCreate(db);
  }

} 
i'm Ahmad
  • 67
  • 13
  • try this `database.execSQL("INSERT INTO steps(_id,status,helpCount,earased,speed) VALUES (14, 1, 2, 'third', 5));"` – M D Mar 24 '15 at 11:21
  • you mean : `database.execSQL("INSERT INTO steps(_id,status,helpCount,earased,speed) VALUES (14, 1, 2, +"+"third"+", 5)");` I tested it but it doesn't work! – i'm Ahmad Mar 24 '15 at 11:26

1 Answers1

0

There are syntax errors in the SQL, such as string values that need to be 'quoted', such as third.

Since you didn't get an exception, the SQL wasn't run. Uninstall your app so that the old version of the database gets removed and onCreate() is run again. When is SQLiteOpenHelper onCreate() / onUpgrade() run?

Community
  • 1
  • 1
laalto
  • 150,114
  • 66
  • 286
  • 303
  • YEEEEEEEES! such a hidden problem! queries in onCreate method run just at install time. so after uninstalling the app and install again, it works. thanks dear laalto ;) – i'm Ahmad Mar 24 '15 at 11:39