Kevik
Kevik

Reputation: 9351

sqLiteDatabase.insert() and SQLite error code 1, Android database

I am getting the following error stacktrace from Logcat:

 SqliteDatabaseCpp(1355): sqlite returned: error code = 1

 msg = table my_table has no column named check_box_status,
 db=/data/data/com.example.dbtester/databases/my_database

 Error inserting check_box_status=1 price=210 check_box_label=testlabel

 table my_table has no column named check_box_status: , while compiling:
 INSERT INTO my_table(check_box_status,price,check_box_label) VALUES (?,?,?)

I am guessing that it has to do with the fact that the middle parameter of the insert() method shown below is null,

  public void insertNewRow(int checkBoxStatus, String checkBoxLabel, int price){
          ContentValues contentValues = new ContentValues();
          contentValues.put(KEY_CHECKBOX_STATUS, checkBoxStatus);
          contentValues.put(KEY_CHECKBOX_LABEL, checkBoxLabel);
          contentValues.put(KEY_PRICE, price);
          sqLiteDatabase.insert(MYDATABASE_TABLE, null, contentValues); // <---
          }

I put null for the middle parameter value of sqLiteDatabase.insert(), however it is possible that the reason I am getting the errors and the stacktrace shown above is that this variable should not be null.

from the information shown below, what should I put in that middle parameter variable that is now null? sqLiteDatabase.insert(MYDATABASE_TABLE, ?, contentValues);

notice that I explicitly declared a primary key in the string used to create the table, "ID INTEGER PRIMARY KEY AUTOINCREMENT" i think this has something to do with the error.

database initialization code:

    public static final String MYDATABASE_NAME = "my_database";
    public static final String MYDATABASE_TABLE = "my_table";
    public static final int MYDATABASE_VERSION = 1;
    public static final String KEY_CHECKBOX_STATUS = "check_box_status";
    public static final String KEY_CHECKBOX_LABEL = "check_box_label";
    public static final String KEY_PRICE = "price"; 

  //create table MY_DATABASE (ID integer primary key, Content text not null);
    private static final String SCRIPT_CREATE_DATABASE =
     "CREATE TABLE IF NOT EXISTS " + MYDATABASE_TABLE + " (" + "ID INTEGER PRIMARY KEY AUTOINCREMENT, " +  
    "KEY_CHECKBOX_STATUS INTEGER, " + "KEY_CHECKBOX_LABEL TEXT, " + " KEY_PRICE INTEGER" + ");";

    SQLiteDatabase sqLiteDatabase;
    SQLiteHelper sqLiteHelper;
    Context context;

    public Database(Context c){
          context = c;
          sqLiteHelper = new SQLiteHelper(context, MYDATABASE_NAME, null, MYDATABASE_VERSION);
         }

Upvotes: 0

Views: 727

Answers (3)

Hoan Nguyen
Hoan Nguyen

Reputation: 18151

Change

private static final String SCRIPT_CREATE_DATABASE =
 "CREATE TABLE IF NOT EXISTS " + MYDATABASE_TABLE + " (" + "ID INTEGER PRIMARY KEY AUTOINCREMENT, " +  
KEY_CHECKBOX_STATUS + " INTEGER, " + KEY_CHECKBOX_LABEL + " TEXT, " +  KEY_PRICE + " INTEGER);";

Upvotes: 1

Pragnani
Pragnani

Reputation: 20155

Your Create Table syntax is wrong, as you are passing Variable name in quotes. So change your create table like this

  "CREATE TABLE IF NOT EXISTS " + MYDATABASE_TABLE + " (" + "ID INTEGER PRIMARY KEY AUTOINCREMENT, " +  
            KEY_CHECKBOX_STATUS+" INTEGER, " + KEY_CHECKBOX_LABEL+" TEXT, " +  KEY_PRICE +" INTEGER" + ");";

Upvotes: 2

Nermeen
Nermeen

Reputation: 15973

In your SQL of create you are putting KEY_CHECKBOX_STATUS as the column name and not the value (check_box_status)..that's why is telling table my_table has no column named check_box_status

so the create should be:

private static final String SCRIPT_CREATE_DATABASE =
 "CREATE TABLE IF NOT EXISTS " + MYDATABASE_TABLE + " (" + "ID INTEGER PRIMARY KEY AUTOINCREMENT, " +  
KEY_CHECKBOX_STATUS + " INTEGER, " + KEY_CHECKBOX_LABEL +" TEXT, " +  KEY_PRICE +" INTEGER" + ");";

Upvotes: 2

Related Questions