转载自:http://blog.csdn.net/lijinqi1987/article/details/51672711
由于sqlite对多进程 *** 作支持效果不太理想,在项目中,为了避免频繁读写 文件数据库带来的性能损耗,我们可以采用 *** 作sqlite内存数据库,并将内存数据库定时同步到文件数据库中的方法。
实现思路如下:
1、创建文件数据库;
2、创建内存数据库(文件数据库、内存数据库的内幕表结构需要一致);
3、在内存数据库中attach文件数据库,这样可以保证文件数据库中的内容在内存数据库中可见;
4、对于insert、select *** 作,在内存数据库中完成,对于delete、update *** 作,需要同时访问内存、文件数据库;
5、定时将内存数据库中的内容flush到文件数据库。
通过sqlite的cAPI实现代码如下:
const char* file_database_path = "/home/tom/test/database/filedb"; //文件数据库存放路径const char* sql_create_data = "CREATE table testinfo (ID TEXT PRIMARY KEY,message TEXT,offset INTEGER,timestamp INTEGER);";const char* sql_insert_data = "INSERT OR REPLACE INTO MAIN.testinfo VALUES('%s','%s',%d,%d);";const char* sql_delete_data = "DELETE FROM MAIN.testinfo WHERE ID = '%s'; DELETE FROM filedb.testinfo WHERE ID = '%s';"; //删除数据库,需同时删除内存、文件数据库中的内容const char* sql_update_data = "UPDATE MAIN.testinfo SET message = '%s',offset = %d,timestamp = %d where ID = '%s'; UPDATE filedb.testinfo SET message = '%s',timestamp = %d where ID = '%s';";//更新数据库,需同时更新内存、文件数据库中的内容const char* sql_search_data = "SELECT * FROM MAIN.testinfo WHERE timestamp BETWEEN %d AND %d union SELECT * FROM testdb.testinfo WHERE timestamp BETWEEN %d AND %d;"; //查找数据库,将内存、文件数据库中查找出的内容合并const char* sql_transfer_data = "INSERT OR REPLACE INTO filedb.testinfo SELECT * FROM testinfo;"; //将内存数据库中的信息同步到文件数据库中const char* sql_delete_memory_table = "DELETE FROM testinfo;"; //内存数据库中的内容同步结束后,清空int InsertRecord(DATA_TYPE type,const char* ID,const char* message,int offset,int timestamp){ int rc = 0; char* errMsg = NulL; char sqlcmd[512] = {0}; time_t insertTimestamp = 0; snprintf(sqlcmd,sizeof(sqlcmd),sql_insert_data,ID,message,offset,timestamp); rc = sqlite3_exec(memdb,sqlcmd,NulL,&errMsg); if (sqlITE_OK != rc) { fprintf(stderr,"cat't add record to memory database %s,sqlcmd=%s,err:%s\n",map_data_table[type].data_table_name,errMsg); return -1; } return 0;}int UpdateRecord(DATA_TYPE type,int timestamp){ int rc = 0; char* errMsg = NulL; char sqlCmd[512] = {0}; snprintf(sqlCmd,sizeof(sqlCmd),sql_update_data,timestamp,ID); rc = sqlite3_exec(memdb,sqlCmd,"cat't update record %s:%s\n",errMsg); return -1; } return 0;}int DeleteRecord(DATA_TYPE type,const char* ID){ int rc = 0; char* errMsg = NulL; char sqlcmd[512] = {0}; snprintf(sqlcmd,sql_delete_data,"cat't delete record %s:%s\n",errMsg); return -1; } return 0;}int queryMessage(DATA_TYPE type,int startTime,int endTime){ int rc = 0; char *errMsg = NulL; sqlite3 *filedb = NulL; char** pRecord = NulL; int row = 0; int column = 0; char sqlcmd[512] = {0}; if (type > VEP_NELEMS(map_data_table) || type < 0) { return -1; } rc = sqlite3_open(file_database_path,&filedb); if (sqlITE_OK != rc) { fprintf(stderr,"cat't open database:%s\n",sqlite3_errmsg(filedb)); sqlite3_close(filedb); return -1; } snprintf(sqlcmd,sql_search_data,startTime,endTime,endTime); rc = sqlite3_get_table(filedb,&pRecord,&row,&column,"cat't get table from%s:%s\n",errMsg); return -1; } int i; printf("row = %d,column = %d\n",row,column); for(i = 0; i < 2*column; i++) { printf("%s ",pRecord[i]); } printf("\n"); return 0;}//定时调用此函数将内存数据中的内容同步到文件数据库int Flush(){ int i = 0; int rc = 0; char* errMsg = NulL; char sqlcmd[512] = {0}; snprintf(sqlcmd,sql_transfer_data); rc = sqlite3_exec(memdb,&errMsg); if (sqlITE_OK != rc) { fprintf(stderr,"cat't transfer memory database %s to file databasede:%s\n",map_data_table[i].data_table_name,sqlite3_errmsg(memdb)); sqlite3_close(memdb); return -1; } snprintf(sqlcmd,sql_delete_memory_table); rc = sqlite3_exec(memdb,&errMsg); return 0;}//创建文件数据库int CreateDbOnfile(){ sqlite3 *db = NulL; int rc = 0; char* errMsg = NulL; char sqlcmd[512] = {0}; int i = 0; rc = sqlite3_open(file_database_path,&db); if (sqlITE_OK != rc) { fprintf(stderr,sqlite3_errmsg(db)); sqlite3_close(db); return -1; } snprintf(sqlcmd,sql_create_data); rc = sqlite3_exec(db,"cat't create file database testinfo:%s\n",errMsg); sqlite3_close(db); return -1; } sqlite3_close(db); return 0;}//创建内存数据库int CreateDbOnMemery(){ int rc = 0; char* errMsg = NulL; char sqlcmd[512] = {0}; int i = 0; rc = sqlite3_open(":memory:",&memdb); if (sqlITE_OK != rc) { fprintf(stderr,sqlite3_errmsg(memdb)); sqlite3_close(memdb); return -1; } snprintf(sqlcmd,sql_create_data); rc = sqlite3_exec(memdb,"cat't create memory database %s\n",errMsg); sqlite3_close(memdb); return -1; } return 0;}//解绑数据库int DetachDb(){ int rc = 0; char* errMsg = NulL; char sqlcmd[512] = {0}; snprintf(sqlcmd,"DETACH '%s'","filedb"); rc = sqlite3_exec(memdb,"detach file database Failed:%s:%s\n",file_database_path,errMsg); sqlite3_close(memdb); return -1; } return 0;}//将文件数据库作为内存数据库的附加数据库int AttachDb(){ int rc = 0; char* errMsg = NulL; char sqlcmd[512] = {0}; snprintf(sqlcmd,"ATTACH '%s' AS %s","cat't attach database %s:%s\n",errMsg); sqlite3_close(memdb); return -1; } return 0;}//初始化数据库,分别创建文件数据库、内存数据库并把文件数据库attach到内存数据库上int InitsqliteDb(){ int retval = 0; retval = CreateDbOnfile(); if (retval != 0) { return retval; } retval = CreateDbOnMemery(); if (retval != 0) { return retval; } retval = AttachDb(); if (retval != 0) { return retval; } return 0;}总结
以上是内存溢出为你收集整理的sqlite内存数据库和文件数据库的同步全部内容,希望文章能够帮你解决sqlite内存数据库和文件数据库的同步所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)