使用VC与GCC操作SQLite数据库的最简单Demo

本文介绍如何使用SQLite数据库,包括在VC6.0和GCC环境下搭建SQLite开发环境,通过示例代码展示如何创建表、插入数据及查询数据等基本操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一、准备工作

  到http://www.sqlite.org/download.html下载Source Code里的amalgamation版,里头有4个文件,sqlite3.c、sqlite3.h、shell.c、sqlite3ext.h。它们具体是干嘛的我还不清楚,容后具体看看,现在只是根据网上看到的方法,简单做一个Demo试试。这里我们用到的文件有sqlite3.c和sqlite3.h,sqlite3.h里声明的有各种接口函数,而sqlite3.c用于生成需要链接的库文件。这篇日志主要是记录怎么组织的文件,做个笔记。

 

二、VC6.0版

  新建一个Win32 Console Application,将自写的main.cpp及前面提到的sqlite3.h、sqlite3.c添加到工程中,直接编译运行即可。

 

三、GCC版

  将main.cpp、sqlite3.h、sqlite3.c置于同一目录下,并在同一目录下新建一文本文件makefile,执行make命令即可。

 

四、相关源码

  (1)不规范的makefile

main.exe: main.cpp sqlite3.lib
	g++ main.cpp sqlite3.lib -o main.exe

sqlite3.lib: sqlite3.o
	ar crv sqlite3.lib sqlite3.o

sqlite3.o:sqlite3.c
	gcc -c sqlite3.c -o sqlite3.o


  (2)main.cpp(源于网络http://dearymz.blog.163.com/blog/static/20565742011518102612855/

#include "sqlite3.h"

#include <stdio.h>
#include <tchar.h>

#include <process.h>

#include <iostream>
using std::cout;
using std::endl;

bool test(sqlite3* db)

{
    sqlite3_stmt* stmt = NULL; 
    if(sqlite3_prepare_v2(db, 
                "create table if not exists files("
                "id int primary key not null, "
                "name string unique not null, "
                "size int not null, "
                "data blob not null)", 
                512, &stmt, NULL) != SQLITE_OK)
        return false;

    if(sqlite3_step(stmt) != SQLITE_DONE)
        return false;

    if(sqlite3_finalize(stmt) != SQLITE_OK)
        return false;

    if(sqlite3_prepare_v2(db,
                "insert into files values(last_insert_rowid() + 1, ?, 0, ?)",
                512, &stmt, NULL) != SQLITE_OK)
        return false;

    if(sqlite3_reset(stmt) != SQLITE_OK)
        return false;

    if(sqlite3_bind_text(stmt, 1, "http://dearymz.blog.163.com", -1, NULL) != SQLITE_OK)
        return false;

    if(sqlite3_bind_text(stmt, 2, "http://dearymz.blog.163.com", -1, NULL) != SQLITE_OK)
        return false;

    if(sqlite3_step(stmt) != SQLITE_DONE)
        return false;

    if(sqlite3_finalize(stmt) != SQLITE_OK)
        return false;

    if(sqlite3_prepare_v2(db,
                "select count(*) from files",
                512, &stmt, NULL) != SQLITE_OK)
        return false;

    if(sqlite3_step(stmt) != SQLITE_ROW)
        return false;

    cout << sqlite3_column_int(stmt, 0) << endl;

    if(sqlite3_finalize(stmt) != SQLITE_OK)
        return false;

    return true;
}

int _tmain(int argc, _TCHAR* argv[])

{
    sqlite3* db = NULL;

    if(sqlite3_open("test.db3", &db) == 0)
    {
        if(test(db))
            cout << "OK" << endl;
        else
            cout << "Error!" << endl;
    }

    cout << sqlite3_errmsg(db) << endl;

    sqlite3_close(db);

    system("pause");

    return 0;
} 


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值