C++使用SQLite步骤及示例
C++使用SQLite步骤及示例
开发环境:Windows 10+VS2013。
开发语言:C++。
1、 下载sqlite文件。
下载网址:http://www.sqlite.org/download.html。
SQLite版本为SQLite 3.11.1,相关文件如下。
sqlite-dll-win32-x86-3110100.zip:包含sqlite3.def、sqlite3.dll文件。
sqlite-amalgamation-3110100.zip:包含sqlite3.h 文件。
sqlite-tools-win32-x86-3110100.zip:包含sqlite3.exe 文件。
2、 生成sqlite3.lib。
sqlite-dll-win32-x86-3110100.zip文件解压到D:\ sqlite。
运行Visual Studio 2013 lib命令行程序。
依次执行控制台命令。
- cd D:\sqlite\sqlite-dll-win32-x86-3110100
- D:
- E:\Microsoft Visual Studio 12.0\VC\bin\lib.exe /def:sqlite3.def /machine:ix86
即可生成sqlite3.lib文件。
3、 创建测试数据。
sqlite-tools-win32-x86-3110100.zip文件解压到D:\ sqlite。
启动命令行,进入D:\ sqlite目录。
命令依次为:
- cd D:\sqlite
- d:
创建test.db测试文件。
创建user表。
| 字段Code | 字段类型 | 字段描述 |
| id | integer | 主键,自增 |
| name | varchar(64) | 用户名 |
| age | integer | 年龄 |
创建命令依次如下。
- D:\sqlite>sqlite3.exe test.db
- SQLite version 3.7.13 2012-06-11 02:05:22
- Enter ".help" for instructions
- Enter SQL statements terminated with a ";"
- sqlite> create table user
- ...> (
- ...> id integer primary key autoincrement,
- ...> name varchar(64),
- ...> age integer
- ...> );
- sqlite> .quit
4、 创建示例工程
创建win32控制台工程SQLiteTest。
sqlite3.h(在sqlite-amalgamation-3071300.zip压缩包中)添加到工程。
sqlite3.lib复制到工程文件夹下。
工程属性中添加sqlite3.lib库依赖。
Configuration Properties->Linker->Input->Additional Dependencies添加sqlite3.lib。
程序代码为:
- /*
- @brief 本程序测试sqlite数据库的增删改查
- @date 2012-09-03
- */
- // SQLiteTest.cpp : Defines the entry point for the console application.
- //
- #include "stdafx.h"
- #include "sqlite3.h"
- #include <iostream>
- using namespace std;
- sqlite3 * pDB = NULL;
- //增加用户
- bool AddUser(const string& sName, const string& sAge);
- //删除用户
- bool DeleteUser(const string& sName);
- //修改用户
- bool ModifyUser(const string& sName, const string& sAge);
- //查找用户
- bool SelectUser();
- int _tmain(int argc, _TCHAR* argv[])
- {
- //打开路径采用utf-8编码
- //如果路径中包含中文,需要进行编码转换
- int nRes = sqlite3_open("D:\\sqlite\\test.db", &pDB);
- if (nRes != SQLITE_OK)
- {
- cout<<"Open database fail: "<<sqlite3_errmsg(pDB);
- goto QUIT;
- }
- //添加“赵钱孙李”
- if ( !AddUser("zhao", "18")
- || !AddUser("qian", "19")
- || !AddUser("sun", "20")
- || !AddUser("li", "21"))
- {
- goto QUIT;
- }
- //删除“赵”
- if (!DeleteUser("zhao"))
- {
- goto QUIT;
- }
- //修改“孙”
- if (!ModifyUser("sun", "15"))
- {
- goto QUIT;
- }
- //查找用户
- if (!SelectUser())
- {
- goto QUIT;
- }
- QUIT:
- sqlite3_close(pDB);
- return 0;
- }
- bool AddUser(const string& sName, const string& sAge)
- {
- string strSql = "";
- strSql += "insert into user(name,age)";
- strSql += "values('";
- strSql += sName;
- strSql += "',";
- strSql += sAge;
- strSql += ");";
- char* cErrMsg;
- int nRes = sqlite3_exec(pDB , strSql.c_str() ,0 ,0, &cErrMsg);
- if (nRes != SQLITE_OK)
- {
- cout<<"add user fail: "<<cErrMsg<<endl;
- return false;
- }
- else
- {
- cout<<"add user success: "<<sName.c_str()<<"\t"<<sAge.c_str()<<endl;
- }
- return true;
- }
- bool DeleteUser(const string& sName)
- {
- string strSql = "";
- strSql += "delete from user where name='";
- strSql += sName;
- strSql += "';";
- char* cErrMsg;
- int nRes = sqlite3_exec(pDB , strSql.c_str() ,0 ,0, &cErrMsg);
- if (nRes != SQLITE_OK)
- {
- cout<<"delete user fail: "<<cErrMsg<<endl;
- return false;
- }
- else
- {
- cout<<"delete user success: "<<sName.c_str()<<endl;
- }
- return true;
- }
- bool ModifyUser(const string& sName, const string& sAge)
- {
- string strSql = "";
- strSql += "update user set age =";
- strSql += sAge;
- strSql += " where name='";
- strSql += sName;
- strSql += "';";
- char* cErrMsg;
- int nRes = sqlite3_exec(pDB , strSql.c_str() ,0 ,0, &cErrMsg);
- if (nRes != SQLITE_OK)
- {
- cout<<"modify user fail: "<<cErrMsg<<endl;
- return false;
- }
- else
- {
- cout<<"modify user success: "<<sName.c_str()<<"\t"<<sAge.c_str()<<endl;
- }
- return true;
- }
- static int UserResult(void *NotUsed, int argc, char **argv, char **azColName)
- {
- for(int i = 0 ; i < argc ; i++)
- {
- cout<<azColName[i]<<" = "<<(argv[i] ? argv[i] : "NULL")<<", ";
- }
- cout<<endl;
- return 0;
- }
- bool SelectUser()
- {
- char* cErrMsg;
- int res = sqlite3_exec(pDB, "select * from user;", UserResult , 0 , &cErrMsg);
- if (res != SQLITE_OK)
- {
- cout<<"select fail: "<<cErrMsg<<endl;
- return false;
- }
- return true;
- }
编译成功后,将sqlite3.dll复制到SQLiteTest.exe同一目录下,运行SQLiteTest.exe。
运行结果:
- add user success: zhao 18
- add user success: qian 19
- add user success: sun 20
- add user success: li 21
- delete user success: zhao
- modify user success: sun 15
- id = 2, name = qian, age = 19,
- id = 3, name = sun, age = 15,
- id = 4, name = li, age = 21,
5、 SQLite管理工具
可视化管理工具,推荐使用:SQLite Expert,见:http://www.sqliteexpert.com/。
#include "stdafx.h"
#include <stdio.h>
#include "sqlite3.h"
#include <process.h>
#pragma comment(lib,"sqlite3.lib") static int callback(void *NotUsed, int argc, char **argv, char **azColName){
int i;
for(i=; i<argc; i++){
printf("%s = %s\n", azColName[i], argv[i] ? argv[i] : "NULL");
}
printf("\n");
return ;
} int _tmain(int argc, char* argv[])
{
sqlite3 *db;
char *zErrMsg = ;
int rc; if( argc!= ){
fprintf(stderr, "Usage: %s DATABASE\n", argv[]);
system("pause");
return();
}
rc = sqlite3_open(argv[], &db);
if( rc ){
fprintf(stderr, "Can't open database: %s\n", sqlite3_errmsg(db));
sqlite3_close(db);
return();
} //char* sqlstatement = "create table test(int id,varchar name);";
//char* sqlstatement = "insert into test values(1,'hello');";
char* sqlstatement = "select * from test;";
rc = sqlite3_exec(db, sqlstatement, callback, , &zErrMsg);
if( rc!=SQLITE_OK ){
printf("%s\n",argv[]);
fprintf(stderr, "SQL error: %s\n", zErrMsg);
sqlite3_free(zErrMsg);
}
sqlite3_close(db); system("pause");
return ;
}
C++使用SQLite步骤及示例的更多相关文章
- PyQt(Python+Qt)学习随笔:工具箱(QToolBox)编程使用的步骤及示例代码
老猿Python博文目录 专栏:使用PyQt开发图形界面Python应用 老猿Python博客地址 使用toolBox开发应用时,通过Designer设计ui界面时,只能在Designer中设计too ...
- Android SQLite(1)简单示例-增,删,改,查
1.主要核心类,Sqlite编程要继承SQLiteOpenHelper import android.content.Context; import android.database.sqlite.S ...
- Sqlite的操作示例代码
import android.content.Context; import android.database.sqlite.SQLiteDatabase; import android.databa ...
- android Api操作SQLite数据库的示例代码
import android.content.Context; import android.database.sqlite.SQLiteDatabase; import android.databa ...
- 用SQL语句操作Sqlite数据库的示例代码
import android.content.Context; import android.database.sqlite.SQLiteDatabase; import android.databa ...
- JNI 详细使用步骤 上手示例
1.定义本地native方法 定义本地方法,通常情况下,应单独定义一个类来封装所有native方法 /** 存放native方法的类 */ public class MyNativeMethods { ...
- python使用sqlite示例
SQLite是一种嵌入式数据库,它的数据库就是一个文件.Python就内置了SQLite3,所以,在Python中使用SQLite,不需要安装任何东西,直接使用. 操作关系数据库,首先需要连接到数据库 ...
- SQLite -- 分页查询
原文:http://blog.csdn.net/lu1024188315/article/details/51734514 参考:http://www.runoob.com/sqlite/sqlite ...
- C#操作SQLite数据库
SQLite介绍 SQLite is a software library that implements a self-contained, serverless, zero-configurati ...
随机推荐
- 杭电oj2000-2011
2000 ASCII码排序 #include <stdio.h> int main(){ char a,b,c,t; while(scanf("%c%c%c", &a ...
- Linux之epoll详细解析实现
/* * fs/eventpoll.c (Efficient event retrieval implementation) * Copyright (C) 2001,...,2009 Davide ...
- android的百度地图开发(一)
1,注册百度开发者账号 2,申请key ,注意开发版SH和发布版的SH 获取开发版SHA1: 输入命令:keytool -list -v -keystore debug.keystore,回车输入 ...
- 关于asp.net中gridview的问题,关于footer,16aspx上下的英语交流网程序,管理员的添加和修改有问题
css部分 这是添加用户的方法 但是A.AdminName 和后面的A.取到的都是空值protected void GridView1_RowCommand(object sender,GridVie ...
- Java IO 学习(六)Java的Direct Memory与IO
ByteBuffer的源码中有这样一段注释: A byte buffer is either direct or non-direct. Given a direct byte buffer, the ...
- Spring:基于配置文件的创建对象的各种方式
在Spring3.0之前,Spring主要创建对象的方法是基于配置文件的,即在配置文件中为对象进行注册,并且可以在配置文件当中为对象的字段或者称之为属性值进行赋值,接下来首先介绍基于配置文件的创建对象 ...
- (转)Ubuntu安装g++-4.8
sudo add-apt-repository ppa:ubuntu-toolchain-r/test sudo apt-get update sudo apt-get install g++-4.8 ...
- Openjudge1388 Lake Counting【DFS/Flood Fill】
http://blog.csdn.net/c20182030/article/details/52327948 1388:Lake Counting 总时间限制: 1000ms 内存限制: ...
- 洛谷——P1706 全排列问题
P1706 全排列问题 题目描述 输出自然数1到n所有不重复的排列,即n的全排列,要求所产生的任一数字序列中不允许出现重复的数字. 输入输出格式 输入格式: n(1≤n≤9) 输出格式: 由1-n组成 ...
- spoj 913 Query on a tree II (倍增lca)
Query on a tree II You are given a tree (an undirected acyclic connected graph) with N nodes, and ed ...