c++中使用sqlite3

sqlite插入和查询效率提高方法及测试结果

一、下载sqlite源码和dll库http://www.sqlite.org/download.html

二、在vs2012中新建工程,解压文件复制sqlite3.h,sqlite3.c文件到新建的项目中,配置将sqlite3.dll文件夹包含到项目中。

三、新建源文件,编写简单的测试。

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

using namespace std;

static int callback(void *NotUsed, int argc, char **argv, char **azColName){
    int i;
    fprintf(stderr, "%s: ", (const char*)NotUsed);
    for (i = 0; i<argc; i++){
        printf("%s = %s\n", azColName[i], argv[i] ? argv[i] : "NULL");
    }
    printf("\n");
    return 0;
}

int main()
{
	char value[1000]={0};
    sqlite3 *db;
    char *zErrMsg = 0;
    int  rc;
	int key=0;
    const char *sql ;
	string temp;

    /* Open database */
    rc = sqlite3_open("test.db", &db);
    if (rc){
		cout <<  "Can't open database test.db" << endl;
		sqlite3_free(zErrMsg);
        exit(0);
    }
    else{
         cout << "open the database successful!" << endl;
    }

    // Create table statement
	
	sql = "CREATE TABLE table1(key INT PRIMARY KEY NOT NULL,value TEXT    NOT NULL);";
	rc = sqlite3_exec(db, sql, callback, 0, &zErrMsg);
    if (rc != SQLITE_OK){
        cout << zErrMsg << endl;
        sqlite3_free(zErrMsg);
    }
    else{
        cout << "create table successful!" << endl;
    }
       
  
  • 2
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值