在VC6下使用SQLite
1.下载Windows版本的SQLite二进制库文件
http://www.sqlite.org/download.html
下载后解压得到:sqlite3.dll sqlite3.def,并没有提供用于VC6的lib文件。我们可以用sqlite3.def文件生成。
2.生成sqlite3.lib文件
打开命令行,切换目录到sqlite3.def库文件所在的目录,运行如下命令:LIB /MACHINE:IX86 /DEF:sqlite3.def
运行后,生成:sqlite3.lib和sqlite3.exp
3.新建控制台程序。将sqlite3.dll, sqlite3.lib, sqlite3.h文件放到工程目录下。
代码如下:
View Code
1 #include <stdio.h> 2 #include <stdlib.h> 3 #include <sqlite3.h> 4 5 bool bNeedPrintHeader = true; 6 void print_row(int nCols, char** columnValues) 7 { 8 for (int i=0; i<nCols; i++) 9 { 10 printf("%10s", columnValues[i]); 11 } 12 printf("\n"); 13 } 14 15 int print_result(void* data, int nCols, char** columnValues, char** columnNames) 16 { 17 if (bNeedPrintHeader) 18 { 19 print_row(nCols, columnNames); 20 bNeedPrintHeader = false; 21 } 22 23 print_row(nCols, columnValues); 24 return 0; 25 } 26 27 int main() 28 { 29 sqlite3 *db = NULL; 30 char *errMsg = NULL; 31 32 int rCode = sqlite3_open("test.db", &db); 33 if (rCode) 34 { 35 fprintf(stderr, "Can't open database:%s\n", sqlite3_errmsg(db)); 36 sqlite3_close(db); 37 exit(1); 38 } 39 else 40 { 41 printf("open test.db successfully!\n"); 42 } 43 44 rCode = sqlite3_exec(db, "BEGIN TRANSACTION;", NULL, NULL, NULL); 45 rCode = sqlite3_exec(db, "CREATE TABLE students(number varchar(10), name varchar(10), sex varchar(6), age varchar(2));", NULL, NULL, NULL); 46 rCode = sqlite3_exec(db, "INSERT INTO students VALUES('00001', 'Mary', 'female', '15');\ 47 INSERT INTO students VALUES('00002', 'John', 'male', '16');\ 48 INSERT INTO students VALUES('00003', 'Mike', 'male', '15');\ 49 INSERT INTO students VALUES('00004', 'Kevin', 'male', '17');\ 50 INSERT INTO students VALUES('00005', 'Alice', 'female', '14');\ 51 INSERT INTO students VALUES('00006', 'Susan', 'female', '16');\ 52 INSERT INTO students VALUES('00007', 'Christina', 'female', '15');\ 53 INSERT INTO students VALUES('00008', 'Brian', 'male', '16');\ 54 INSERT INTO students VALUES('00009', 'Dennis', 'male', '14');\ 55 INSERT INTO students VALUES('00010', 'Daphne', 'female', '18');", NULL, NULL, &errMsg); 56 57 bNeedPrintHeader = true; 58 rCode = sqlite3_exec(db, "SELECT students.* FROM students WHERE sex='female';", print_result, NULL, &errMsg); 59 printf("\n"); 60 61 bNeedPrintHeader = true; 62 rCode = sqlite3_exec(db, "SELECT students.* FROM students WHERE sex='male';", print_result, NULL, &errMsg); 63 rCode = sqlite3_exec(db, "COMMIT TRANSACTION;", NULL, NULL, NULL); 64 65 printf("error code: %d\n", rCode); 66 printf("error message: %s\n", errMsg); 67 sqlite3_close(db); 68 69 return 0; 70 }
posted on 2012-08-03 10:47 ActiveChange 阅读(2710) 评论(0) 编辑 收藏 举报