gpt4 book ai didi

java - 在同一数据库上运行的不同线程中的多个游标

转载 作者:太空宇宙 更新时间:2023-11-04 13:02:59 24 4
gpt4 key购买 nike

我在 asynctask 类 doInBackground() 中初始化数据库实例。数据库实例及其方法位于一个单独的类中(我将展示的类)从 asynctask 中,我在 Azure 客户端的类中启动另一个线程,该线程最终查询数据库类以填充我的 azure db 客户端。回到我的异步任务,然后我再次访问数据库(与第一个查询同时)以查询数据库以获取单独的信息。似乎两个游标同时从不同的线程查询给我带来了问题。

下面是 db 类中的方法,它们使用第一个游标将内容发送到 Azure。这些方法是从 Azure 类中运行的单独线程调用的。 Azure 类在 asynctask 类中调用。正如您将在 logcat 中看到的,这工作得很好。

    public ArrayList<WeatherEvent> GetUnsychedWeatherEvent() {
String query = COLUMN_SYNCHED + " = 0 ";
Cursor cursor = db.query(TABLE_WEATHER, null, query, null, null, null, null);

ArrayList<WeatherEvent> unsychedWeather = new ArrayList<>();
while (cursor.moveToNext()) {
unsychedWeather.add( GetWeatherEvent(cursor) );
}
cursor.close();

return unsychedWeather;

}

//get weather event from db for azure
//TODO: finish populating this
public WeatherEvent GetWeatherEvent(Cursor cursor) {
WeatherEvent wEvent = new WeatherEvent();
wEvent.Latitude = cursor.getDouble(cursor.getColumnIndex(COLUMN_LATITUDE));
wEvent.Longitude = cursor.getDouble(cursor.getColumnIndex(COLUMN_LONGITUDE));
wEvent.CurrTemp = cursor.getString(cursor.getColumnIndex(COLUMN_CURRENT_TEMPERATURES));
wEvent.CurrDesc = cursor.getString(cursor.getColumnIndex(COLUMN_CURRENT_WEATHER_DESCRIPTION));
System.out.println("From DB, current temp: " + wEvent.CurrTemp);
try {
wEvent.Time = timeStampFormat.parseDateTime(cursor.getString(cursor.getColumnIndex(COLUMN_TIME_STAMP)));
} catch (Exception e) {
Log.d("GetEvent()", "Error parsing date " + e.toString());
}

return wEvent;
}

接下来的这些方法来自同一个数据库类。这里的“每日光标”给我带来了麻烦。这些方法在 asynctask 类中调用。

  public ArrayList<WeatherEvent> DailyWeatherEvents(ArrayList<WeatherEvent> dailyWeatherEvents) {
Log.d("DB Access", "got to dailyweatherevents");

//todo: more specific time query
Cursor dailycursor = db.rawQuery("SELECT avg(" + COLUMN_CURRENT_TEMPERATURES + "), " + COLUMN_TIME_STAMP + ", " +
"CASE WHEN strftime('%M', " + COLUMN_TIME_STAMP + ") < '30' " +
"THEN strftime('%H', " + COLUMN_TIME_STAMP + ") " +
"ELSE strftime('%H', " + COLUMN_TIME_STAMP + ", '+1 hours') END " +
"FROM " + TABLE_WEATHER + " "+
"GROUP BY strftime('%H', " + COLUMN_TIME_STAMP + ", '+30 minutes')", null);

//ArrayList<WeatherEvent> dailyWeatherEvents = new ArrayList<>();
try {
if (dailycursor.moveToFirst()) {
while (dailycursor.moveToNext()) {
dailyWeatherEvents.add(GetDailyWeatherEvent(dailycursor));

}
dailycursor.close();
System.out.println("Size of dailyinfo " + dailyWeatherEvents.size());
}
} catch (Exception e) {
e.printStackTrace();
}
return dailyWeatherEvents;
}

public WeatherEvent GetDailyWeatherEvent(Cursor dailycursor) {
WeatherEvent wEvent = new WeatherEvent();
// wEvent.Latitude = dailycursor.getDouble(dailycursor.getColumnIndex(COLUMN_LATITUDE));
// wEvent.Longitude = dailycursor.getDouble(dailycursor.getColumnIndex(COLUMN_LONGITUDE));
wEvent.CurrTemp = dailycursor.getString(dailycursor.getColumnIndex(COLUMN_CURRENT_TEMPERATURES));
System.out.println(wEvent.CurrTemp + " mariah this comes from cursor");
wEvent.CurrDesc = dailycursor.getString(dailycursor.getColumnIndex(COLUMN_CURRENT_WEATHER_DESCRIPTION));
try {
wEvent.Time = timeStampFormat.parseDateTime(dailycursor.getString(dailycursor.getColumnIndex(COLUMN_TIME_STAMP)));
} catch (Exception e) {
Log.d("GetEvent()", "Error parsing date " + e.toString());
}
wEvent.CurrIcon = dailycursor.getString(dailycursor.getColumnIndex(COLUMN_CURRENT_ICONS));
wEvent.SevereWeatherPresent = dailycursor.getInt(dailycursor.getColumnIndex(COLUMN_SEVERE_WEATHER_PRESENT))>0;

return wEvent;

}

asynctask 类中的行启动了这一切。 WeatherDBAccess 是数据库类。 WeatherAzureAccess 是 azure 的类:

  WeatherDBAccess._context = this.context;
WeatherDBAccess.Instance().AddWeatherEvent(wEvent);
//starts azure
WeatherAzureAccess.context = this.context;
WeatherAzureAccess.Instance();


//prep dailyinfo arraylist

dailyinfo = WeatherDBAccess.Instance().DailyWeatherEvents(dailyinfo);

这是我的 logcat,“From DB, current temp: XX”行是从 azure 的方法打印的:

    01-12 15:15:43.869 29979-30189/pdgt.cat.com.noaahso I/SQLiteAssetHelper: successfully opened database HSO.sqlite
01-12 15:15:43.869 29979-30189/pdgt.cat.com.noaahso D/WeatherDBAccess: Instance Created /data/data/pdgt.cat.com.noaahso/databases/HSO.sqlite
01-12 15:15:43.869 29979-30189/pdgt.cat.com.noaahso I/System.out: Current Temp: 54
01-12 15:15:43.899 29979-30189/pdgt.cat.com.noaahso I/System.out: Weather Event Added
01-12 15:15:43.919 29979-30189/pdgt.cat.com.noaahso D/DB Access: got to dailyweatherevents
01-12 15:15:43.919 29979-30189/pdgt.cat.com.noaahso E/CursorWindow: Failed to read row 1, column -1 from a CursorWindow which has 3 rows, 3 columns.
01-12 15:15:43.919 29979-30189/pdgt.cat.com.noaahso W/System.err: java.lang.IllegalStateException: Couldn't read row 1, col -1 from CursorWindow. Make sure the Cursor is initialized correctly before accessing data from it.
01-12 15:15:43.919 29979-30298/pdgt.cat.com.noaahso D/AzureAccess: WeatherSync thread STARTED!
01-12 15:15:43.919 29979-30189/pdgt.cat.com.noaahso W/System.err: at android.database.CursorWindow.nativeGetString(Native Method)
01-12 15:15:43.919 29979-30189/pdgt.cat.com.noaahso W/System.err: at android.database.CursorWindow.getString(CursorWindow.java:451)
01-12 15:15:43.919 29979-30189/pdgt.cat.com.noaahso W/System.err: at android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java:51)
01-12 15:15:43.919 29979-30189/pdgt.cat.com.noaahso W/System.err: at pdgt.cat.com.noaahso.WeatherDBAccess.GetDailyWeatherEvent(WeatherDBAccess.java:235)
01-12 15:15:43.919 29979-30189/pdgt.cat.com.noaahso W/System.err: at pdgt.cat.com.noaahso.WeatherDBAccess.DailyWeatherEvents(WeatherDBAccess.java:219)
01-12 15:15:43.919 29979-30189/pdgt.cat.com.noaahso W/System.err: at pdgt.cat.com.noaahso.WeatherTask.doInBackground(WeatherTask.java:228)
01-12 15:15:43.919 29979-30189/pdgt.cat.com.noaahso W/System.err: at pdgt.cat.com.noaahso.WeatherTask.doInBackground(WeatherTask.java:47)
01-12 15:15:43.919 29979-30189/pdgt.cat.com.noaahso W/System.err: at android.os.AsyncTask$2.call(AsyncTask.java:288)
01-12 15:15:43.919 29979-30189/pdgt.cat.com.noaahso W/System.err: at java.util.concurrent.FutureTask.run(FutureTask.java:237)
01-12 15:15:43.919 29979-30298/pdgt.cat.com.noaahso I/System.out: From DB, current temp: 49
01-12 15:15:43.919 29979-30189/pdgt.cat.com.noaahso W/System.err: at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
01-12 15:15:43.919 29979-30189/pdgt.cat.com.noaahso W/System.err: at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
01-12 15:15:43.919 29979-30189/pdgt.cat.com.noaahso W/System.err: at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
01-12 15:15:43.919 29979-30189/pdgt.cat.com.noaahso W/System.err: at java.lang.Thread.run(Thread.java:818)
01-12 15:15:43.919 29979-30298/pdgt.cat.com.noaahso I/System.out: From DB, current temp: 49
01-12 15:15:43.919 29979-30298/pdgt.cat.com.noaahso I/System.out: From DB, current temp: 49
01-12 15:15:43.919 29979-30298/pdgt.cat.com.noaahso I/System.out: From DB, current temp: 49
01-12 15:15:43.919 29979-30298/pdgt.cat.com.noaahso I/System.out: From DB, current temp: 50
01-12 15:15:43.919 29979-30298/pdgt.cat.com.noaahso I/System.out: From DB, current temp: 50
01-12 15:15:43.919 29979-30298/pdgt.cat.com.noaahso I/System.out: From DB, current temp: 50
01-12 15:15:43.919 29979-30298/pdgt.cat.com.noaahso I/System.out: From DB, current temp: 51
01-12 15:15:43.919 29979-30298/pdgt.cat.com.noaahso I/System.out: From DB, current temp: 51
01-12 15:15:43.919 29979-29979/pdgt.cat.com.noaahso D/AsyncTask: got to onpostExecute
01-12 15:15:43.919 29979-30298/pdgt.cat.com.noaahso I/System.out: From DB, current temp: 51
01-12 15:15:43.919 29979-30298/pdgt.cat.com.noaahso I/System.out: From DB, current temp: 51
01-12 15:15:43.919 29979-30298/pdgt.cat.com.noaahso I/System.out: From DB, current temp: 51
01-12 15:15:43.919 29979-30298/pdgt.cat.com.noaahso I/System.out: From DB, current temp: 51
01-12 15:15:43.919 29979-30298/pdgt.cat.com.noaahso I/System.out: From DB, current temp: 51
01-12 15:15:43.919 29979-30298/pdgt.cat.com.noaahso I/System.out: From DB, current temp: 51

我是否需要在第一个游标上使用 FutureCallback 才能知道何时启动第二个游标?或者我是否需要将第一个游标设置为全局并等到第一个查询完成后再重置一个游标以执行第二个查询?在寻找解决方案时,我了解到启动第二个光标的成本很高。我的问题是,第二个游标很少使用(可能仅在 onCreate() 上),而第一个游标几乎总是会使用,因为它经常用于向 Azure 发送内容。另外仅供引用,我关闭了我的数据库 onDestroy()。

最佳答案

首先,您的错误是:

java.lang.IllegalStateException: Couldn't read row 1, col -1 from CursorWindow. Make sure the Cursor is initialized correctly before accessing data from it.

这意味着您有结果(第 1 行),但您正在尝试读取无效的列索引(col -1)。

问题区域可能在这里:

wEvent.CurrTemp = dailycursor.getString(dailycursor.getColumnIndex(COLUMN_CURRENT_TEMPERATURES));

您从 getColumnIndex 得到 -1 结果,因为您的 SQL 查询中不存在 COLUMN_CURRENT_TEMPERATURES

Cursor dailycursor = db.rawQuery("SELECT avg(" + COLUMN_CURRENT_TEMPERATURES + "), " + COLUMN_TIME_STAMP + ", " +
"CASE WHEN strftime('%M', " + COLUMN_TIME_STAMP + ") < '30' " +
"THEN strftime('%H', " + COLUMN_TIME_STAMP + ") " +
"ELSE strftime('%H', " + COLUMN_TIME_STAMP + ", '+1 hours') END " +
"FROM " + TABLE_WEATHER + " "+
"GROUP BY strftime('%H', " + COLUMN_TIME_STAMP + ", '+30 minutes')", null);

您需要在 SELECT 语句中包含您想要读取的所有列:

Cursor dailycursor = db.rawQuery("SELECT avg(" + COLUMN_CURRENT_TEMPERATURES + "), " + COLUMN_CURRENT_TEMPERATURES + ", " + COLUMN_CURRENT_WEATHER_DESCRIPTION + ", " + COLUMN_CURRENT_ICONS + ", " + COLUMN_SEVERE_WEATHER_PRESENT + ", " + COLUMN_TIME_STAMP + ", " +
"CASE WHEN strftime('%M', " + COLUMN_TIME_STAMP + ") < '30' " +
"THEN strftime('%H', " + COLUMN_TIME_STAMP + ") " +
"ELSE strftime('%H', " + COLUMN_TIME_STAMP + ", '+1 hours') END " +
"FROM " + TABLE_WEATHER + " "+
"GROUP BY strftime('%H', " + COLUMN_TIME_STAMP + ", '+30 minutes')", null);

关于java - 在同一数据库上运行的不同线程中的多个游标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34769916/

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