0

我开始有点发疯了哈哈,我花了两天时间试图修复下面的这个错误,有人可以帮助我,因为我无法解决:(

//DB Adapter 类创建表

public class DBAdapter {

    private static final String vd_offer = ("CREATE TABLE " + TABLE_VD_OFFER  + " ("
                + OFFER_ID + " INTEGER, " // PRIMARY KEY
                + VENUE_ID + " INTEGER, " 
                + OFFER_COST + " INTEGER, " 
                + OFFER_NAME + " TEXT, " 
                + OFFER_DESC_SHORT + " TEXT, " 
                + OFFER_DESC_LONG + " TEXT, " 
                + OFFER_DATE_START + " TEXT, " 
                + OFFER_DATE_END + " TEXT, " 
                + OFFER_RECUR + " TEXT );"
                );

    private final Context context; 
    private DatabaseHelper DBHelper;
    private SQLiteDatabase db;


    public DBAdapter(Context ctx)
    {
        this.context = ctx;
        this.DBHelper = new DatabaseHelper(this.context);
    }

    private static class DatabaseHelper extends SQLiteOpenHelper 
    {
        DatabaseHelper(Context context) 
        {
            super(context, DATABASE_NAME, null, DATABASE_VERSION);
        }

        @Override
        public void onCreate(SQLiteDatabase db) 
        {
            db.execSQL(vd_offer); 
        }

        @Override
        public void onUpgrade(SQLiteDatabase db, int oldVersion, 
        int newVersion) 
        {               
            db.execSQL("DROP TABLE IF EXISTS " + TABLE_VD_OFFER);
            onCreate(db);
        }
    } 

    public DBAdapter open() throws SQLException 
    {
        this.db = this.DBHelper.getWritableDatabase();
        return this;
    }

    public void close() 
    {
        this.DBHelper.close();
    }
}

// 将变量传递给 DB 的类

public class VDOffer {

    private DatabaseHelper mDbHelper;
    private SQLiteDatabase mDb;

    private final Context mCtx;

    private static class DatabaseHelper extends SQLiteOpenHelper {

        DatabaseHelper(Context context) {
            super(context, DATABASE_NAME, null, DATABASE_VERSION);
        }

        @Override
        public void onCreate(SQLiteDatabase db) {
        }

        @Override
        public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        }
    }


    public VDOffer(Context ctx) {
        this.mCtx = ctx;
    }

    public VDOffer open() throws SQLException {
        this.mDbHelper = new DatabaseHelper(this.mCtx);
        this.mDb = this.mDbHelper.getWritableDatabase();
        return this;
    }

    public void close() {
        this.mDbHelper.close();
    }

    public long addOffer (int offer_id, int venue_id, int offer_cost, 
            String offer_name, String offer_desc_short, String offer_desc_long,
            String offer_date_start, String offer_date_end, String offer_recur){

        ContentValues values = new ContentValues();

        values.put(OFFER_ID, offer_id);
        values.put(VENUE_ID, venue_id);
        values.put(OFFER_COST, offer_cost);
        values.put(OFFER_NAME, offer_name);
        values.put(OFFER_DESC_SHORT, offer_desc_short);
        values.put(OFFER_DESC_LONG, offer_desc_long);
        values.put(OFFER_DATE_START, offer_date_start);
        values.put(OFFER_DATE_END, offer_date_end);
        values.put(OFFER_RECUR, offer_recur);       

        return this.mDb.insert(TABLE_VD_OFFER, null, values);

    }
}

// 只是将一些代码拼凑在一起来测试输入

public void update (){

    int offer_id = 1;
    int venue_id = 1;
    int offer_cost = 1;
    String offer_name = "test";
    String offer_desc_short = "test";
    String offer_desc_long = "test";
    String offer_date_start = "test";
    String offer_date_end = "test";
    String offer_recur = "test";        

    VDOffer entry = new VDOffer(Events.this);

    boolean didItWork = true;
    try {
        entry.open();
        entry.addOffer(offer_id, venue_id, offer_cost, offer_name, offer_desc_short, offer_desc_long, offer_date_start, offer_date_end, offer_recur);
        entry.close();

    } catch (Exception e) {
        didItWork = false;
        String error = e.toString();
        Dialog d = new Dialog(this);
        d.setTitle("Dang it!");
        TextView tv = new TextView(this);
        tv.setText(error);
        d.setContentView(tv);
        d.show();
    } finally {
        if (didItWork) {
            Dialog d = new Dialog(this);
            d.setTitle("Heck Yea!");
            TextView tv = new TextView(this);
            tv.setText("Success");
            d.setContentView(tv);
            d.show();
            entry.close();
        }
    }       

}

// 记录猫

05-29 23:56:43.021: W/KeyCharacterMap(30728): No keyboard for id 0
05-29 23:56:43.021: W/KeyCharacterMap(30728): Using default keymap: /system/usr/keychars/qwerty.kcm.bin
05-29 23:56:47.960: D/dalvikvm(30728): GC_EXTERNAL_ALLOC freed 1925 objects / 139800 bytes in 362ms
05-29 23:56:49.840: I/Database(30728): sqlite returned: error code = 1, msg = no such table: vd_offer
05-29 23:56:49.850: E/Database(30728): Error inserting offer_desc_long=test offer_desc=test offer_date_end=test offer_id=1 offer_recur=test offer_name=test venue_id=1 offer_date_start=test offer_cost=1
05-29 23:56:49.850: E/Database(30728): android.database.sqlite.SQLiteException: no such table: vd_offer: , while compiling: INSERT INTO vd_offer(offer_desc_long, offer_desc, offer_date_end, offer_id, offer_recur, offer_name, venue_id, offer_date_start, offer_cost) VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?);
4

1 回答 1

1

您有两个(!!!)类被调用DatabaseHelper,其中只有一个具有创建表的代码。您需要丢失没有表创建代码的一个,并确保指向另一个来创建数据库。

编辑

您应该在 dbhelper 类中包含所有数据库活动,然后实例化它并在需要时使用它。摆脱DatabaseHelper@中的类vdOffer并使用它:

private DBAdapter mDbhelper;
mDbHelper = new DBAdapter(this);
mDbHelper.open();

然后在那里创建插入方法并访问它们。

mDbHelper.insertOffer(parameters);
于 2012-06-01T00:40:43.837 回答