2021SC@SDUSC
上篇讲了文件操作和查找系统通用协调时间,最后一部分讲sqlite操作系统的接口。
除了几个数据结构和#define,SQLite 3.0的API包括了83个独立的函数。(一个完整的API参考作为一个独立的文档提供。)幸运的是,接口不是与它所显示的大小一般复杂。简单的程序仍可以通过仅仅三个函数工作:sqlite3_open()、sqlite3_exec()和sqlite3_close()。更多的数据库引擎运行控制可以用sqlite3_prepare()来编译一个SQLite语句成字节代码并通过sqlite3_step()来执行它。一个用sqlite3_column_开头的命令序列可以用来提取关于查询结果的信息。许多接口函数是以UTF-8和UTF-16的形式成对出现的。并且有一个用于实现用户定义SQL函数和用户定义的text比较。
先介绍几个相关函数
1.sqlite3_open()
这个函数打开一个sqlite数据库文件的连接并且返回一个数据库连接对象。
2.sqlite3_prepare()
这个函数将sql文本转换成一个准备语句对象,同时返回这个对象的指针。
3.sqlite3_step()
这个过程用于执行由前面sqlite3_prepare创建的准备语句。
4. sqlite3_column()
这个过程从sqlite3_step()执行一个准备语句得到的结果集的当前行中返回一个列。
5.sqlite3_finalize(sqlite3_stmt *pStmt)
这个过程销毁前面由sqlite3_prepare创建的准备语句
6. sqlite3_close
这个过程关闭前面使用sqlite3_open打开的数据库连接
初始化和去初始化操作系统接口。
int sqlite3_os_init(void){
static sqlite3_vfs winVfs = {
3, /* 版本号 */
sizeof(winFile), /* szOsFile */
SQLITE_WIN32_MAX_PATH_BYTES, /*最大路径名*/
0, /* pNext */
"win32", /* zName */
&winAppData,
winOpen,
winDelete,
winAccess,
winFullPathname,
winDlOpen,
winDlError,
winDlSym,
winDlClose,
winRandomness,
winSleep,
winCurrentTime,
winGetLastError,
winCurrentTimeInt64,
winSetSystemCall,
winGetSystemCall,
winNextSystemCall,
};
#if defined(SQLITE_WIN32_HAS_WIDE)
static sqlite3_vfs winLongPathVfs = {
3, /*版本号 */
sizeof(winFile), /* szOsFile */
SQLITE_WINNT_MAX_PATH_BYTES, /* 最大路径名 */
0, /* pNext */
"win32-longpath", /* zName */
&winAppData,
winOpen,
winDelete,
winAccess,
winFullPathname,
winDlOpen,
winDlError,
winDlSym,
winDlClose,
winRandomness,
winSleep,
winCurrentTime,
winGetLastError,
winCurrentTimeInt64,
winSetSystemCall,
winGetSystemCall,
winNextSystemCall,
};
#endif
static sqlite3_vfs winNolockVfs = {
3, /* 版本号 */
sizeof(winFile), /* szOsFile */
SQLITE_WIN32_MAX_PATH_BYTES, /* mxPathname */
0, /* pNext */
"win32-none", /* zName */
&winNolockAppData,
winOpen,
winDelete,
winAccess,
winFullPathname,
winDlOpen,
winDlError,
winDlSym,
winDlClose,
winRandomness,
winSleep,
winCurrentTime,
winGetLastError,
winCurrentTimeInt64,
winSetSystemCall,
winGetSystemCall,
winNextSystemCall,
};
#endif
重复检查aSyscall[]数组的构造是否正确。
assert( ArraySize(aSyscall)==80 );
获取内存映射分配粒度
memset(&winSysInfo, 0, sizeof(SYSTEM_INFO));
#if SQLITE_OS_WINRT
osGetNativeSystemInfo(&winSysInfo);
#else
osGetSystemInfo(&winSysInfo);
#endif
assert( winSysInfo.dwAllocationGranularity>0 );
assert( winSysInfo.dwPageSize>0 );
sqlite3_vfs_register(&winVfs, 1);
#if defined(SQLITE_WIN32_HAS_WIDE)
sqlite3_vfs_register(&winLongPathVfs, 0);
#endif
sqlite3_vfs_register(&winNolockVfs, 0);
#if defined(SQLITE_WIN32_HAS_WIDE)
sqlite3_vfs_register(&winLongPathNolockVfs, 0);
#endif
#ifndef SQLITE_OMIT_WAL
winBigLock = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_VFS1);
#endif
return SQLITE_OK;
}
int sqlite3_os_end(void){
#if SQLITE_OS_WINRT
if( sleepObj!=NULL ){
osCloseHandle(sleepObj);
sleepObj = NULL;
}
#endif
#ifndef SQLITE_OMIT_WAL
winBigLock = 0;
#endif
return SQLITE_OK;
}
#endif /* SQLITE_OS_WIN */