gpt4 book ai didi

android - 仅在Android上查询时出现SQL逻辑错误

转载 作者:塔克拉玛干 更新时间:2023-11-03 07:09:03 25 4
gpt4 key购买 nike

我正在开发一个依赖 SQLite3 数据库作为数据存储库的 C++ Android NDK 项目。数据库有三个表;列数为 6、8 和 6;行数分别为 240、~1250 万和~400 万。

SQLite 驱动程序未经修改直接从 SQLite3 合并源代码 3.19.3 版编译到我的 native 库中。

我的问题是,在运行 native 库时,数据库查询导致“SQL logic error or missing database”。我知道数据库在我期望的位置,并且可以访问。如果我在桌面环境(而不是移动设备)中对同一个数据库执行相同的查询,我会看到预期的结果。此外,如果我在移动设备上使用包含存储库中数据子集(约 300 条记录)的数据库执行相同的查询,我会看到预期的结果。

有什么想法吗?

作为引用,这是我正在使用的查询:

WITH 
weather_distance AS
(SELECT latitude, longitude, temperature, humidity, time, (ABS((latitude - $lat)) + ABS((longitude - $lon))) AS distance FROM Weather WHERE time BETWEEN $start AND $end),
min_weather_distance AS
(SELECT latitude, longitude, temperature, humidity, time FROM weather_distance WHERE distance = (SELECT MIN(distance) FROM weather_distance)),
solar_distance AS
(SELECT latitude, longitude, ghi, (time - 1800) AS time, (ABS((latitude - $lat)) + ABS((longitude - $lon))) AS distance FROM SolarData WHERE time BETWEEN $start AND $end),
min_solar_distance AS
(SELECT latitude, longitude, ghi, time FROM solar_distance WHERE distance = (SELECT MIN(distance) FROM solar_distance))
SELECT s.time, s.ghi, w.temperature, w.humidity FROM min_weather_distance w INNER JOIN min_solar_distance s ON w.time = s.time ORDER BY s.time ASC

和我用来进行查询的代码 (C++):

const char* getEnvQuery =
"WITH "
"weather_distance AS "
"(SELECT latitude, longitude, temperature, humidity, time, (ABS((latitude - $lat)) + ABS((longitude - $lon))) AS distance FROM Weather WHERE time BETWEEN $start AND $end), "
"min_weather_distance AS "
"(SELECT latitude, longitude, temperature, humidity, time FROM weather_distance WHERE distance = (SELECT MIN(distance) FROM weather_distance)), "
"solar_distance AS "
"(SELECT latitude, longitude, ghi, (time - 1800) AS time, (ABS((latitude - $lat)) + ABS((longitude - $lon))) AS distance FROM SolarData WHERE time BETWEEN $start AND $end), "
"min_solar_distance AS (SELECT latitude, longitude, ghi, time FROM solar_distance WHERE distance = (SELECT MIN(distance) FROM solar_distance)) "
"SELECT s.time, s.ghi, w.temperature, w.humidity FROM min_weather_distance w INNER JOIN min_solar_distance s ON w.time = s.time ORDER BY s.time ASC;\0";

sqlite3_stmt* getEnvStmt;
prepareSqlStatement(getEnvQuery, &getEnvStmt, envdbhandle, "Error preparing SQL statement to retrieve environmental data. SQLite return code: ");
sqlite3_bind_double(getEnvStmt, 1, iter->latitude); //iter is defined above quoted code
sqlite3_bind_double(getEnvStmt, 2, iter->longitude);
sqlite3_bind_double(getEnvStmt, 3, iter->startTime);
sqlite3_bind_double(getEnvStmt, 4, iter->endTime);
__android_log_print(ANDROID_LOG_DEBUG, "SPP/getEnvironment", "Bound parameters: lat=%f, lon=%f, start=%ld, end=%ld", iter->latitude, iter->longitude, iter->startTime, iter->endTime);
int rc = sqlite3_step(getEnvStmt);
__android_log_print(ANDROID_LOG_DEBUG, "SPP/getEnvironment", "step(getEnvStmt) = %d", rc);
int errCode = sqlite3_extended_errcode(envdbhandle);
__android_log_print(ANDROID_LOG_DEBUG, "SPP/getEnvironment", "Most recent SQLITE error code: %s. Message: %s", sqlite3_errstr(errCode), sqlite3_errmsg(envdbhandle));
while(rc == SQLITE_ROW)
{
EnvironmentDatum envData;
int dbTime = sqlite3_column_int(getEnvStmt, 0);
envData.UnixTime = timeconvert::secondsOfYearToUNIXTime(dbTime, year);
__android_log_print(ANDROID_LOG_DEBUG, "SPP/getEnvironment", "EnvironmentDatum dbTime=%d, UnixTime=%f", dbTime, envData.UnixTime);
envData.GHI = sqlite3_column_double(getEnvStmt, 1);
envData.Temperature = sqlite3_column_double(getEnvStmt, 2);
envData.Humidity = sqlite3_column_double(getEnvStmt, 3);
envCollection.push_back(envData);
rc = sqlite3_step(getEnvStmt);
}
sqlite3_finalize(getEnvStmt);

重要调试信息:

sqlite3_stmt* verQueryStmt;
prepareSqlStatement("select sqlite_version();\0", &verQueryStmt, envdbhandle, "Error getting driver version. Error code:");
sqlite3_step(verQueryStmt);
std::string sqliteVersion = parseSqliteStringColumn(verQueryStmt, 0);
sqlite3_finalize(verQueryStmt);
__android_log_print(ANDROID_LOG_DEBUG, "SPP/buildScenario", "sqlite version=%s", sqliteVersion.c_str()); // outputs "sqlite version=3.19.3"
__android_log_print(ANDROID_LOG_DEBUG, "SPP/buildScenario", "env db readability=%s", (sqlite3_db_readonly(envdbhandle, "main") == 1 ? "READONLY" : (sqlite3_db_readonly(envdbhandle, "main") == 0 ? "READ/WRITE" : "NOT CONNECTED"))); // outputs "READ/WRITE"

根据请求,这里是 prepareStatement:

static int prepareSqlStatement(const char* query, sqlite3_stmt** statement, sqlite3* db, const char* failMsg)
{
int returnCode = sqlite3_prepare(db, query, -1, statement, NULL);
if(returnCode != SQLITE_OK || statement == NULL)
{
int errCode = sqlite3_extended_errcode(dbhandle);
std::cout << "Most recent SQLITE error code: " << sqlite3_errstr(errCode) << ". Message: " << sqlite3_errmsg(dbhandle) << std::endl;
reportError(failMsg, -1 * returnCode);
}
return returnCode;
}

在预期中,这里是 reportError:

static void reportError(const char* message, int errorCode)
{
std::stringstream ss;
ss << message << errorCode;
throw std::runtime_error(ss.str());
}

最佳答案

当查询的中间结果变得太大时,数据库必须将一些数据换出到一个临时文件中。

6410 是 SQLITE_IOERR_GETTEMPPATH,这意味着没有 temporary file storage locations是可访问的。

Android 没有任何默认的 Unix 路径。内置数据库框架使用 SQLITE_TEMP_STORE 编译其 SQLite 库拷贝。 = 3. 如果你想得到实际的临时文件,你应该把它们放在 Context.getCacheDir() 返回的任何目录中。 ;这需要设置 SQLITE_TMPDIR 环境变量。

关于android - 仅在Android上查询时出现SQL逻辑错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44766917/

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