在向Sqlite数据库中的表添加数据时出现此错误。
12-21 17:31:19.345: E/SQLiteLog(24127): (1) no such table: accessory_data
12-21 17:31:19.345: E/DB ERROR(24127): android.database.sqlite.SQLiteException:
no such table: accessory_data (code 1): ,
while compiling: SELECT aid, name, image, url, price
FROM accessory_data
请注意,数据库以前是由另一种方法创建的,以便将数据添加到另一个表中。我在这里做的只是将数据添加到同一数据库中的不同表。这是问题的根源吗?
DBOperations类:
public class DBOps extends SQLiteOpenHelper {
private static final int DATABASE_VERSION = 1;
private static final String DATABASE_NAME = "jezzadb";
public String CREATE_ACC_QUERY = "CREATE TABLE " + AccessoriesTableInfo.TABLE_NAME + " (" + AccessoriesTableInfo.AID + " VARCHAR, " +
AccessoriesTableInfo.NAME + " VARCHAR, " + AccessoriesTableInfo.IMAGE + " VARCHAR, " + AccessoriesTableInfo.PRICE + " VARCHAR, " +
AccessoriesTableInfo.URL + " VARCHAR);";
public DBOps(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
// TODO Auto-generated constructor stub
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(CREATE_ACC_QUERY);
}
public void insertAccessories(DBOps dop, Accessories a)
{
SQLiteDatabase SQ = dop.getWritableDatabase();
ContentValues cv = new ContentValues();
cv.put(AccessoriesTableInfo.AID, a.ID);
cv.put(AccessoriesTableInfo.NAME, a.Name);
cv.put(AccessoriesTableInfo.URL, a.URL);
cv.put(AccessoriesTableInfo.IMAGE, a.Image);
cv.put(AccessoriesTableInfo.PRICE, a.PRICE);
SQ.insert(AccessoriesTableInfo.TABLE_NAME, null, cv);
Log.d("Database Operations", "Inserted Accessories succesfully!");
}
public ArrayList<Accessories> getAccessories(DBOps dop)
{
ArrayList<Accessories> arr = new ArrayList<Accessories>();
SQLiteDatabase SQ = dop.getReadableDatabase();
String[] columns = {AccessoriesTableInfo.AID,AccessoriesTableInfo.NAME,AccessoriesTableInfo.IMAGE,AccessoriesTableInfo.URL,
AccessoriesTableInfo.PRICE};
Cursor CR = SQ.query(AccessoriesTableInfo.TABLE_NAME, columns, null, null, null, null, null);
CR.moveToFirst();
for(int i=0;i<CR.getCount();i++)
{
while (!CR.isAfterLast()) {
Accessories a= new Accessories();
a.ID = CR.getString(0);
a.Name = CR.getString(1);
a.URL = CR.getString(2);
a.Image = CR.getString(3);
a.PRICE = CR.getString(4);
arr.add(a);
CR.moveToNext();
}
CR.close();
break;
}
return arr;
}
AccessoriesTableInfo类:
public class AccessoriesTableData {
public AccessoriesTableData()
{
}
public static abstract class AccessoriesTableInfo implements BaseColumns
{
public static final String AID = "aid";
public static final String NAME = "name";
public static final String IMAGE = "image";
public static final String URL = "url";
public static final String PRICE = "price";
public static final String TABLE_NAME = "accessory_data";
}
}
我做错了什么?
答案 0 :(得分:1)
请注意,数据库以前是由另一种方法创建的,以便将数据添加到另一个表中。我在这里做的只是将数据添加到同一数据库中的不同表。这是问题的根源吗?
是的,这就是问题所在。每个数据库文件只使用一个SQLiteOpenHelper
子类。
所发生的情况是具有所请求版本1的数据库文件已存在,因此不会调用onCreate()
或onUpgrade()
等数据库帮助程序生命周期回调。
进一步阅读:When is SQLiteOpenHelper onCreate() / onUpgrade() run?