SQLiteException:near“null”:在数据库中插入时出现语法错误

时间:2012-05-19 02:36:36

标签: android sqlite

美好的一天,只是为了简化相关代码,

如果我在我的活动中这样做:

     Intent i = getIntent();
          Bundle extras = i.getExtras();

          BitmapFactory.Options bf = new BitmapFactory.Options();
          bf.inSampleSize = 2;


          filename = extras.getString("pic_name");
          ImageView  iv = new ImageView(getApplicationContext());
          Bitmap bm = BitmapFactory.decodeFile(filename, bf);
          iv.setImageBitmap(bm);
          setContentView(iv);

  registerForContextMenu(iv);



public boolean onContextItemSelected(MenuItem item){
       // weatherimagedb = new WeatherImageDB(this);
        //weatherimagedb.open();

        ContentValues vals = new ContentValues();
        int selection = item.getItemId();
        switch(selection){
        case Holidays:

             vals.put(ImageDB.HOLIDAYS, filename);

        break;
        case Weather:
            vals.put(ImageDB.WEATHER, filename);

        break;
        }
             imagedb.tagImage(filename, vals); //logcat error is here

  return true;
}

在imageDB数据库类中,我有一个这样的方法:

public long tagImage(String pathname, ContentValues val){  
    return db.insert(DATABASE_TABLE, null, val);    //logcat error is here

}

我在数据库中收到此错误,但它不会导致我的应用程序崩溃:

05-19 02:44:55.580: E/Database(3088): Error inserting 
05-19 02:44:55.580: E/Database(3088): android.database.sqlite.SQLiteException: near "null": syntax error: , while compiling: INSERT INTO imagetags(null)  VALUES(NULL);
05-19 02:44:55.580: E/Database(3088):   at android.database.sqlite.SQLiteCompiledSql.native_compile(Native Method)
05-19 02:44:55.580: E/Database(3088):   at android.database.sqlite.SQLiteCompiledSql.compile(SQLiteCompiledSql.java:92)
05-19 02:44:55.580: E/Database(3088):   at android.database.sqlite.SQLiteCompiledSql.<init>(SQLiteCompiledSql.java:65)
05-19 02:44:55.580: E/Database(3088):   at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:83)
05-19 02:44:55.580: E/Database(3088):   at android.database.sqlite.SQLiteStatement.<init>(SQLiteStatement.java:41)
05-19 02:44:55.580: E/Database(3088):   at android.database.sqlite.SQLiteDatabase.compileStatement(SQLiteDatabase.java:1231)
05-19 02:44:55.580: E/Database(3088):   at android.database.sqlite.SQLiteDatabase.insertWithOnConflict(SQLiteDatabase.java:1658)
05-19 02:44:55.580: E/Database(3088):   at android.database.sqlite.SQLiteDatabase.insert(SQLiteDatabase.java:1515)
05-19 02:44:55.580: E/Database(3088):   at com.MyApps.ImageUpdate.ImageDB.tagImage(ImageDB.java:82)
05-19 02:44:55.580: E/Database(3088):   at com.MyApps.ImageUpdate.ViewImage.onContextItemSelected(ViewImage.java:134)
05-19 02:44:55.580: E/Database(3088):   at android.app.Activity.onMenuItemSelected(Activity.java:2254)

我可能做错了什么?..我没有正确使用ContentValues吗?

谢谢。

2 个答案:

答案 0 :(得分:1)

您的selection既不是Holidays也不是Weather

答案 1 :(得分:0)

在某些情况下,可能没有任何案例开关是真的,因此“val”中不会添加任何内容。