如何从sqlite中选择并返回值?

时间:2013-11-28 07:55:29

标签: sqlite

我已创建数据库并将值插入表中。现在我正在尝试选择数据,但我收到了以下错误:enter image description here

我已经粘贴了我的整个SQLHelper类代码。请问任何人都可以让我知道我在哪里做错了吗?

public class DatabaseHelper extends SQLiteOpenHelper {
    private static final int DATABASE_VERSION = 1;

    // Database Name
    private static final String DATABASE_NAME = "MyDB.db";

    // Table Names
    private static final String TABLE_USERS = "Users";
    private static final String TABLE_SCORES = "Scores";
    private static final String TABLE_SYNCSTATUS = "SyncStatus";

    // Common column names
    private static final String KEY_ID = "id";
    private static final String KEY_CREATED_AT = "created_at";

    // USERS Table - column names
    private static final String KEY_USERID = "userID";
    private static final String KEY_USERNAME = "userName";
    private static final String KEY_USERPSWD = "userPswd";
    private static final String KEY_FIRSTNAME = "firstName";
    private static final String KEY_LASTNAME = "lastName";

    // SCORES Table - column names
    private static final String KEY_USER_ID = "userID";
    private static final String KEY_GAME_ID = "gameID";
    private static final String KEY_SCORES = "scores";

    // SYNCSTATUS Table - column names
    private static final String KEY_STATRDATE = "startDate";
    private static final String KEY_ENDDATE = "endDate";
    private static final String KEY_STATUS = "status";

    // Table Create Statements
    // USERS table create statement
    private static final String CREATE_TABLE_USERS = "CREATE TABLE IF NOT EXISTS "
            + TABLE_USERS + "(" + KEY_ID + " INTEGER PRIMARY KEY AUTOINCREMENT," + KEY_USERID
            + " TEXT," + KEY_USERNAME + " INTEGER," + KEY_USERPSWD + " TEXT," 
            + KEY_FIRSTNAME + " TEXT," + KEY_LASTNAME + " TEXT,"  
            + KEY_CREATED_AT + " DATETIME" + ")";

    // Scores table create statement
    private static final String CREATE_TABLE_SCORES = "CREATE TABLE IF NOT EXISTS "
            + TABLE_SCORES + "(" + KEY_ID + " INTEGER PRIMARY KEY AUTOINCREMENT," + KEY_USER_ID 
            + " INTEGER," + KEY_GAME_ID + " INTEGER," + KEY_SCORES + " INTEGER," 
            + KEY_CREATED_AT + " DATETIME" + ")";

    // Sync table create statement
    private static final String CREATE_TABLE_SYNCSTATUS = "CREATE TABLE IF NOT EXISTS "
            + TABLE_SYNCSTATUS + "(" + KEY_ID + " INTEGER PRIMARY KEY AUTOINCREMENT,"
            + KEY_STATRDATE + " DATETIME," + KEY_ENDDATE + " DATETIME," + KEY_STATUS + " INTEGER,"
            + KEY_CREATED_AT + " DATETIME" + ")";

    public DatabaseHelper(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        // creating required tables
        db.execSQL(CREATE_TABLE_USERS);
        db.execSQL(CREATE_TABLE_SCORES);
        db.execSQL(CREATE_TABLE_SYNCSTATUS);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        // on upgrade drop older tables
        db.execSQL("DROP TABLE IF EXISTS " + TABLE_USERS);
        db.execSQL("DROP TABLE IF EXISTS " + TABLE_SCORES);
        db.execSQL("DROP TABLE IF EXISTS " + TABLE_SYNCSTATUS);
        // create new tables
        onCreate(db);
    }

    public int validateUserLogin(String uname, String pswd)
    {
        SQLiteDatabase db = this.getReadableDatabase();
        String[] whereArgs = new String[]{uname, pswd};

        String query = "SELECT "+KEY_USERID+" FROM "+TABLE_USERS+" WHERE "+KEY_USERNAME+" = ? AND "+KEY_USERPSWD+" = ?";
        try{
            Cursor cur= db.rawQuery(query, whereArgs);
                    //Boolean b = cur.moveToFirst();
                    if (cur.moveToFirst() == true)
                    {
                        return cur.getInt(cur.getColumnIndex(KEY_USERID));
                    }
                    else
                        return -1;
        }catch(Exception e){
            return -1;
        }

    }

    public void insertValues(UsersCredential uc){
        SQLiteDatabase db= this.getWritableDatabase();
        ContentValues cv=new ContentValues();
        cv.put(KEY_USERID, uc.getUserID());
        cv.put(KEY_USERNAME, uc.getUserName());
        cv.put(KEY_USERPSWD, uc.getUserPassword());
        cv.put(KEY_FIRSTNAME, uc.getUserFirstName());
        cv.put(KEY_LASTNAME, uc.getUserLastName());
        cv.put(KEY_CREATED_AT, getDateTime());
        db.insert(TABLE_USERS, null, cv);
    }

    public void deleteRecords() {
        SQLiteDatabase db= this.getWritableDatabase();
        db.execSQL("delete from Users");
    }

    private String getDateTime() {
        SimpleDateFormat dateFormat = new SimpleDateFormat(
                "dd-MM-yyyy HH:mm:ss", Locale.getDefault());
        Date date = new Date();
        return dateFormat.format(date);
    }
}

我在validateUserLogin(String uname, String pswd)方法中遇到了异常,我的代码出了什么问题?

1 个答案:

答案 0 :(得分:1)

您的查询不会返回任何数据。 在您尝试访问数据之前,必须检查moveToFirst的返回值。