sqlite3使用

c库:

下载地址:https://www.sqlite.org/download.html

交叉编译工具编译出错:

-->WARNING: 'aclocal-1.15' is missing on your system

sudo apt install automake-1.15

-->sqlite3-shell.o: file not recognized: File format not recognized

是因为我在pc端点一了这个文件,make clean以下即可

1、打开关闭

sqlite3_open,sqlite3_close

2、执行命令:

sqlite3_exec

3、select语句得到返回用:

sqlite3_get_table,sqlite3_free_table

4、另外一个好用的接口:

sqlite3_prepare,sqlite3_bind_blob(), sqlite3_step(通过判断返回值为SQLITE_ROW得到所有数据), sqlite3_finalize

参考:https://blog.csdn.net/u012351051/article/details/90634066?utm_medium=distribute.pc_relevant.none-task-blog-BlogCommendFromBaidu-1.nonecase&depth_1-utm_source=distribute.pc_relevant.none-task-blog-BlogCommendFromBaidu-1.nonecase

sqlite3_stmt *stmt;

stringstream getcmd;

string tmpstr;

int sqlrst = 0, preResult = 0;

getcmd << "insert into mytable (id, name, score, picture) values ("<< usrid << ", 张三, " << inputScore << ",?);";//如果要用sqlite3_bind_,要使用“?”来代替。

tmpstr = getcmd.str();

getcmd.str("");

coutdbg << tmpstr << endl;

preResult = sqlite3_prepare(dbwork, tmpstr.c_str(), tmpstr.length(), &stmt, 0);

sqlite3_bind_blob(stmt, 1, featurebuf, len, NULL);//可以为sql语句中的"?"填数据,1代表第一个?

sqlrst = sqlite3_step(stmt);//如果有多行数据,返回SQLITE_ROW,正常返回SQLITE_DONE

sqlite3_finalize(stmt);

5、错误#define SQLITE_MISUSE 21

一般是我们sql语句有问题,或者没有对应的表格,问题一般都是我们的问题。

6、未解之谜:#define SQLITE_ERROR 1 和14错误

我一直执行sql语句,发现我insert 1018 条语句后,报这个错误,后面都不能再次插入数据,我尝试sqlite3_close+sqlite3_open结果打开失败,重新运行程序从1019条继续插入却不会报这个错误!(出错后使用sqlite3_exec+sqlite3_get_table暂时没发现错误)我使用新建数据库和新建表格不会出现上面的错误

 

7.sqlite3效率问题:

参考:https://blog.csdn.net/u011726005/article/details/77756467

同异步处理的优劣势,数据损坏问题:https://blog.csdn.net/chinaclock/article/details/48622243

打开与关闭同步操作,关闭同步,我执行多条(非批量操作)insert或delete效果提升10倍

int tableOptionClassDefine::optSetSyncPragma(int syncflag)

{

string sqlstr;

if(syncflag)

{

sqlstr = "PRAGMA synchronous = NO;";

}

else

{

sqlstr = "PRAGMA synchronous = OFF;";

}

int rst;

rst = sqlite3_exec(sodb, sqlstr.c_str(), NULL, NULL, &sqlite_error_msg);

}

使用事务:在批量操作时,效果极为突出,提高上几十倍效率,并且此时同异步不起太大作用。

sqlite3_exec(sodb, "begin", NULL, NULL, &sqlite_error_msg);

sqlite3_exec(sodb, "commit", NULL, NULL, &sqlite_error_msg);

如:

sqlite3_exec(sodb, "begin", NULL, NULL, &sqlite_error_msg);

sqlstr = "insert into " SRC2_T1_TABLE_NAME " (uid, name, age, addr, photo) values (?, ?, ?, ?, ?);";

coutdbg(sqlstr);

rst = sqlite3_prepare_v2(sodb, sqlstr.c_str(), -1, &stmt, 0);

for(i = 0; i < 10000; i++)

{

tmpinfo.uid = i + 1;

tmpinfo.age = (i *1233 + 5) % 20;

tmpinfo.name = "欣" + to_string(tmpinfo.uid);

tmpinfo.addr = "锦州" + to_string(tmpinfo.uid);

setPicVal(tmpinfo, tmpinfo.uid);

sqlite3_reset(stmt);

sqlite3_bind_int(stmt, 1, tmpinfo.uid);

sqlite3_bind_text(stmt, 2, tmpinfo.name.c_str(), tmpinfo.name.size(), SQLITE_STATIC);

sqlite3_bind_int(stmt, 3, tmpinfo.age);

sqlite3_bind_text(stmt, 4, tmpinfo.addr.c_str(), tmpinfo.addr.size(), SQLITE_STATIC);

sqlite3_bind_blob(stmt, 5, tmpinfo.pic, STUDENT_PIC_SIZE_FIXED, SQLITE_STATIC);

rst = sqlite3_step(stmt);

if(JUDEG_SQL_RESULT(rst))

{

coutdbg("err:" << rst);

break;

}

}

sqlite3_exec(sodb, "commit", NULL, NULL, &sqlite_error_msg);

sqlite3_finalize(stmt);

 

8.读取sqlite数据:

int tableOptionClassDefine::optGetUsrIdInform(u32 usrid, studentsInformDef *info)

{

JUDGE_OPT_DB_NULL_AND_PTR(info);

 

string sqlstr;

stringstream sstr;

int rst;

 

TAB_TRANSATION_RUN_BEGIN;

sqlite3_stmt *stmt;

sqlstr = "select uid, age, name, age, addr, photo from " SRC2_T1_TABLE_NAME " where uid = ?;";

coutdbg(sqlstr);

rst = sqlite3_prepare_v2(sodb, sqlstr.c_str(), -1, &stmt, 0);

 

sqlite3_reset(stmt);

sqlite3_bind_int(stmt, 1, usrid);

rst = sqlite3_step(stmt);

if(rst == SQLITE_ROW)

{

const char *picval;

info->uid = sqlite3_column_int(stmt, 0);

info->name = (const char *)sqlite3_column_text(stmt, 1);

info->age = sqlite3_column_int(stmt, 2);

info->addr = (const char *)sqlite3_column_text(stmt, 3);

picval = (const char *)sqlite3_column_blob(stmt, 4);

info->copyPic(picval, STUDENT_PIC_SIZE_FIXED);

rst = WORK_ERR_OK;

}

else

{

coutdbg("err:" << rst);

rst = SQLITE_OPT_ERR_DEFIN(rst);

}

TAB_TRANSATION_RUN_COMMIT;

sqlite3_finalize(stmt);

return rst;

}

 

9.我使用了联合主键(复合主键,关联主键)。

CREATE TABLE tabUserFPData (studentNumber INTEGER, age INTEGER, IdCard INTEGER,name TEXT, photo BLOB, addr TEXT, PRIMARY KEY (studentNumber, age, IdCard));

  使用IdCard查找name居然很慢,但是用studentNumber查找很快;用IdCard查找studentNumber和age快,猜测唯一主键是studentNumber,通过studentNumber查找都很快,而使用IdCard查找其他几个联合主键快。

总结:---->联合主键,第一个key=studentNumber是表的主键,而其他几个联合主键并不是表的主键,但是他们的相互查找是很快的。但是如果sex是0-1变化的,也不能起啥作用。---->如果数据库一行数据很大,比如photo文件大,会降低操作效率

你可能感兴趣的:(sqlite3使用)