直接上代码
mydll.cpp
#include "sqlite3.h"
#include <stdio.h>
#define MYDLL_EXPORTS
#include "mydll.h"
#include <iostream>
using namespace std;
#pragma comment(lib, "sqlite3.lib")
sqlite3 *db = NULL; //定义数据库连接
const char *errMsg = 0; //定义错误信息
char *zerrMsg = 0; //定义错误信息
//打开数据库
int open_db()
{
int rc = sqlite3_open("E:/project/tb3/sqlite/tb/sqlit_tb/sqlit_tb/sqlitej.db",&db); //打开数据库
if(rc != SQLITE_OK) //数据库打开失败
{
errMsg = sqlite3_errmsg(db); //获取错误信息
cout<<errMsg<<endl;
sqlite3_close(db); //关闭数据库连接
return -1;
}
cout<<"open database successfully!"<<endl;
return 0;
}
//创建表
int create_table()
{
if(open_db() != 0)
{
open_db();
}
char *sql = "create table tab(id int primary key ,name varchar(20) ,age int)";
/**
//执行创建表语句 参数一:数据库连接 参数二:需要执行的SQL语句 参数三:回调函数 参数四:回调函数的第一个参数 参数五:错误消息。
回调函数的格式如下:
int sqlite_callback(
void* pvoid, 由 sqlite3_exec() 的第四个参数传递而来
int argc, 表的列数
char** argv, 指向查询结果的指针数组, 可以由 sqlite3_column_text() 得到
char** col 指向表头名的指针数组, 可以由 sqlite3_column_name() 得到
);
说明:
sqlite3_exec() 的回调函数必须按照此格式, 当然形参的名字任意.
如果某列的数据类型不是char*, 则可以对结果执行相关的转换, 如:用atoi()把结果转换为整数(integer), 如果是二进制数据, 则可以直接强制类型转换, 如:(void*)argv[i].
该回调函数有两种返回值类型.
1.返回零:sqlite3_exec() 将继续执行查询.
2.返回非零:sqlite3_exec()将立即中断查询, 且 sqlite3_exec() 将返回 SQLITE_ABORT.
*/
int rc = sqlite3_exec(db,sql,NULL,NULL,&zerrMsg);
if(rc != SQLITE_OK)
{
errMsg = sqlite3_errmsg(db);
cout<<errMsg<<endl; // cout<<zerrMsg<<endl;
sqlite3_close(db);
return -1;
}
cout<<"创建表成功!"<<endl;
return 0;
}
//删除表
int drop_table()
{
if(open_db() != 0)
{
open_db();
}
char *sql = "drop table tab";
int rc = sqlite3_exec(db,sql,NULL,NULL,&zerrMsg);
if(rc != SQLITE_OK)
{
errMsg = sqlite3_errmsg(db);
cout<<errMsg<<endl; // cout<<zerrMsg<<endl;
sqlite3_close(db);
return -1;
}
cout << "delete success" << endl;
return 0;
}
//数据添加
int insert_data(int id,char *name,int age)
{
if(open_db() != 0)
{
open_db();
}
sqlite3_stmt *stmt = NULL; //准备语句对象
char *sql = "insert into tab(id,name,age) values(?,?,?)";
/**
参数一:数据库连接 参数二:需要执行的SQL语句 参数三:SQL的长度 参数四:准备语句对象
参数三:如果nByte小于0,则函数取出zSql中从开始到第一个0终止符的内容;如果nByte不是负的,那么它就是这个函数能从zSql中读取的字节数的最大值。如果nBytes非负,zSql在第一次遇见’/000/或’u000’的时候终止
参数五:上面提到zSql在遇见终止符或者是达到设定的nByte之后结束,假如zSql还有剩余的内容,那么这些剩余的内容被存放到pZTail中,不包括终止符
*/
int rc = sqlite3_prepare_v2(db,sql,strlen(sql),&stmt,NULL);
if(rc != SQLITE_OK)
{
errMsg = sqlite3_errmsg(db);
cout<<errMsg<<endl;
if(stmt)
{
sqlite3_finalize(stmt);
}
sqlite3_close(db);
return -1;
}
sqlite3_bind_int(stmt,1,id);
//参数一:准备语句对象 参数二:序号(从1开始)参数三:字符串值 参数四:字符串长度 参数五:函数指针,SQLITE3执行完操作后回调此函数,通常用于释放字符串占用的内存。(这个函数指针参数具体怎么使用,我现在还不清楚
sqlite3_bind_text(stmt,2,name,strlen(name),NULL);
sqlite3_bind_int(stmt,3,age);
/**
statement准备好了以后,就是操作的执行了
它的返回值相对有些特殊。返回SQLITE_BUSY表示暂时无法执行操作,SQLITE_DONE表示操作执行完毕(执行update, delete, insert等语句),
SQLITE_ROW表示执行完毕并且有返回(执行select语句时)。当返回值为SQLITE_ROW时,我们需要对查询结果进行处理,
SQLITE3提供sqlite3_column_*系列函数。
*/
if(sqlite3_step(stmt) != SQLITE_DONE)
{
sqlite3_finalize(stmt);
sqlite3_close(db);
return -1;
}
cout << "add success" << endl;
sqlite3_reset(stmt);
sqlite3_finalize(stmt);
sqlite3_close(db);
return 0;
}
//数据查询 根据id唯一性查询
int search_data(int id)
{
if(open_db() != 0)
{
open_db();
}
char *sql = "select * from tab where id = ?";
sqlite3_stmt *stmt = NULL;
int rc = sqlite3_prepare_v2(db,sql,strlen(sql),&stmt,NULL);
if(rc != SQLITE_OK)
{
errMsg = sqlite3_errmsg(db);
cout<<errMsg<<endl;
if(stmt)
{
sqlite3_finalize(stmt);
}
sqlite3_close(db);
return -1;
}
sqlite3_bind_int(stmt,1,id);
int nColumn = sqlite3_column_count(stmt); //获取数据库表的列数
int type; //表字段所对应的类型
rc = sqlite3_step(stmt);
if(rc == SQLITE_ROW)
{
for(int i=0;i<nColumn;i++)
{
type = sqlite3_column_type(stmt,i);
if(type == SQLITE_INTEGER)
{
cout<<sqlite3_column_name(stmt,i)<<"\t"<<sqlite3_column_int(stmt,i)<<endl;
}
if(type == SQLITE_TEXT)
{
cout<<sqlite3_column_name(stmt,i)<<"\t"<<sqlite3_column_text(stmt,i)<<endl;
}
else if(type == SQLITE_NULL)
{
cout<<"no value"<<endl;
}
}
}
else if(rc == SQLITE_DONE)
{
cout<<"select finish!"<<endl;
}
else
{
cout<<"select fail"<<endl;
sqlite3_finalize(stmt);
}
sqlite3_finalize(stmt);
sqlite3_close(db);
return 0;
}
//数据查询 根据姓名批量查询
int search_data(char *name)
{
if(open_db() != 0)
{
open_db();
}
char *sql = "select * from tab where name = ?";
sqlite3_stmt *stmt = NULL;
int rc = sqlite3_prepare_v2(db,sql,strlen(sql),&stmt,NULL);
if(rc != SQLITE_OK)
{
errMsg = sqlite3_errmsg(db);
cout<<errMsg<<endl;
if(stmt)
{
sqlite3_finalize(stmt);
}
sqlite3_close(db);
return -1;
}
sqlite3_bind_text(stmt,1,name,strlen(name),NULL);
int nColumn = sqlite3_column_count(stmt);
int type;
rc = sqlite3_step(stmt);//如果是select语句,且有还有记录,则应该返回SQLITE_ROW
while(true) {
if(rc == SQLITE_ROW)
{
cout<<"--------------"<<endl;
for(int i=0;i<nColumn;i++)
{
type = sqlite3_column_type(stmt,i);
if(type == SQLITE_INTEGER)
{
cout<<sqlite3_column_name(stmt,i)<<"\t"<< sqlite3_column_int(stmt,i)<<endl;
}
if(type == SQLITE_TEXT)
{
cout<<sqlite3_column_name(stmt,i)<<"\t"<< sqlite3_column_text(stmt,i)<<endl;
}
else if(type == SQLITE_NULL)
{
cout<<"no value"<<endl;
}
}
}
else if(rc == SQLITE_DONE)
{
cout<<"select finish!"<<endl;
}
else
{
cout<<"select fail"<<endl;
sqlite3_finalize(stmt);
}
if (sqlite3_step(stmt) != SQLITE_ROW)
break;
}
sqlite3_finalize(stmt);
sqlite3_close(db);
return 0;
}
//删除数据
int delete_data(int age)
{
if(open_db() != 0)
{
open_db();
}
char *sql = "delete from tab where age = ?";
sqlite3_stmt *stmt = NULL;
int rc = sqlite3_prepare_v2(db,sql,strlen(sql),&stmt,NULL);
if(rc != SQLITE_OK)
{
errMsg = sqlite3_errmsg(db);
cout<<errMsg<<endl;
if(stmt)
{
sqlite3_finalize(stmt);
}
sqlite3_close(db);
return -1;
}
sqlite3_bind_int(stmt,1,age);
rc = sqlite3_step(stmt);//如果是update, delete, insert等语句,正常应该返回 SQLITE_DONE
if(rc == SQLITE_DONE)//SQLITE_DONE意味着已成功完成执行该语句
{
cout<<"删除数据成功"<<endl;
}
sqlite3_finalize(stmt);
sqlite3_close(db);
return 0;
}
int callback(void *data, int argc, char **argv, char **azColName){
int i;
fprintf(stderr, "%s: ", (const char*)data);
for(i=0; i<argc; i++){
printf("%s = %s\n", azColName[i], argv[i] ? argv[i] : "NULL");
}
printf("\n");
return 0;
}
//数据查询
int All_Data()
{
char *sql;
const char* data = "Callback function called";
if(open_db() != 0)
{
open_db();
}
sql = "SELECT * from tab";
/* Execute SQL statement */
int rc = sqlite3_exec(db, sql, callback, (void*)data, &zerrMsg);
if( rc != SQLITE_OK ){
fprintf(stderr, "SQL error: %s\n", zerrMsg);
sqlite3_free(zerrMsg);
}else{
fprintf(stdout, "Operation done successfully\n");
}
sqlite3_close(db);
return 0;
}
mydll.h
#pragma once
#ifndef _MYDLL_H
#define _MYDLL_H
#ifdef MYDLL-EXPORTS
#define MYDLL _declspec(dllexport)
#else
#define MYDLL _declspec(dllimport)
#endif
int MYDLL open_db();
int MYDLL create_table();
int MYDLL drop_table();
int MYDLL insert_data(int id, char *name, int age);
int MYDLL search_data(int id);
int MYDLL search_data(char *name);
int MYDLL All_Data();
int MYDLL delete_data(int age);
#endif
app.cpp
#include <iostream>
#include <stdio.h>
#include "mydll.h"
//使用库
#pragma comment(lib,"Dll_tb.lib")
using namespace std;
int main()
{
open_db();
create_table();
//drop_table();
insert_data(7, (char*)"ete",42);
//search_data(2);
All_Data();
//delete_data(21);
return 0;
}
工程代码下载链接: