Android:表行插入错误。列不存在?

时间:2014-12-22 08:53:51

标签: java android sql sqlite insert

所以以前我用一个有2列的表实现了一个DB。我改变主意,在表中添加了一个列并尝试插入新值,即使我正确地(我相信)在下面提到的类中添加了所有正确的列声明,我也遇到了这个问题。

包含列名和数据的TableData类:

public class ImageTableData {

    public ImageTableData()
    {

    }
    public static abstract class ImagesTableData implements BaseColumns
    {
        public static final String ImageID = "imid";
        public static final String PID = "pid";
        public static final String URL = "url";
        public static final String DATABASE_NAME = "jezza_db";
        public static final String TABLE_NAME = "product_pics";
    }
}

我在DatabaseOperations课程中的查询:

public String CREATE_IMAGE_QUERY = "CREATE TABLE " + ImagesTableData.TABLE_NAME + " (" + ImagesTableData.ImageID + " VARCHAR, " + ImagesTableData.PID + " VARCHAR, " + ImagesTableData.URL + " VARCHAR);";

我的创建查询执行:

public void onCreate(SQLiteDatabase db) {
        db.execSQL(CREATE_IMAGE_QUERY);
        }

我相信这是两个宣言。但是,当我添加新列“ImageID”时,我开始收到此错误。它之前只有2列,效果很好。

    12-22 14:13:25.345: E/SQLiteLog(21777): (1) table product_pics has no column named imid
    12-22 14:13:25.345: E/SQLiteDatabase(21777): Error inserting pid=77153628 imid=imablack3jpg url=http://storage.googleapis.com/bucketjezza/black3.jpg
    12-22 14:13:25.345: E/SQLiteDatabase(21777): android.database.sqlite.SQLiteException: table product_pics has no column named imid (code 1): , while compiling: INSERT INTO product_pics(pid,imid,url) VALUES (?,?,?)
    12-22 14:13:25.345: E/SQLiteDatabase(21777):    at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
    12-22 14:13:25.345: E/SQLiteDatabase(21777):    at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:1121)
    12-22 14:13:25.345: E/SQLiteDatabase(21777):    at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:694)
    12-22 14:13:25.345: E/SQLiteDatabase(21777):    at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
    12-22 14:13:25.345: E/SQLiteDatabase(21777):    at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)
    12-22 14:13:25.345: E/SQLiteDatabase(21777):    at android.database.sqlite.SQLiteStatement.<init>(SQLiteStatement.java:31)
    12-22 14:13:25.345: E/SQLiteDatabase(21777):    at android.database.sqlite.SQLiteDatabase.insertWithOnConflict(SQLiteDatabase.java:1589)
    12-22 14:13:25.345: E/SQLiteDatabase(21777):    at android.database.sqlite.SQLiteDatabase.insert(SQLiteDatabase.java:1461)
    12-22 14:13:25.345: E/SQLiteDatabase(21777):    at function.DBOps.insertImages(DBOps.java:138)
    12-22 14:13:25.345: E/SQLiteDatabase(21777):    at activity.classes.HomeActivity.commitToDatabase(HomeActivity.java:287)
    12-22 14:13:25.345: E/SQLiteDatabase(21777):    at activity.classes.HomeActivity$2.onSuccess(HomeActivity.java:235)

我的SELECT查询也崩溃了,返回此错误:

12-22 14:13:25.355: E/SQLiteLog(21777): (1) no such column: ImageID
12-22 14:13:25.355: E/DB ERROR(21777): android.database.sqlite.SQLiteException: no such column: ImageID (code 1): , while compiling: SELECT ImageID FROM product_pics WHERE ImageID=imaimablack3jpg

我的SELECT查询和代码块:

public boolean isImageAdded(DBOps dop, String id)
    {
        try
        {
            String idmod = "ima" + id;
            boolean isAdded = false;
            Log.d("URL Recieved: ", id);
            SQLiteDatabase SQ = dop.getReadableDatabase();
            String sqlQuery = "SELECT ImageID FROM "+ ImagesTableData.TABLE_NAME +" WHERE ImageID="+idmod; 
            Cursor cursor = SQ.rawQuery(sqlQuery, null);
            Log.d("Cursor Count : ", String.valueOf(cursor.getCount()));
            if(cursor.getCount() > 0)
            {
                cursor.close();
                isAdded = true;
            }
            else
            {
                cursor.close();
                isAdded = false;
            }
        }
        catch(Exception ex)
        {
            Log.e("DB ERROR", ex.toString());
        }
        return false;
    }

为什么会这样?我已正确添加新列了吗?请帮助:)

4 个答案:

答案 0 :(得分:2)

更正您的查询

String sqlQuery = "SELECT imid FROM "+ ImagesTableData.TABLE_NAME +" WHERE imid ='"+idmod+"'"; 

表格中没有列名ImageID。您定义了ImageID=imid

或其他方式

 String sqlQuery = "SELECT "+ ImagesTableData.ImageID +" FROM "+ ImagesTableData.TABLE_NAME +" WHERE "+ ImagesTableData.ImageID + " = '"+idmod+"'"; 

答案 1 :(得分:1)

您正在尝试检索不存在的字段ImageID

String sqlQuery = "SELECT ImageID FROM "+  ...

相反,您有一个名为imid的字段,如下所示:

public static final String ImageID = "imid";

所以,正确的查询是:

String sqlQuery = "SELECT imid FROM "+  ...

答案 2 :(得分:1)

卸载应用程序并重新安装,然后您的问题就会解决。

因为只要数据库文件不在应用程序目录中,就只会调用数据库帮助程序类的onCreate()方法。如果我们的应用程序目录中有数据库文件可用,则不会调用数据库助手类的onCreate()方法。如果重新安装应用程序而不卸载数据库文件将不会被删除。所以不会执行onCreate()数据库助手类。

答案 3 :(得分:1)

更改您的SQL查询
"SELECT ImageID FROM "+ ImagesTableData.TABLE_NAME

"SELECT " + ImagesTableData.ImageID + " FROM "+ ImagesTableData.TABLE_NAME