欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

sqlite3 beginning

程序员文章站 2022-05-31 08:48:27
...

安装&使用
下载源码,autoconf的包, ./configure && make -j8
除了可执行文件, 还能得到.a文件

g++ -Ixxx -Lxxx -lxxxx即可

#include <iostream>
#include <string>
#include "sqlite3.h"

using namespace std;

int main() {
	sqlite3 *db = nullptr;
	int len = sqlite3_open("user", &db);
	if(len){
		cerr<<"error create new db"<<endl;
		cout<<db<<endl;
		exit(-1);
	}

	const char *cmd = "create table SensorData("
	   "ID integer primary key, "
		 "SensorID integer);";

	char *zErrMsg = nullptr;
	sqlite3_exec(db, cmd, NULL, NULL, &zErrMsg);
	if(!zErrMsg){
		cout<<"exec successful"<<endl;
	} else {
		cout<<"errMsg = "<<zErrMsg<<endl;
		sqlite3_free(zErrMsg);
	}

	char cmdBuf[BUFSIZ];
	for(int i=104;i<160;i+=2){
		sprintf(cmdBuf, "insert into 'sensorDaTa' values(null, %d);", i);
		zErrMsg = nullptr;
		sqlite3_exec(db, cmdBuf, NULL, NULL, &zErrMsg);
		if(zErrMsg){
			cerr<<zErrMsg<<endl;
			sqlite3_free(zErrMsg);
		} else {
		}
	}

	sqlite3_close(db);
	db = nullptr;
	cout<<"done"<<endl;
	
}
相关标签: sqlite3 sqlite3