Android,SQLITE:无法从CursorWindow读取第0行第11列,其中包含1行,11列

时间:2013-12-27 22:34:08

标签: android sql database json sqlite

无法从包含1行,11列的CursorWindow读取第0行第11行。

当我想从我的数据库中读取数据时(当我登录时),这是我得到的错误。它还说“app停止工作”。 这是JSON响应:

E/JSON﹕ {"tag":"login","success":1,"error":0,"user":{"fname":"somefirstname","lname":"somelastname","email":"some@email.com","uname":"someuser","address":"someaddress","tel":"somephone","dob":"15\/01\/1997","ptc":"somepostco","id":"3","created_at":"2013-12-28 01:20:33"}}
12-27 17:23:14.602     

前一个错误的另一个错误是“无法从CursorWindow读取第0行第11行”。它是created_at字符串,当我删除它时,它会在没有崩溃的情况下登录,但它会读取错误的数据......

这是我的DatabaseHandler.java

public class DatabaseHandler extends SQLiteOpenHelper {

    // All Static variables
    // Database Version
    private static final int DATABASE_VERSION = 2;

    // Database Name
    private static final String DATABASE_NAME = "login";

    // Login table name
    private static final String TABLE_LOGIN = "member";

    // Login Table Columns names
    private static final String KEY_ID = "id";
    private static final String KEY_FIRSTNAME = "fname";
    private static final String KEY_LASTNAME = "lname";
    private static final String KEY_EMAIL = "email";
    private static final String KEY_USERNAME = "uname";
    private static final String KEY_UID = "uid";
    private static final String KEY_ADDRESS = "address";
    private static final String KEY_TELEPHONE = "tel";
    private static final String KEY_DATEOFBIRTH = "dob";
    private static final String KEY_POSTCODE = "ptc";
    private static final String KEY_CREATED_AT = "created_at";

    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_FIRSTNAME + " TEXT,"
                + KEY_LASTNAME + " TEXT,"
                + KEY_EMAIL + " TEXT UNIQUE,"
                + KEY_USERNAME + " TEXT,"
                + KEY_UID + " TEXT,"
                + KEY_ADDRESS + " TEXT,"
                + KEY_TELEPHONE + " TEXT,"
                + KEY_DATEOFBIRTH + " TEXT,"
                + KEY_POSTCODE + " TEXT,"
                + KEY_CREATED_AT + " 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 fname, String lname, String email, String uname, String uid, String address, String tel, String ptc, String dob, String created_at) {
        SQLiteDatabase db = this.getWritableDatabase();

        ContentValues values = new ContentValues();
        values.put(KEY_FIRSTNAME, fname); // FirstName
        values.put(KEY_LASTNAME, lname); // LastName
        values.put(KEY_EMAIL, email); // Email
        values.put(KEY_USERNAME, uname); // UserName
        values.put(KEY_UID, uid); // Email
        values.put(KEY_ADDRESS, address); // Address
        values.put(KEY_TELEPHONE, tel); // Telephone
        values.put(KEY_DATEOFBIRTH, dob); // Date of Birth
        values.put(KEY_POSTCODE, ptc); // ZIP code
        values.put(KEY_CREATED_AT, created_at); // Created At

        // Inserting Row
        db.insert(TABLE_LOGIN, null, values);
        db.close(); // Closing database connection
    }


    /**
     * 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("id", cursor.getString(1)); // id - uid
            user.put("username", cursor.getString(2)); // uname - username
            user.put("email", cursor.getString(4));
            user.put("fname", cursor.getString(5));
            user.put("lname", cursor.getString(6));
            user.put("dob", cursor.getString(7));
            user.put("tel", cursor.getString(8));
            user.put("address", cursor.getString(9));
            user.put("ptc", cursor.getString(10));
            //user.put("created_at", cursor.getString(11));
        }
        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();
    }

}

这是我的.sql数据库

CREATE TABLE `login`.`member` (   
`id` INT NOT NULL AUTO_INCREMENT PRIMARY KEY, 
`username` VARCHAR(30) NOT NULL,  
`encrypted_password` VARCHAR(80) NOT NULL,    
`email` VARCHAR(50) NOT NULL, 
`fname` VARCHAR(30) NOT NULL, 
`lname` VARCHAR(30) NOT NULL,  
`dob` VARCHAR(10) NOT NULL, 
`tel` VARCHAR(15) NOT NULL,  
`address` VARCHAR(50) NOT NULL,
`ptc` VARCHAR(10) NOT NULL,  
`salt` VARCHAR(10) NOT NULL,
`created_at` datetime DEFAULT NULL   
) ENGINE = InnoDB;

这一切都发生在我更新我的数据库时,之前它只有用户名,fname,lname,id和created_at字符串,然后我添加了一些其他的,这就发生了。请记住,在它在应用程序中的帐户信息中正确读取数据之前,所以你不要说我的标签/ ids是错误的等...

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("id", cursor.getString(1));
            user.put("username", cursor.getString(2));
            user.put("email", cursor.getString(3));
            user.put("fname", cursor.getString(4));
            user.put("lname", cursor.getString(5));
            user.put("dob", cursor.getString(6));
            user.put("tel", cursor.getString(7));
            user.put("address", cursor.getString(8));
            user.put("ptc", cursor.getString(9));
            user.put("created_at", cursor.getString(10));
        }
        cursor.close();
        db.close();
        // return user
        return user;
    }

这是我的登录API

$response["success"] = 1;
$response["user"]["fname"] = $user["fname"];
$response["user"]["lname"] = $user["lname"];
$response["user"]["email"] = $user["email"];
$response["user"]["uname"] = $user["username"];
$response["user"]["address"] = $user["address"];
$response["user"]["tel"] = $user["tel"];
$response["user"]["dob"] = $user["dob"];
$response["user"]["ptc"] = $user["ptc"];
$response["user"]["id"] = $user["id"];
$response["user"]["created_at"] = $user["created_at"];
echo json_encode($response);

1 个答案:

答案 0 :(得分:2)

我发现您的Android SQLite CREATE_LOGIN_TABLE语句中没有创建列encrypted_password的问题。然后,您的表只有11列,这导致最大索引10.没有索引为11的列,没有名称为encrypted_password的列,并且调用cursor.getString(11);是强制关闭应用程序的错误原因。< / p>

所有数据库数据读取的问题都是与已创建列的顺序不匹配的硬编码索引。onCreateDatabase()方法上创建表时,列顺序为:< / p>

  1. ID
  2. FNAME
  3. L-NAME
  4. 电子邮件
  5. 等。由于create table语句中列名的顺序。

    在你的getUserDetails方法中,你正在使用索引为5的fname,索引为6的lname等。硬编码索引与create table语句中的列顺序不匹配,而是使用username和id值而不是fname和lname。 Simmilar它适用于其他专栏。

    为了正确使用列索引,您应该在getColumnIndex(String columnName)上使用Cursor方法而不是硬编码索引。例如:

    int fnameIndex = cursor.getColumnIndex(KEY_FIRSTNAME);
    String fname = cursor.getString(fnameIndex);
    

    以下是您可以在getUserDetails()方法中使用的代码段:

        user.put("id", cursor.getString(cursor.getColumnIndex(KEY_ID))); // id - uid
        user.put("username", cursor.getString(cursor.getColumnIndex(KEY_USERNAME))); // uname - username
        user.put("email", cursor.getString(cursor.getColumnIndex(KEY_EMAIL)));
        user.put("fname", cursor.getString(cursor.getColumnIndex(KEY_FIRSTNAME)));
        user.put("lname", cursor.getString(cursor.getColumnIndex(KEY_LASTNAME)));
        user.put("dob", cursor.getString(cursor.getColumnIndex(KEY_DATEOFBIRTH)));
        user.put("tel", cursor.getString(cursor.getColumnIndex(KEY_TELEPHONE)));
        user.put("address", cursor.getString(cursor.getColumnIndex(KEY_ADDRESS)));
        user.put("ptc", cursor.getString(cursor.getColumnIndex(KEY_CREATED_AT)));