无法读取我插入SQLite的数据

时间:2016-08-12 13:38:20

标签: android sqlite android-sqlite

我添加到DatabaseAccess类的INSERT命令后,我的数据库没有更新。

我验证了在使用INSERT命令的函数(sign_user)中,我可以通过在同一函数中使用查询来显示我在Toast消息上添加的数据。
但是当我尝试通过查询接收我从另一个函数(login_to_user)添加的数据时,它不会返回任何数据。

以下是我的功能:

public boolean login_to_user(String user_name,String password){
    boolean login_status = false;
    Cursor user_info = database.rawQuery("SELECT * FROM "+DatabaseHelper.TABLE_LOGIN+" WHERE user_name like '"+user_name+"' AND password like '"+password+"'",null);
    int row_count = user_info.getCount();
    user_info.close();
    if(row_count > 0){
        login_status = true;
    }
    return login_status;
}

public void sign_user(String user_name, String password, Context context){
        database.rawQuery("INSERT INTO "+DatabaseHelper.TABLE_LOGIN+" (user_name,password) VALUES ('" + user_name + "','" + password + "')",null);
        Cursor user_info = database.rawQuery("SELECT * FROM "+DatabaseHelper.TABLE_LOGIN+" WHERE user_name like '"+user_name+"' AND password like '"+password+"'",null);
        while(user_info.moveToNext()){
            String username=user_info.getString(0);
        }
        Toast toast = Toast.makeText(context,user_name,Toast.LENGTH_SHORT);
        toast.show();
    }

编辑:我的DatabaseAccess类:

public class DatabaseAccess {
private SQLiteOpenHelper openHelper;
private SQLiteDatabase database;
private static DatabaseAccess instance;

private DatabaseAccess(Context context) {
    this.openHelper = new DatabaseHelper(context);
}

public static DatabaseAccess getInstance(Context context) {
    if (instance == null) {
        instance = new DatabaseAccess(context);
    }
    return instance;
}

public void open() {
    this.database = openHelper.getWritableDatabase();
}

public void close() {
    if (database != null) {
        this.database.close();
    }
}

public void sign_user(String user_name, String password, Context context){
    database.rawQuery("INSERT INTO "+DatabaseHelper.TABLE_LOGIN+" (user_name,password) VALUES ('" + user_name + "','" + password + "')",null);
    Cursor user_info = database.rawQuery("SELECT * FROM "+DatabaseHelper.TABLE_LOGIN+" WHERE user_name like '"+user_name+"' AND password like '"+password+"'",null);
    while(user_info.moveToNext()){
        String username=user_info.getString(0);
    }
    Toast toast = Toast.makeText(context,user_name,Toast.LENGTH_SHORT);
    toast.show();
}

public boolean login_to_user(String user_name,String password){
    boolean login_status = false;
    Cursor user_info = database.rawQuery("SELECT * FROM "+DatabaseHelper.TABLE_LOGIN+" WHERE user_name like '"+user_name+"' AND password like '"+password+"'",null);
    int row_count = user_info.getCount();
    user_info.close();
    if(row_count > 0){
        login_status = true;
    }
    return login_status;
}

0 个答案:

没有答案