一、sqlite 优势
sqlite不需要server,轻量级适合嵌入式开发使用,接口有C++ Java等,开发方便,
详细优势如下链接
https://blog.youkuaiyun.com/qq_34470212/article/details/52769724
二、移植
官网下载源码
https://www.sqlite.org/download.html
编译x86版本
./configure --prefix=/opt/sqlite_x86
安装编译 make && make install
编译arm版本
./configure CC=arm-linux-gcc --host=arm-linux --prefix=/opt/sqlite_arm
(要注意arm-linux-gcc编译器的路径)
安装编译:make && make install
三、简单的demo测试
添加编译出来的库 头文件到工程
#include "dialog.h"
#include <QApplication>
#include <sqlite3.h>
#include <QDebug>
#include <string>
#include <iostream>
#include <stdio.h>
using namespace std;
int callback(void*,int,char**,char**);
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
// Dialog w;
//w.show();
time_t timer_old;//time_t就是long int 类型
timer_old = time(NULL);
while(1)
{
sqlite3* db;
int nResult = sqlite3_open("/home/lzc/develop/sqlite/data/test.db",&db);
if (nResult != SQLITE_OK)
{
cout<<"打开数据库失败:"<<sqlite3_errmsg(db)<<endl;
return 0;
}
else
{
qDebug("打开数据库成功\n");
}
char* errmsg;
//第一次创建使用
//nResult = sqlite3_exec(db,"create table fuck(id integer primary key autoincrement,name varchar(100))",NULL,NULL,&errmsg);
// if (nResult != SQLITE_OK)
// {
// sqlite3_close(db);
// // cout<<errmsg;
// sqlite3_free(errmsg);
// return 0;
// }
string strSql;
strSql+="begin;\n";
for (int i=0;i<1;i++)
{
strSql+="insert into fuck values(null,'heh');\n";
}
strSql+="commit;";
//cout<<strSql<<endl;
nResult = sqlite3_exec(db,strSql.c_str(),NULL,NULL,&errmsg);
if (nResult != SQLITE_OK)
{
sqlite3_close(db);
// cout<<errmsg<<endl;
sqlite3_free(errmsg);
return 0;
}
// strSql = "select * from fuck";
// nResult = sqlite3_exec(db,strSql.c_str(),callback,NULL,&errmsg);
// if (nResult != SQLITE_OK)
// {
// sqlite3_close(db);
// // cout<<errmsg<<endl;
// sqlite3_free(errmsg);
// return 0;
// }
sqlite3_close(db);
time_t timer;//time_t就是long int 类型
timer = time(NULL);
time_t ttt=timer-timer_old;
qDebug("5555 \n");
qDebug("ttt = % ld \n",ttt);
// fprintf( stream, "%ld\n", ttt );
timer_old = timer;
}
return a.exec();
}
int callback(void* ,int nCount,char** pValue,char** pName)
{
string s;
for(int i=0;i<nCount;i++)
{
s+=pName[i];
s+=":";
s+=pValue[i];
s+="\n";
}
cout<<s<<endl;
return 0;
}