lundi 22 décembre 2014

Android: Table Row Insert Error. Column does not exist?

So previously I implemented a DB with a table that had 2 columns. I changed my mind and added one more column to the table and tried inserting the new value and I faced this problem even though I correctly (I believe) added all the right column declarations in the classes mentioned below.


TableData Class that contains the column names and data:



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";
}
}


And my Query in the DatabaseOperations class:



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


And my create query execution:



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


I believe those are the two declarations. However I started getting THIS error when I added a new column "ImageID". It worked well before when it had only 2 columns.



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://ift.tt/1GNfyLT
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)


And my SELECT query crashed as well, returning this error:



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


My SELECT Query and code block:



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


Why is this happening? I have added the new column correctly havent I? Please help :)


Aucun commentaire:

Enregistrer un commentaire