gpt4 book ai didi

java - 游标、SQLite 和查询

转载 作者:行者123 更新时间:2023-12-01 12:30:26 25 4
gpt4 key购买 nike

我正在我的应用程序中实现一个 sqlitedatabase,并且在启动时不断收到 IllegalStateException。

09-21 22:48:26.749  15762-16277/nz.co.exium.panther E/AndroidRuntime﹕ FATAL EXCEPTION: SyncAdapterThread-2
java.lang.IllegalStateException: Couldn't read row 0, col 4 from CursorWindow. Make sure the Cursor is initialized correctly before accessing data from it.
at android.database.CursorWindow.nativeGetString(Native Method)
at android.database.CursorWindow.getString(CursorWindow.java:438)
at android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java:51)
at android.database.CursorWrapper.getString(CursorWrapper.java:114)
at nz.co.exium.panther.sync.PantherSyncAdapter.getRegistrationId(PantherSyncAdapter.java:269)
at nz.co.exium.panther.sync.PantherSyncAdapter.onPerformSync(PantherSyncAdapter.java:64)
at android.content.AbstractThreadedSyncAdapter$SyncThread.run(AbstractThreadedSyncAdapter.java:254)

我相信这是由于查询返回一个相当空的表(自动递增的 _ID 除外),然后当我尝试从游标获取字符串时引起的。

String registrationID = "";
Uri uri = CustomerEntry.CONTENT_URI;
Cursor cursor = mContext.getContentResolver().query(uri, CUSTOMER_PROJECTION, null, null, null);
if (cursor.moveToFirst()) {

registrationID = cursor.getString(INDEX_CUST_REGID);
}

SQL 创建表调用:

final String SQL_CREATE_CUSTOMER_TABLE = "CREATE TABLE " + CustomerEntry.TABLE_NAME + " (" +
CustomerEntry._ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
CustomerEntry.COLUMN_CUST_NAME + " TEXT NOT NULL, " +
CustomerEntry.COLUMN_CUST_EMAIL + " TEXT NOT NULL, " +
CustomerEntry.COLUMN_CUST_QRHASH + " TEXT NOT NULL," +
CustomerEntry.COLUMN_CUST_REGID + " TEXT NOT NULL)";

在本例中,INDEX_CUST_REGID 是与 String[] 投影中的位置(本例中为第三个位置)相关的最终静态 int。这是有道理的,这会抛出,但有没有一种方法或方式可以查询 SyncAdapter 的特定列(如 CUST_REGID),或者有一种方法可以在尝试cursor.getString() 之前检查请求的列是否已返回?

这也可能解决我在知道电子邮件、姓名、QRHASH 之前保存注册 ID 时遇到的另一个问题。不能只插入一列而其余列也有值(NOT NULL),即使它是毫无值(value)的数据并尽快被覆盖。

尝试存储 Reg ID 的方法。

private void storeRegistrationID(Context context, String regID) {
//DB insert regid
ContentValues cValues = new ContentValues();
cValues.put(CustomerEntry.COLUMN_CUST_NAME, "");
cValues.put(CustomerEntry.COLUMN_CUST_EMAIL, "");
cValues.put(CustomerEntry.COLUMN_CUST_QRHASH, "");
cValues.put(CustomerEntry.COLUMN_CUST_REGID, regID);
mContext.getContentResolver().insert(CustomerEntry.CONTENT_URI, cValues);
}

按要求投影:

 String[] CUSTOMER_PROJECTION = new String[]{
CustomerEntry._ID,
CustomerEntry.COLUMN_CUST_NAME,
CustomerEntry.COLUMN_CUST_EMAIL,
CustomerEntry.COLUMN_CUST_QRHASH,
CustomerEntry.COLUMN_CUST_REGID
};

最佳答案

您确定该表确实已创建吗? SQL_CREATE_CUSTOMER_TABLE 文本字符串末尾缺少一个分号 - 尽管我不确定是否必须运行 sql 来创建表。我建议在 Eclipse 模拟器上运行它,然后从 DDMS 角度来看,在某个地方复制数据库,您可以使用 Firefox 的 SQLite Manager 插件打开它 - 这将显示数据库表。

关于java - 游标、SQLite 和查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25958555/

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