将数据添加到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";
    }
}


我究竟做错了什么?

最佳答案

请注意,数据库以前是由另一种方法创建的,以便将数据添加到另一张表中。我在这里所做的只是将数据添加到同一数据库中的另一个表中。那是问题的根源吗?


是的,就是这个问题。每个数据库文件仅使用一个SQLiteOpenHelper子类。

发生的情况是请求版本为1的数据库文件已经存在,因此没有调用诸如onCreate()onUpgrade()之类的数据库帮助程序生命周期回调。

进一步阅读:When is SQLiteOpenHelper onCreate() / onUpgrade() run?

08-18 04:04
查看更多