【发布时间】:2019-07-03 01:06:38
【问题描述】:
我在 Android Studio 上用 SQL 创建了一个数据库,但我无法恢复和更新这个数据库中的数据。
我检查了很多网站和指南,但没有一个给我正确的答案。
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("Create table user(pseudo text primary key, password text, email text, points integer, pointsshop integer)");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("drop table if exists user");
}
//inserting in database
public boolean insert(String pseudo, String password, String email, int points, int pointsshop){
SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put("pseudo",pseudo);
contentValues.put("password",password);
contentValues.put("email",email);
contentValues.put("points",points);
contentValues.put("pointsshop",pointsshop);
long ins = db.insert("user",null,contentValues);
if(ins==-1)return false;
else return true;
}
public void update_points(int points, String pseudo){
this.getWritableDatabase().execSQL("update user set
points='" + points + "' where pseudo='" + pseudo + "'");
}
public int get_points(String pseudo){
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.rawQuery("Select points from user where
pseudo=?",new String[]{pseudo});
return cursor.getInt(0);
}
}
}
我想从我的数据库中获取数据“点”,但是当我运行我的应用程序时,这个崩溃了
【问题讨论】:
标签: java sqlite android-studio