我尝试使用sqlite更新我的数据库。一切运行良好,但数据库没有更新。 我已经使用这个数据库来存储用户。但是,当我想更新它时,它无法正常工作。
logcat为空
这是我尝试通过硬编码来更新的功能:
public void updateUser(String email, String total_usage) {
SQLiteDatabase db = this.getWritableDatabase();
db.rawQuery("UPDATE login SET total_usage = 132 WHERE email = 'sampleMail'", null);
db.rawQuery("UPDATE login SET total_usage = 132 WHERE email = 'sampleMail'", null);
}
也许问题出在其他地方。这是完整的java文件:
public class DatabaseHandler extends SQLiteOpenHelper {
// All Static variables
// Database Version
private static final int DATABASE_VERSION = 1;
// Database Name
private static final String DATABASE_NAME = "android_api";
// Login table name
private static final String TABLE_LOGIN = "login";
// Login Table Columns names
private static final String KEY_ID = "id";
private static final String KEY_NAME = "name";
private static final String KEY_EMAIL = "email";
private static final String KEY_UID = "uid";
private static final String KEY_CREATED_AT = "created_at";
private static final String KEY_TOTAL_USAGE = "total_usage";
public DatabaseHandler(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
// Creating Tables
@Override
public void onCreate(SQLiteDatabase db) {
String CREATE_LOGIN_TABLE = "CREATE TABLE " + TABLE_LOGIN + "("
+ KEY_ID + " INTEGER PRIMARY KEY,"
+ KEY_NAME + " TEXT,"
+ KEY_EMAIL + " TEXT UNIQUE,"
+ KEY_UID + " TEXT,"
+ KEY_CREATED_AT + " TEXT,"
+ KEY_TOTAL_USAGE + " TEXT);";
db.execSQL(CREATE_LOGIN_TABLE);
}
// Upgrading database
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// Drop older table if existed
db.execSQL("DROP TABLE IF EXISTS " + TABLE_LOGIN);
// Create tables again
onCreate(db);
}
/**
* Storing user details in database
* */
public void addUser(String name, String email, String uid, String created_at) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(KEY_NAME, name); // Name
values.put(KEY_EMAIL, email); // Email
values.put(KEY_UID, uid); // Email
values.put(KEY_CREATED_AT, created_at); // Created At
// values.put(KEY_TOTAL_USAGE, total_usage); // Total_usage
// Inserting Row
db.insert(TABLE_LOGIN, null, values);
db.close(); // Closing database connection
}
/**
* Storing user details in database
* */
public void updateUser(String email, String total_usage) {
SQLiteDatabase db = this.getWritableDatabase();
db.rawQuery("UPDATE login SET total_usage = 132 WHERE email = 'sampleMail'", null);
db.rawQuery("UPDATE login SET total_usage = 132 WHERE email = 'sampleMail'", null);
}
/**
* Getting user data from database
* */
public HashMap<String, String> getUserDetails(){
HashMap<String,String> user = new HashMap<String,String>();
String selectQuery = "SELECT * FROM " + TABLE_LOGIN;
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.rawQuery(selectQuery, null);
// Move to first row
cursor.moveToFirst();
if(cursor.getCount() > 0){
user.put("name", cursor.getString(1));
user.put("email", cursor.getString(2));
user.put("uid", cursor.getString(3));
user.put("created_at", cursor.getString(4));
}
cursor.close();
db.close();
// return user
return user;
}
/**
* Getting user login status
* return true if rows are there in table
* */
public int getRowCount() {
String countQuery = "SELECT * FROM " + TABLE_LOGIN;
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.rawQuery(countQuery, null);
int rowCount = cursor.getCount();
db.close();
cursor.close();
// return row count
return rowCount;
}
/**
* Re crate database
* Delete all tables and create them again
* */
public void resetTables(){
SQLiteDatabase db = this.getWritableDatabase();
// Delete All Rows
db.delete(TABLE_LOGIN, null, null);
db.close();
}
}
我不能做错的事。希望有人能看到它。 这是一个类似的问题:Android SQLite update query not working?但它并没有解决我的问题。我变得绝望,如果有人能帮助我,我真的很感激。
以下是我的数据库的图片(上传图片的声誉不足):http://nl.tinypic.com/r/mm8nxg/8
以下代码无效:
public void updateUser(String email, String total_usage) {
String selectQuery = "UPDATE login SET total_usage = 132 WHERE email = 'sampleMail'";
SQLiteDatabase db = this.getWritableDatabase();
db.execSQL(selectQuery);
}
答案 0 :(得分:2)
db.rawQuery("UPDATE login SET total_usage = 132 WHERE email = 'sampleMail'", null); db.rawQuery("UPDATE login SET total_usage = 132 WHERE email = 'sampleMail'", null);
使用execSQL()
而非rawQuery()
进行此类查询。
rawQuery()
只编译SQL但不运行它。 execSQL()
编译并运行它。 (带rawQuery()
的SQL仅在移动返回的游标时执行。)
答案 1 :(得分:0)
您需要拥有原始数据库创建脚本,但您还需要告诉数据库您需要哪些更改。
对我来说,我创建了这个字符串:
private static final String ALTER_TABLE =
"ALTER TABLE player ADD COLUMN showscores INTEGER default 1";
这会在我现有的数据库中添加一列。但我必须在onUpgrade中实现这一点。
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
Log.w(TAG, "Upgrading database from version " + oldVersion + " to "
+ newVersion + ". No data will be destroyed");
if (oldVersion != newVersion) {
db.execSQL(ALTER_TABLE);
}
}
答案 2 :(得分:0)
在构造函数
中public DatabaseHandler(Context context) {
super(context, DATABASE_NAME", null, DATABASE_VERSION); // be aware of that number, when you change your db add one plus
this.onCreate(this.getWritableDatabase());
}
及以下
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(createTable);//execute all querys to create it
}