我正在实现一个应用程序,我喜欢使用SQLite数据库来存储用户的一些信息。我需要在数据库中将id = 0更新为同一行。
user.clean() = Clear all the values stored in database (row with id = 0);
user.save() = update the values in the database (row with id = 0);
user.load() = get all the values from the database (row with id = 0);
我的问题是:应用程序没有从数据库中读取任何内容,我在logcat中没有例外。
我的课程:
/**
* This class is used to manage the user information.
*/
public class User extends SQLiteOpenHelper {
private static final String LOCAL_DB_NAME = "local_db";
private static final int LOCAL_DB_VERSION = 1;
public static final String USER_DB_TABLE = "local_user";
public static final String[] USER_ALL_COLUMNS = {"id", "name","email","birthday","country","credential","purchased","params"};
private static final String CREATE_LOCAL_DB = "CREATE TABLE IF NOT EXISTS " + USER_DB_TABLE +
"( id INTEGER PRIMARY KEY, name TEXT, email TEXT, birthday TEXT, country TEXT, credential TEXT, purchased TEXT, params TEXT)";
public User(Context context){
super(context, LOCAL_DB_NAME, null, LOCAL_DB_VERSION);
this.load();
}
@Override
public void onCreate(SQLiteDatabase database) {
database.execSQL(CREATE_LOCAL_DB);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
Log.w(User.class.getName(),
"Upgrading database from version " + oldVersion + " to "
+ newVersion + ", which will destroy all old data");
db.execSQL("DROP TABLE IF EXISTS " + USER_DB_TABLE);
onCreate(db);
}
private String name = "example";
private String birthday = "10-12-2000";
private String country = "br";
private String credential = "xxxxxxxx";
private String email = "xxxxx@xxxxxx";
private String purchased = "xxxxxxx";
private String params = "xxxx";
// This method saves the this user instance in the local database.
public void save(){
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put("id", 0);
values.put("name", this.name);
values.put("email", this.email);
values.put("birthday", this.birthday);
values.put("country", this.country);
values.put("credential", this.credential);
values.put("purchased", this.purchased);
values.put("params", this.params);
try{
db.update(USER_DB_TABLE, values, "id = ?", new String[] { String.valueOf(0) });
}catch (Exception e){
db.insert(USER_DB_TABLE, null, values);
}
db.close();
}
// This method cleans the local user stored in the database.
public void clean() {
this.name = "";
this.birthday = "";
this.country = "";
this.credential = "";
this.email = "";
this.purchased = "";
this.params = "";
this.save();
}
// This method loads the user stored in the database.
public void load() {
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.query(USER_DB_TABLE, USER_ALL_COLUMNS, "id =?",
new String[] { String.valueOf(0) }, null, null, null, null);
if (cursor.moveToFirst()){
this.name = cursor.getString(1);
this.birthday = cursor.getString(2);
this.country = cursor.getString(3);
this.credential = cursor.getString(4);
this.email = cursor.getString(5);
this.purchased = cursor.getString(6);
this.params = cursor.getString(7);
}else{
this.clean();
}
db.close();
}
}
-------------------------------解决--------------- ---------------------------
我只将第一个ID更改为1,并且完美运行。感谢。
答案 0 :(得分:0)
if (cursor != null)
cursor.moveToFirst();
this.name = cursor.getString(1);
光标为空。您应该检查moveToFirst()
的结果,如果它返回get...()
,则只调用true
:
if (cursor.moveToFirst()) {
this.name = cursor.getString(1);
...
} else {
// reset variables to empty/defaults
}
(虽然这是一个好习惯,但没有必要检查cursor != null
。)
答案 1 :(得分:0)
尝试添加
cursor.moveToPosition(位置);