android sqlite异常没有找到列名

时间:2014-04-21 11:30:29

标签: android sqlite

我正在尝试将值插入到sqlite数据库android中,以下是ma代码

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)
  {
  }
}

在按钮监听器中,我这样做:

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);

但我得到的例外如下:

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)

我试过没有字段&#34; desc&#34;它没有那个工作。我该怎么办?

3 个答案:

答案 0 :(得分:1)

你不能使用 desc ,因为它是一个保留关键字(它意味着与ORDER BY子句一起使用):

public static final String COL_DESCRIPTION= "desc";

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

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

使用:

public static final String COL_DESCRIPTION= "description";

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

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

答案 1 :(得分:0)

&#39;降序&#39;是一个SQLite&#39;保留字&#39;。最好的做法是将列重命名为其他内容。更多信息:

https://sqlite.org/lang_keywords.html

另一个提示,最佳做法是在与数据库通信时始终使用静态字符串。您已经定义了它们,为什么不重复使用它们。

localContentValues.put(COL_DESCRIPTION, "iliketurtles");

答案 2 :(得分:0)

更改

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

localContentValues.put(COL_DESCRIPTION, "c");