下载sqlite
官方网页上下载即可
编译
下载内容分别是源码,32和64位的dll,和shell工具。
解压后将源码的头文件放到inc文件夹,32位的dll和def,和64位的dll,def分别放到各自的文件夹。现在生成lib文件
用Visual Studio的“开发人员命令提示”进入sqlite-x86目录
执行lib /def:sqlite3.def /machine:x86 命令生成 lib 文件
执行lib /def:sqlite3.def /machine:x64编译x64的lib
头文件,lib,dll都有了,使用就比较容易了。可以下载的sqlite可视化工具,查看数据比较直观,不然就只能使用下载的tools去查看了。
测试
#include <stdio.h>
#include <stdlib.h>
#include <sqlite3.h>
static int callback(void *NotUsed, int argc, char **argv, char **azColName){
int i;
for(i=0; i<argc; i++){
printf("%s = %s\n", azColName[i], argv[i] ? argv[i] : "NULL");
}
printf("\n");
return 0;
}
int main(int argc, char* argv[])
{
sqlite3 *db;
char *zErrMsg = 0;
int rc;
char *sql;
/* Open database */
rc = sqlite3_open("test.db", &db);
if( rc ){
fprintf(stderr, "Can't open database: %s\n", sqlite3_errmsg(db));
exit(0);
}else{
fprintf(stdout, "Opened database successfully\n");
}
/* Create SQL statement */
sql = "CREATE TABLE COMPANY(" \
"ID INT PRIMARY KEY NOT NULL," \
"NAME TEXT NOT NULL," \
"AGE INT NOT NULL," \
"ADDRESS CHAR(50)," \
"SALARY REAL );";
/* Execute SQL statement */
rc = sqlite3_exec(db, sql, callback, 0, &zErrMsg);
if( rc != SQLITE_OK ){
fprintf(stderr, "SQL error: %s\n", zErrMsg);
sqlite3_free(zErrMsg);
}else{
fprintf(stdout, "Table created successfully\n");
}
sqlite3_close(db);
return 0;
}