【发布时间】:2016-03-22 20:22:49
【问题描述】:
我正在运行一个后台任务,它会下载一个 JSON 文件,对其进行解析,然后将其添加到 SQLite 数据库的内容中。
我在运行时遇到了一些错误。
引起:android.database.CursorWindowAllocationException:光标 2048 kb 的窗口分配失败。 # 打开 Cursors=728 (# cursors 由这个 proc=728 打开)
E/CursorWindow: 无法分配 CursorWindow '/data/data/com.mycompany.inventory/databases/dbInventory.sql' 的大小 2097152 由于错误 -12。
JSON 中有大约 1500 个项目。
这是我的异步任务调用的方法:
public void addModelsToDB(JSONObject dict){
String insertQuery = "";
String deleteQuery = "DROP TABLE IF EXISTS 'tblModels'";
String createQuery = "CREATE TABLE 'tblModels' ('modelsID' INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT UNIQUE,'makeKey' INTEGER, 'modelName' TEXT, 'modelKey' INTEGER)";
Cursor cursor = dbLocals.executeRawQuery(deleteQuery, null);
cursor.moveToFirst();
cursor = dbLocals.executeRawQuery(createQuery, null);
cursor.moveToFirst();
try {
JSONArray dicRecordSet = dict.getJSONArray("Recordset");
JSONObject dicRecords = dicRecordSet.getJSONObject(0);
JSONArray arrRecords = dicRecords.getJSONArray("Record");
for (int i = 0; i < arrRecords.length(); i++) {
JSONObject record = arrRecords.getJSONObject(i);
insertQuery = "INSERT INTO 'tblModels' VALUES(" + null + ", "
+ record.getString("MODMAKlMakeKey") + ", '"
+ record.getString("MODvc50Name").replaceAll("'", "''") + "', "
+ record.getString("MODlModelKey")
+")";
cursor = dbLocals.executeRawQuery(insertQuery, null);
cursor.moveToFirst();
}
} catch (JSONException e) {
e.printStackTrace();
}
cursor.close();
}
我的数据库管理员返回一个游标。
public Cursor executeRawQuery(String query, String[] selectionArgs) {
Cursor cursor = databaseConn.rawQuery(query, selectionArgs);
return cursor;
}
我做错了什么?
【问题讨论】:
标签: android json sqlite cursor