我正在尝试将SQLite配置为在嵌入式系统(ARM®Cortex®-M7)上运行。我已经从SQLite网站下载了合并程序,将其导入到项目中,并添加了以下符号: SQLITE_THREADSAFE=0、SQLITE_OS_OTHER=1、SQLITE_OMIT_WAL=1以允许其编译。
然后我下载了test_onefile.c (可在这里获得:http://www.sqlite.org/vfs.html),它应该允许SQLite直接在嵌入式媒体上操作,而不使用中间文件系统,并将其导入到项目中(我还确保提供了一个sqlite3_os_init()函数来注册虚拟文件系统)。
SQLITE_API int sqlite3_os_init(void)
{
extern int fs_register(void);
return fs_register();
}在一个单独的文件中,fs_register()如下所示:
/*
** This procedure registers the fs vfs with SQLite. If the argument is
** true, the fs vfs becomes the new default vfs. It is the only publicly
** available function in this file.
*/
int fs_register(void)
{
if (fs_vfs.pParent) return SQLITE_OK;
fs_vfs.pParent = sqlite3_vfs_find(0);
fs_vfs.base.mxPathname = fs_vfs.pParent->mxPathname;
fs_vfs.base.szOsFile = MAX(sizeof(tmp_file), sizeof(fs_file));
return sqlite3_vfs_register(&fs_vfs.base, 0);
}我可以使用sqlite3_register_vfs()、sqlite3_open()和sqlite3_prepare()成功地注册文件系统、打开数据库和准备SQL语句。打开数据库时,我一定要使用": memory :“字符串在内存中创建数据库,而不是将其作为文件。
static void TestSQLiteOpenDB(void)
{
/******** setup ********************************/
sqlite3 *db;
int rc;
/******** run element/component under test *****/
rc = sqlite3_open(":memory:", &db);
sqlite3_close(db);
/******** assertion test ***********************/
TEST_ASSERT_EQUAL_INT(SQLITE_OK, rc);
}我的问题是在尝试运行sqlite3_exec()时。当调用test_onefile.c中的以下代码段时,程序崩溃:
/*
** Populate the buffer pointed to by zBufOut with nByte bytes of
** random data.
*/
static int fsRandomness(sqlite3_vfs *pVfs, int nByte, char *zBufOut)
{
sqlite3_vfs *pParent = ((fs_vfs_t *)pVfs)->pParent;
return pParent->xRandomness(pParent, nByte, zBufOut);
}如果我将此函数更改为简单地返回0,它似乎可以工作。然后我可以创建表格,向表格中插入数据等。
我的问题是:在SQLite中是否需要用随机数据填充这个缓冲区,或者这个变通方法可以吗?我不想给自己制造更多的麻烦,但把它作为失败点来追踪是一场噩梦,我不能完全理解正在发生的事情。
发布于 2018-05-09 03:56:23
SQLite将这种随机性用于临时文件,以强制更改日志/WAL文件,生成唯一的列名,并在自动递增的ID溢出时使用。
如果返回值是常量,其中一些可能会进入无限循环,因此您应该尝试获得实际的随机性。(它不需要是加密安全的。)
https://stackoverflow.com/questions/50240055
复制相似问题