开发者

Android: database reading problem throws exception

开发者 https://www.devze.com 2022-12-25 05:35 出处:网络
i am having this problem with the android database. I adopted the DBAdapter file the NotepadAdv3 example from the google android page.

i am having this problem with the android database. I adopted the DBAdapter file the NotepadAdv3 example from the google android page.

DBAdapter.java

public class DBAdapter {
private static final String TAG = "DBAdapter";

private static final String DATABASE_NAME = "PasswordDb";
private static final String DATABASE_TABLE = "myuserdata";
private static final String DATABASE_USERKEY = "myuserkey";
private static final int DATABASE_VERSION = 2;

public static final String KEY_USERKEY = "userkey";
public static final String KEY_TITLE = "title";
public static final String KEY_DATA = "data";
publ开发者_如何学Pythonic static final String KEY_ROWID = "_id";

private final Context mContext;

private DatabaseHelper mDbHelper;
private SQLiteDatabase mDb;

private static final String DB_CREATE_KEY =
    "create table " + DATABASE_USERKEY
    + " ("
    + "userkey text not null"
    +");";

private static final String DB_CREATE_DATA =
    "create table " + DATABASE_TABLE
    + " ("
    + "_id integer primary key autoincrement, "
    + "title text not null"
    + "data text"
    +");";

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

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

    @Override
        public void onUpgrade(SQLiteDatabase db, int oldVersion, 
        int newVersion) 
    {
        Log.w(TAG, "Upgrading database from version " + oldVersion 
            + " to "
            + newVersion + ", which will destroy all old data");
        db.execSQL("DROP TABLE IF EXISTS myuserkey");
        db.execSQL("DROP TABLE IF EXISTS myuserdata");            
        onCreate(db);
    }
}

public DBAdapter(Context ctx)
{
    this.mContext = ctx;
}

public DBAdapter Open() throws SQLException{    
    try {
        mDbHelper = new DatabaseHelper(mContext); 
    }
    catch(Exception e){
        Log.e(TAG, e.toString());
    }
    mDb = mDbHelper.getWritableDatabase();
    return this;
}

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

public Long storeKey(String userKey){
    ContentValues initialValues = new ContentValues();

    initialValues.put(KEY_USERKEY, userKey);
    try {
        mDb.delete(DATABASE_USERKEY, "1=1", null);
    }
    catch(Exception e)
    {
        Log.e(TAG, e.toString());
    }
    return mDb.insert(DATABASE_USERKEY, null, initialValues);
}

public String retrieveKey() {   
    final Cursor c;
    try {
        c = mDb.query(DATABASE_USERKEY, new String[] {
            KEY_USERKEY}, 
                null, 
                null, 
                null, 
                null, 
                null);
    }catch(Exception e){
        Log.e(TAG, e.toString());
        return "";
    }

    if(c.moveToFirst()){
        return c.getString(0);
    }
    else{
        Log.d(TAG, "UserKey Empty");
    }
    return "";
}
//not including any function related to "myuserdata" table

}

Class1.java

{
mUserKey = mDbHelper.retrieveKey();
mDbHelper.storeKey(Key);

}

the error that i am receiving is from Log.e(TAG, e.toString()) in the methods retrieveKey() and storeKey()

"no such table: myuserkey: , while compiling: SELECT userkey FROM myuserkey"


Did you pop the DB version so onUpgrade fires? You're at version 2 in the example above but if you changed the schema since version 2 then you need to pop the version again.


i think you are not mention your provider tag in AndroidManifest.xml .Check it.

u have to mention the tag like

 <provider android:name=".name of providerclass"
          android:authorities="authority path" />
0

精彩评论

暂无评论...
验证码 换一张
取 消