Android SQLite数据库(3300):sqlite返回:错误代码= 1

时间:2012-06-01 00:19:15

标签: android database sqlite

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

// 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();
    }
}

//将vaiables传递给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(?, ?, ?, ?, ?, ?, ?, ?, ?);

1 个答案:

答案 0 :(得分:1)

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

修改

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

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

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

mDbHelper.insertOffer(parameters);