0

I am trying insert values into sqlite database android, The following is ma code

public class MySQLiteOpenHelper extends SQLiteOpenHelper
{
  public static final String COL_DATETIME = "date_time";    
  public static final String COL_IMEI = "imei";
  public static final String COL_CATEGORY = "category";
  public static final String COL_DESCRIPTION= "desc";
  public static final String COL_LAT = "locationx";
  public static final String COL_LONG = "locationy";

  public static final String DATABASE_NAME = "cmas";
  public static final String TABLE_NAME = "survey_details";

  public MySQLiteOpenHelper(Context paramContext, String paramString, SQLiteDatabase.CursorFactory paramCursorFactory, int paramInt)
  {
    super(paramContext, paramString, paramCursorFactory, paramInt);
  }

  public void onCreate(SQLiteDatabase paramSQLiteDatabase)
  {
    paramSQLiteDatabase.execSQL("CREATE TABLE survey_details(date_time TEXT,imei TEXT,category TEXT,locationx REAL,locationy REAL,desc TEXT);");
  }

  public void onUpgrade(SQLiteDatabase paramSQLiteDatabase, int paramInt1, int paramInt2)
  {
  }
}

In button listener i am doing like this:

ContentValues localContentValues = new ContentValues();
                  localContentValues.put("date_time","zzz");
                  localContentValues.put("imei","a");
                  localContentValues.put("category","b");
                  localContentValues.put("locationx",99.999);
                  localContentValues.put("locationy", 11.77);
                  localContentValues.put("desc", "c");
                  MainActivity.this.profile.insert("survey_details", "date_time", localContentValues);

But i am getting an exception like the following:

04-21 16:54:51.036: E/SQLiteLog(953): (1) table survey_details has no column named description
04-21 16:54:51.056: E/SQLiteDatabase(953): Error inserting category=b locationx=99.999 locationy=11.77 date_time=zzz description=c imei=a
04-21 16:54:51.056: E/SQLiteDatabase(953): android.database.sqlite.SQLiteException: table survey_details has no column named description (code 1): , while compiling: INSERT INTO survey_details(category,locationx,locationy,date_time,description,imei) VALUES (?,?,?,?,?,?)
04-21 16:54:51.056: E/SQLiteDatabase(953):  at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
04-21 16:54:51.056: E/SQLiteDatabase(953):  at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:882)
04-21 16:54:51.056: E/SQLiteDatabase(953):  at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:493)
04-21 16:54:51.056: E/SQLiteDatabase(953):  at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
04-21 16:54:51.056: E/SQLiteDatabase(953):  at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)
04-21 16:54:51.056: E/SQLiteDatabase(953):  at android.database.sqlite.SQLiteStatement.<init>(SQLiteStatement.java:31)
04-21 16:54:51.056: E/SQLiteDatabase(953):  at android.database.sqlite.SQLiteDatabase.insertWithOnConflict(SQLiteDatabase.java:1467)
04-21 16:54:51.056: E/SQLiteDatabase(953):  at android.database.sqlite.SQLiteDatabase.insert(SQLiteDatabase.java:1339)
04-21 16:54:51.056: E/SQLiteDatabase(953):  at com.example.cmas.MainActivity$4.onClick(MainActivity.java:251)
04-21 16:54:51.056: E/SQLiteDatabase(953):  at android.view.View.performClick(View.java:4084)
04-21 16:54:51.056: E/SQLiteDatabase(953):  at android.view.View$PerformClick.run(View.java:16966)
04-21 16:54:51.056: E/SQLiteDatabase(953):  at android.os.Handler.handleCallback(Handler.java:615)
04-21 16:54:51.056: E/SQLiteDatabase(953):  at android.os.Handler.dispatchMessage(Handler.java:92)
04-21 16:54:51.056: E/SQLiteDatabase(953):  at android.os.Looper.loop(Looper.java:137)
04-21 16:54:51.056: E/SQLiteDatabase(953):  at android.app.ActivityThread.main(ActivityThread.java:4745)
04-21 16:54:51.056: E/SQLiteDatabase(953):  at java.lang.reflect.Method.invokeNative(Native Method)
04-21 16:54:51.056: E/SQLiteDatabase(953):  at java.lang.reflect.Method.invoke(Method.java:511)
04-21 16:54:51.056: E/SQLiteDatabase(953):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
04-21 16:54:51.056: E/SQLiteDatabase(953):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
04-21 16:54:51.056: E/SQLiteDatabase(953):  at dalvik.system.NativeStart.main(Native Method)

I tried without the field "desc", it is working without that.What should I do?

2
  • try to increase the version of your database Commented Apr 21, 2014 at 11:32
  • try to uninstall and install app again. Commented Apr 21, 2014 at 11:33

3 Answers 3

1

You can't use desc, because it's a reserved keyword (it's meant to be used with the ORDER BY clause):

public static final String COL_DESCRIPTION= "desc";

and

paramSQLiteDatabase.execSQL("CREATE TABLE survey_details(date_time TEXT,imei TEXT,category TEXT,locationx REAL,locationy REAL,desc TEXT);");

and

localContentValues.put("desc", "c");

use:

public static final String COL_DESCRIPTION= "description";

and

paramSQLiteDatabase.execSQL("CREATE TABLE survey_details(date_time TEXT,imei TEXT,category TEXT,locationx REAL,locationy REAL,description TEXT);");

and

localContentValues.put("description", "c");
Sign up to request clarification or add additional context in comments.

4 Comments

Tried, Same error still there.04-21 17:08:36.376: E/SQLiteLog(1096): (1) table survey_details has no column named description 04-21 17:08:36.406: E/SQLiteDatabase(1096): Error inserting category=b locationx=99.999 locationy=11.77 date_time=pqr description=c imei=a 04-21 17:08:36.406: E/SQLiteDatabase(1096): android.database.sqlite.SQLiteException: table survey_details has no column named description (code 1): , while compiling: INSERT INTO survey_details(category,locationx,locationy,date_time,description,imei) VALUES (?,?,?,?,?,?)
see my edited answer... you have to replace "desc" EVERYWHERE it appears. (I thought you were using the COL_DESCRIPTION variable, instead of hardcoding the filed name)
BOb same problem still there dude
You have to change all three lines.
0

'desc' is a SQLite 'reserved word'. Best course of action is to rename your column to something else. More here:

https://sqlite.org/lang_keywords.html

Another tip, best practice is to always use your static strings when talking to your database. You've already defined them so why not reuse them.

localContentValues.put(COL_DESCRIPTION, "iliketurtles");

Comments

0

Change

`localContentValues.put("desc", "c");`

to

localContentValues.put(COL_DESCRIPTION, "c");

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.