ThinkChat2.0新版上线,更智能更精彩,支持会话、画图、阅读、搜索等,送10W Token,即刻开启你的AI之旅 广告
1. 去官网下载去 sqlite [官网下载页面](https://www.sqlite.org/download.html)下载源文件 sqlite-amalgamation-3180000.zip 。 2. 在VS2013中,文件->新建->项目->Win32项目,命名为sqlite3(其他名称也行)。在向导设置中,选择DLL(D)、空项目。选择release x64模式。 3. 将下载的sqlite-amalgamation-3180000.zip 中的“sqlite3.h”、“sqlite3.c”、“sqlite3ext.h”三个文件添加到工程中,具体做法:右键sqlite3工程->添加->现有项,在出现的对话框中选择上述三个文件。 4. 编译生成sqlite3.dll文件,同时会在工程所在目录中产生sqlite3.obj文件。 5. 接下来要用到VS安装目录下的lib.exe文件。将sqlite3.obj文件复制到lib.exe所在目录下,我的是C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\bin\amd64。 6. 打开cmd窗口,转到lib.exe所在文件夹下。输入命令:lib sqlite3.obj。这时会生成静态的sqlite3.lib(大小为2.91MB)如果无法生成,可以试试用管理员权限运行cmd。 7. 将sqlite3.lib和sqlite3.h添加到VS工程就可以使用sqlite啦。 * * * * * * **Connect** ```c++ #include <stdio.h> #include <sqlite3.h> int main(int argc, char* argv[]) { sqlite3 *db; char *zErrMsg = 0; int rc; rc = sqlite3_open("test.db", &db); if( rc ){ fprintf(stderr, "Can't open database: %s\n", sqlite3_errmsg(db)); return(0); }else{ fprintf(stderr, "Opened database successfully\n"); } sqlite3_close(db); } ``` * **Create table** ```c++ #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)); return(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; } ``` * **Insert Table** ```c++ // T1.cpp: 定义控制台应用程序的入口点。 // #include <Windows.h> #include "stdafx.h" #include <iostream> #include <sqlite3.h> using namespace std; #pragma comment(lib,"sqlite3.lib") 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 CN2Unicode(char *input,wchar_t *output) { int len = strlen(input); //wchar_t *out = (wchar_t *) malloc(len*sizeof(wchar_t)); len=MultiByteToWideChar(CP_ACP,0,input,-1,output,MAX_PATH); return 1; } int main() { sqlite3 *db; char *zErrMsg = 0; int rc; char *sql; rc = sqlite3_open("test.db", &db); if( rc ){ fprintf(stderr, "Can't open database: %s\n", sqlite3_errmsg(db)); return(0); }else{ fprintf(stderr, "Opened database successfully\n"); } /*Create Sql statement*/ sql = "CREATE TABLE IF NOT EXISTS COMPANY(" \ "ID INT PRIMARY KEY NOT NULL," \ "NAME TEXT NOT NULL," \ "AGE INT NOT NULL," \ "ADDRESS CHAR(50)," \ "SALARY REAL );"; 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"); } char sql_insert[200]; char* old_name= "李四"; char* old_add = "杭州"; wchar_t* new_name; wchar_t* new_add; CN2Unicode(old_name,new_name); CN2Unicode(old_add,new_add); sprintf(sql_insert,"INSERT INTO \"COMPANY\" (\"ID\", \"NAME\", \"AGE\", \"ADDRESS\", \"SALARY\") VALUES (6, '%s', 12, '%s', 1.0);",new_name,new_add); rc = sqlite3_exec(db, sql_insert, callback, 0, &zErrMsg); if(rc != SQLITE_OK) { fprintf(stderr, "SQL error: %s\n", zErrMsg); sqlite3_free(zErrMsg); } else { fprintf(stdout, "Records created successfully\n"); } sqlite3_close(db); system("pause"); return 0; } ```