数据库插入在Android模拟器上运行良好,但在设备上运行不正常

时间:2012-02-29 12:38:47

标签: android database sqlite

我使用this tutorial创建了一个数据库。

当我在表格中插入值时,它可以在模拟器上运行,我已经看到值正确插入。但是当我在设备上运行我的应用程序时,它给了我这个错误:

02-29 17:30:45.341: E/Database(3300):android.database.sqlite.SQLiteException:no such   table: recently_used: , while compiling: INSERT INTO recently_used(iconsset_name, icon_name) VALUES(?, ?); 
02-29 17:30:45.341: E/Database(3300):   at android.database.sqlite.SQLiteCompiledSql.native_compile(Native Method) 
02-29 17:30:45.341: E/Database(3300):at   android.database.sqlite.SQLiteCompiledSql.compile(SQLiteCompiledSql.java:92) 
02-29 17:30:45.341: E/Database(3300):at android.database.sqlite.SQLiteCompiledSql.<init>(SQLiteCompiledSql.java:65)
02-29 17:30:45.341: E/Database(3300):   at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:83) 
02-29 17:30:45.341: E/Database(3300):   at android.database.sqlite.SQLiteStatement.<init>  (SQLiteStatement.java:41)

这是我的数据库类。

public class DBHelpter extends SQLiteOpenHelper{

    private static final String DATABASE_NAME = "DatabaseName.db";

    public static final String ICONS_SET_TABLE_NAME = "iconset";
    public static final String ID_ICONS_SET = "_id";
    public static final String NAME_ICONS_SET ="name";
    public static final String SERVER_ID_ICONS_SET = "serverid";
    public static final String PRODUCT_ID_ICONSET = "productid";

    public static final String ICON_TABLE_NAME = "icons";
    public static final String ID_ICONS = "_id";

    public static final String SERVER_ICON_ID = "serverid";
    public static final String ICONSET_ID_ICONS = "iconsetid";

    public static final String RECENTLY_USED_TABLE_NAME = "recently_used";
    public static final String ICONS_NAME_RECENTLY = "icon_name";
    public static final String ICONSSET_NAME_RECENTLY = "iconsset_name";
    public static final String RECENTLY_ICONSET_ID = "_id";


    public static final int DATABASE_VERSION_NO = 3;
    public Context mContext = null;

    public static final String mIconsSetQuery = "CREATE TABLE "+ICONS_SET_TABLE_NAME + "(" + ID_ICONS_SET +" INTEGER PRIMARY KEY AUTOINCREMENT," + NAME_ICONS_SET +" TEXT," + SERVER_ID_ICONS_SET +" INTEGER," + PRODUCT_ID_ICONSET +" TEXT);";
    public static final String mIcons = "CREATE TABLE "+ ICON_TABLE_NAME + "(" + ID_ICONS+ " INTEGER PRIMARY KEY  AUTOINCREMENT,"+ SERVER_ICON_ID + " INTEGER,"+ICONSET_ID_ICONS + " INTEGER);";
    public static final String recently_table_query = "CREATE TABLE "+ RECENTLY_USED_TABLE_NAME + "(_id INTEGER PRIMARY KEY AUTOINCREMENT, icon_name TEXT,iconsset_name TEXT);";

    public DBHelpter(Context context) {

        super(context, DATABASE_NAME, null, DATABASE_VERSION_NO);
        mContext = context;
    }

    @Override
    public synchronized void close() {
        super.close();
    }

    @Override
    public synchronized SQLiteDatabase getReadableDatabase() {
        return super.getReadableDatabase();
    }

    @Override
    public synchronized SQLiteDatabase getWritableDatabase() {
        return super.getWritableDatabase();
    }

    @Override
    public void onCreate(SQLiteDatabase db) {

        db.execSQL(mIconsSetQuery);
        db.execSQL(mIcons);
        db.execSQL(recently_table_query);

    }

    @Override
    public void onOpen(SQLiteDatabase db) {
        super.onOpen(db);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        db.execSQL("DROP TABLE IF EXISTS " + ICONS_SET_TABLE_NAME);
        db.execSQL("DROP TABLE IF EXISTS " + ICON_TABLE_NAME);
        db.execSQL("DROP TABLE IF EXISTS " + RECENTLY_USED_TABLE_NAME);

        onCreate(db);
    }
}

这是我正在使用的方法。

public void recentlyUsed(RecentlyUsedIcons recentlyUsedIcons) {
     values.put(DBHelpter.ICONS_NAME_RECENTLY, recentlyUsedIcons.getIcon_name());
     values.put(DBHelpter.ICONSSET_NAME_RECENTLY, recentlyUsedIcons.getIconSet_name());
     mDatabase.insert(DBHelpter.RECENTLY_USED_TABLE_NAME, null, values);
}

2 个答案:

答案 0 :(得分:1)

我也遇到了这个问题

public static final int DATABASE_VERSION_NO = 3;

我将数据库版本1更改为2,之后它一直在运行。 将其更改为4并尝试重新在手机上重新编译。我希望它有效。

答案 1 :(得分:0)

问题是旧数据库仍然存储在设备内部,有两种方法可以解决问题:

  1. 尝试清除应用程序数据并从设备中卸载应用程序并重新安装。
  2. 通过在代码中将数据库版本设置为(例如)vers_no + 1来升级现有数据库版本。