vc++ sqlite3 增删改插 例子


项目demo_sqlite下载地址

(项目中的代码与文章中贴出的代码有小差异,均可使用,文章中的是最新的)

http://download.csdn.net/download/prg1986/4953824


下载sqlite

去www.sqlite.org下载sqlite-amalgamation-*.zip、sqlite-dll-win32-x86-*.zip


本文中将使用的简称
sqlite-amalgamation-3071501.zip : sqlite-amalgamation.zip
sqlite-dll-win32-x86-3071501.zip : sqlite-dll.zip
demo_sqlite_ciud_using_vcpp : demo_sqlite

项目IDE版本
vs2003

生成sqlite3.lib
运行"\MicrosoftVisualStudio.NET2003\Common7\Tools\vsvars32.bat"(即"Microsoft Visual Studio .NET 2003" -> "Visual Studio .NET 工具" -> "Visual Studio .NET 2003 命令提示"), 到目录sqlite-dll下执行以下命令:
"lib /def:sqlite3.def /machine:ix86"
即可生成sqlite3.lib

需要的文件
sqlite-amalgamation\sqlite3.h
sqlite-dll\sqlite3.lib

在项目demo_sqlite中需要使用sqlite3.lib和sqlite3.h

项目demo_sqlite依赖的文件的存放路径

demo_sqlite\sqlite\
sqlite3.h
sqlite3.lib

在项目demo_sqlite中使用sqlite3.lib和sqlite3.h
用vs2003打开项目demo_sqlite, 做以下配置:
项目.属性."C/C++".常规.附加包含目录
".\sqlite"
项目.属性.链接器.常规.附加库目录
".\sqlite"
项目.属性.链接器.输入.附加依赖项
"sqlite3.lib"

至此, 即可编译运行

注意

第二次以后运行程序,运行前需要手动删除debug目录下生成的demo.db

代码

/*
author : program2013
date : 2013.1.2
fileName : DemoSqliteCiudUsingVcpp.cpp
*/

#include <tchar.h>
#include "sqlite3.h"
#include <iostream>
using namespace std;

#define NULL_CALLBACK NULL

sqlite3 * pDbConnection = NULL;
const char * szSqlCreate = " create table tb_usr(id integer primary key autoincrement, name varchar(16), score integer); ";
char * szSqlInsertFormat = " insert into tb_usr(name, score) values('%s', %d); ";
char * szSqlDeleteFormat = " delete from tb_usr where name= '%s'; ";
char * szSqlUpdateFormat = " update tb_usr set score=%d where name='%s'; ";
const char * szSqlQuery = " select id, name, score from tb_usr; ";
char szSql[128];

//create table structure
bool createTable(char* szErrorInfo)
{
	return (SQLITE_OK == sqlite3_exec(pDbConnection , szSqlCreate, NULL_CALLBACK, NULL, &szErrorInfo));
}

//add record
bool insertRecord(const char * szName, int nScore, char* szErrorInfo)
{
	
	sprintf(szSql, szSqlInsertFormat, szName, nScore);
    return ( SQLITE_OK ==  sqlite3_exec(pDbConnection , szSql, NULL_CALLBACK, NULL, &szErrorInfo) );
}

//delete record
bool deleteRecord(const char * szName, char* szErrorInfo)
{
	sprintf(szSql, szSqlDeleteFormat, szName);
    return ( SQLITE_OK == sqlite3_exec(pDbConnection, szSql, NULL_CALLBACK, NULL, &szErrorInfo) );
}

//update record
bool updateRecord(const char * szName, int nScore, char* szErrorInfo)
{
	sprintf(szSql, szSqlUpdateFormat, nScore, szName);
    return ( SQLITE_OK == sqlite3_exec(pDbConnection, szSql, NULL_CALLBACK, NULL, &szErrorInfo) );
}

int displayResultSet(void *NotUsed, int recordCount, char **record, char **colName)
{
    for(int i = 0; i < recordCount; i++)
    {
        cout << colName[i] << " = " << (record[i] ? record[i] : "NULL") << ", ";
    }
	
    cout << endl;
    return 0;
}

//query record
bool queryRecord(char* szErrorInfo)
{
    return ( SQLITE_OK == sqlite3_exec(pDbConnection, szSqlQuery, displayResultSet, NULL, &szErrorInfo) );
}

void myExit(sqlite3 * pDbConnection, const char* szErrorInfo)
{
	sqlite3_close(pDbConnection);
	cout << szErrorInfo << endl;
	system("pause");
	exit(0);
}

int _tmain(int argc, _TCHAR* argv[])
{
	char szErrorInfo[256] = {'\0'};
	
    if( SQLITE_OK != sqlite3_open(".\\demo.db", &pDbConnection) )
    {
		string strErrorInfo("error to open database, ");
		strErrorInfo += sqlite3_errmsg(pDbConnection);
        myExit(pDbConnection, strErrorInfo.c_str());
    }

	//create table structure
	if ( !createTable(szErrorInfo) )
	{
		myExit(pDbConnection, szErrorInfo);
	}

    //add record
    if ( !( insertRecord("zhangsan", 90, szErrorInfo) && insertRecord("wangwu", 69, szErrorInfo) && insertRecord("lisi", 58, szErrorInfo) ) )
    {
        myExit(pDbConnection, szErrorInfo);
    }

    //delete zhangsan
    if ( !deleteRecord("zhangsan", szErrorInfo) )
    {
		myExit(pDbConnection, szErrorInfo);
    }

    //update lisi
    if ( !updateRecord("lisi", 98, szErrorInfo) )
    {
		myExit(pDbConnection, szErrorInfo);
    }

    //query record
    if ( !queryRecord(szErrorInfo) )
    {
		myExit(pDbConnection, szErrorInfo);
    }

	szErrorInfo[0] = '\0';
	myExit(pDbConnection, szErrorInfo);

	return 0;
}


项目demo_sqlite下载地址

(项目中的代码与文章中贴出的代码有小差异,均可使用,文章中的是最新的)

http://download.csdn.net/download/prg1986/4953824

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值