vc++调用sqlite

1.下载对应的库,源码,工具

源码: http://www.sqlite.org/2016/sqlite-amalgamation-3150200.zip

库: http://www.sqlite.org/2016/sqlite-dll-win32-x86-3150200.zip

工具: http://www.sqlite.org/2016/sqlite-tools-win32-x86-3150200.zip

2.生成sqlite对应的lib库

打开vc的编译环境命令行,切换目录到sqlite3.def库文件所在的目录,运行如下命令

LIB /MACHINE:IX86 /DEF:sqlite3.def

生成:sqlite3.lib和sqlite3.exp

将源码中sqlite3.h,生成的sqlite3.lib和库中sqlite3.dll放入工程目录就可以用了

3.vc调用

建立各控制台程序,修改主程序代码如下,转自网上,做了部分修改

生成的数据库是test.db


// testSqlite.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"

#include <stdlib.h>
#include "sqlite3.h"
#pragma comment(lib, "sqlite3.lib")

int column_names_printed = 0;

void print_row(int n_values, char** values)
{
    int i;
    for (i = 0; i < n_values; ++i) {
        printf("%10s", values[i]);
    }
    printf("\n");
}

int print_result(void* data, int n_columns, char** column_values, char** column_names)
{
    if(!column_names_printed){
        print_row(n_columns, column_names);
        column_names_printed = 1;
    }

    print_row(n_columns, column_values);

    return 0;
}

int _tmain(int argc, _TCHAR* argv[])
{
    sqlite3 *db = NULL;
    char* errMsg = NULL;
    
    //int rc = sqlite3_open("test.db", &db);
    //if(rc){
    //    fprintf(stderr, "Can't open database: %s\n", sqlite3_errmsg(db));
    //    sqlite3_close(db);
    //    exit(1);
    //    return 1;
    //}else{
    //    printf("open test.db successfully!\n");
    //}
  
    // open db file, if db file is not exist, create it
    int rc = sqlite3_open_v2("test.db", &db,
        SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE | SQLITE_OPEN_NOMUTEX | SQLITE_OPEN_SHAREDCACHE, 0);
    if (rc == SQLITE_OK) {
        printf("open test.db successfully!\n");
    }else{
        fprintf(stderr, "Can't open database: %s\n", sqlite3_errmsg(db));
        sqlite3_close(db);
        exit(1);
        return 1;
    }


    rc = sqlite3_exec(db, "BEGIN TRANSACTION;", NULL, NULL, NULL);

    rc = sqlite3_exec(db, 
        "CREATE TABLE students(number varchar(10), name varchar(10), sex varchar(6), age varchar(2));",
        NULL, NULL, NULL);

    rc = sqlite3_exec(db, 
        "INSERT INTO students VALUES('00001', 'Mary', 'female', '15');"
        "INSERT INTO students VALUES('00002', 'John', 'male', '16');"
        "INSERT INTO students VALUES('00003', 'Mike', 'male', '15');"
        "INSERT INTO students VALUES('00004', 'Kevin', 'male', '17');"
        "INSERT INTO students VALUES('00005', 'Alice', 'female', '14');"
        "INSERT INTO students VALUES('00006', 'Susan', 'female', '16');"
        "INSERT INTO students VALUES('00007', 'Christina', 'female', '15');"
        "INSERT INTO students VALUES('00008', 'Brian', 'male', '16');"
        "INSERT INTO students VALUES('00009', 'Dennis', 'male', '14');"
        "INSERT INTO students VALUES('00010', 'Daphne', 'female', '18');",
        NULL, NULL, &errMsg);

    column_names_printed = 0;

    rc = sqlite3_exec(db, "SELECT students.* FROM students WHERE sex='female';", print_result, NULL, &errMsg);
    column_names_printed = 1;
    printf("\n");
    column_names_printed = 0;
    rc = sqlite3_exec(db, "SELECT students.* FROM students WHERE sex='male';", print_result, NULL, &errMsg);
    column_names_printed = 1;

    rc = sqlite3_exec(db, "COMMIT TRANSACTION;", NULL, NULL, NULL);

    printf("error code: %d\n", rc);
    printf("error message: %s\n", errMsg);

    sqlite3_close(db);

    return 0;
}

 

4.查看

在控制台下运行工具里的sqlite3程序

打开数据库

sqlite3 test.db

 

设置显示模式

.head on

.mode column

 

查看所有的表

.tables

 

查看数据库的各个成员的结构

select * from sqlite_master;

 

查看表数据

select * from students;

 

退出

.quit

posted on 2016-12-08 10:18  litandy  阅读(3583)  评论(0编辑  收藏  举报

导航