gpt4 book ai didi

Android,SQLITE : Failed to read row 0, 来自 CursorWindow 的第 11 列,它有 1 行,11 列

转载 作者:搜寻专家 更新时间:2023-10-30 20:28:07 25 4
gpt4 key购买 nike

无法从具有 1 行 11 列的 CursorWindow 读取第 0 行第 11 列。

这是我想从数据库中读取数据时(登录时)遇到的错误。它还说“应用程序停止工作”。这是 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 字符串,然后我添加了一些其他字符串,这就发生了。请记住,在它正确读取应用程序中我的帐户信息中的数据之前,您不会说我的标签/ID 错误等...

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);

最佳答案

我看到一个问题,在您的 Android SQLite CREATE_LOGIN_TABLE 语句中,没有创建列 encrypted_pa​​ssword。那么您的表只有 11 列,这导致最大索引 10。没有索引为 11 的列,也没有名称为 encrypted_pa​​ssword 并调用 cursor.getString(11); 的列是强制关闭您的应用程序的错误原因。

所有读取数据库数据的问题都是硬编码索引,这些索引与创建列的顺序不匹配。当您在 onCreateDatabase() 方法上创建表时,列顺序是:

  1. 编号
  2. 名字
  3. 我的名字
  4. 电邮

等由于创建表语句中列名的顺序。

在您的 getUserDetails 方法中,您使用索引为 5 的 fname、索引为 6 的 lname 等。硬编码索引与创建表语句中列的顺序不匹配,您使用用户名和 ID值而不是 fname 和 lname。其他列也类似。

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

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

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

    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)));

关于Android,SQLITE : Failed to read row 0, 来自 CursorWindow 的第 11 列,它有 1 行,11 列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20808722/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com