全方位认识SQLite二

作者:奋斗ing丶孩子

转自:http://blog.sina.com.cn/s/blog_a6fb6cc90101ffmy.html


示例代码

示例代码一:

#include

#include"sqlite3.h"

usingnamespacestd;

intmain()

{

sqlite3*db=NULL;//定义sqlite的连接

constchar*zErrMsg= 0;//错误信息

intrc;

rc= sqlite3_open("E:/Programcode/sqlite code/testsqlite.db",&db);

if(rc!= sqlITE_OK)//如果打开数据库失败

{

zErrMsg= sqlite3_errmsg(db);//获取错误信息

cout<<zErrMsg<<endl;

sqlite3_close(db);//关闭数据库连接

return-1;

}

cout<<"opentestsqlite.db successfully!"<<endl;

sqlite3_close(db);

return0;

}

示例代码二:

文件my_db.hsqlite3.h

文件sqlitedb.cppmain.cpp

My_db.h代码

#ifndefMY_DB_H

#defineMY_DB_H

intopen_db();

intcreate_table();

intdrop_table();

intinsert_data(intid,char*name,intage);

intsearch_data(intid);

intsearch_data(char*name);

intdelete_data(intage);

#endif

sqlitedb.cpp代码

#include"sqlite3.h"

#include"my_db.h"

#include

usingnamespacestd;

sqlite3 *db =NULL;//定义数据库连接

constchar*errMsg= 0;//定义错误信息

char*zerrMsg= 0;//定义错误信息

//打开数据库

intopen_db()

{

intrc= sqlite3_open("E:/Programcode/sqlitecode/sqlitejdbc.db",&db);//打开数据库

if(rc!=sqlITE_OK)//数据库打开失败

{

errMsg= sqlite3_errmsg(db);//获取错误信息

cout<<errMsg<<endl;

sqlite3_close(db);//关闭数据库连接

return-1;

}

cout<<"opendatabase successfully!"<<endl;

return0;

}

//创建表

intcreate_table()

{

if(open_db()!= 0)

{

open_db();

}

char*sql="createtable tab(id int primary key,name varchar(20),ageint)";

intrc= sqlite3_exec(db,sql,NULL,&zerrMsg);

if(rc!= sqlITE_OK)

{

errMsg= sqlite3_errmsg(db);

cout<<errMsg<<endl;//cout<<zerrMsg<<endl;

sqlite3_close(db);

return-1;

}

cout<<"创建表成功!"<<endl;

return0;

}

//删除

intdrop_table()

{

if(open_db()!= 0)

{

open_db();

}

char*sql="droptable tab";

intrc= sqlite3_exec(db,&zerrMsg);

if(rc!= sqlITE_OK)

{

errMsg= sqlite3_errmsg(db);

cout<<errMsg<<endl;//cout<<zerrMsg<<endl;

sqlite3_close(db);

return-1;

}

cout<<"删除表成功"<<endl;

return0;

}

//数据添加

intinsert_data(intid,intage)

{

if(open_db()!= 0)

{

open_db();

}

sqlite3_stmt*stmt = NULL;//准备语句对象

char*sql="insertinto tab(id,name,age) values(?,?,?)";

intrc= sqlite3_prepare_v2(db,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);

//参数一:准备语句对象参数二:序号(从开始)参数三:字符串值参数四:字符串长度参数五:函数指针,sqlite3执行完操作后回调此函数,通常用于释放字符串占用的内存。(这个函数指针参数具体怎么使用,我现在还不清楚

sqlite3_bind_text(stmt,2,strlen(name),NULL);

sqlite3_bind_int(stmt,3,age);

if(sqlite3_step(stmt)!= sqlITE_DONE)

{

sqlite3_finalize(stmt);

sqlite3_close(db);

return-1;

}

cout<<"数据插入成功!"<<endl;

sqlite3_reset(stmt);

sqlite3_finalize(stmt);

sqlite3_close(db);

return0;

}

//数据查询根据id唯一性查询

intsearch_data(intid)

{

if(open_db()!= 0)

{

open_db();

}

char*sql="select *from tab where id = ?";

sqlite3_stmt*stmt= NULL;

intrc= sqlite3_prepare_v2(db,id);

intnColumn= sqlite3_column_count(stmt);//获取数据库表的列数

inttype;//表字段所对应的类型

rc= sqlite3_step(stmt);

if(rc== sqlITE_ROW)

{

for(inti=0;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;

}

elseif(type== sqlITE_NULL)

{

cout<<"novalue"<<endl;

}

}

}

elseif(rc== sqlITE_DONE)

{

cout<<"selectfinish!"<<endl;

}

else

{

cout<<"selectfail"<<endl;

sqlite3_finalize(stmt);

}

sqlite3_finalize(stmt);

sqlite3_close(db);

return0;

}

//数据查询根据姓名批量查询

intsearch_data(char*name)

{

if(open_db()!= 0)

{

open_db();

}

char*sql="select *from tab where name = ?";

sqlite3_stmt*stmt= NULL;

intrc= sqlite3_prepare_v2(db,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,NULL);

intnColumn= sqlite3_column_count(stmt);

inttype;

rc= sqlite3_step(stmt);//如果是select语句,且有还有记录,则应该返回sqlITE_ROW

while(true){

if(rc== sqlITE_ROW)

{

cout<<"--------------"<<endl;

for(inti=0;i

{

type= sqlite3_column_type(stmt,i);

if(type== sqlITE_INTEGER)

{

cout<<sqlite3_column_name(stmt,i)<<endl;

}

if(type== sqlITE_TEXT)

{

cout<<sqlite3_column_name(stmt,i)<<endl;

}

elseif(type== sqlITE_NULL)

{

cout<<"novalue"<<endl;

}

}

}

elseif(rc== sqlITE_DONE)

{

cout<<"selectfinish!"<<endl;

}

else

{

cout<<"selectfail"<<endl;

sqlite3_finalize(stmt);

}

if(sqlite3_step(stmt)!= sqlITE_ROW)

break;

}

sqlite3_finalize(stmt);

sqlite3_close(db);

return0;

}

//删除数据

intdelete_data(intage)

{

if(open_db()!= 0)

{

open_db();

}

char*sql="deletefrom tab where age = ?";

sqlite3_stmt*stmt= NULL;

intrc= sqlite3_prepare_v2(db,age);

rc=sqlite3_step(stmt);//如果是update,delete,insert等语句,正常应该返回sqlITE_DONE

if(rc== sqlITE_DONE)//sqlITE_DONE意味着已成功完成执行该语句

{

cout<<"删除数据成功"<<endl;

}

sqlite3_finalize(stmt);

sqlite3_close(db);

return0;

}

main.cpp代码

#include"my_db.h"

#include

usingnamespacestd;

intmain()

{

//open_db();

//create_table();

//drop_table();

//insert_data(3,"wang",25);

//search_data(1);

//search_data("wang");

//delete_data(28);

return0;

}

sqlite加密解密

数据库而言,加密无非是必不可少了一个环节,如何才能让自己更安全更放心的使用sqlite呢?加密!但是在这里还是很不好意思的说,如果是面向公众开放的开源性sqlite,他是不能实现加密的,当然,除非你付款。此前我也很想研究下加密部分,当看到资料上存在可以加密、解密的方法时,很兴奋的试了试,结果却很让人很纠结。

进入sqlite3.h文件当中,搜索sqlite3_key(sqlite3*db,const void *pKey,int nKey)加密方法sqlite3_rekey(sqlite3*db,int nKey)解密方法时,可以看到主要的注释——The code toimplement this API is not available in the public release ofsqlite.

虽然加密解密方法对于开源用户来说不能使用,但是我们毕竟也了解到sqlite本身是提供加密、解密方法的。所以先了解一下加密、解密方法的使用吧。

加密:

一个未加密的数据库添加密码:如果想要添加密码,则可以在打开数据库文件之后,关闭数据库文件之前的任何时刻调用sqlite3_key函数即可,

函数有三个参数,其中参数一:数据库对象参数二:要设定的密码参数三:密码的长度。例:sqlite3_key(db,"123456",6);

解密:

sqlite3_rekey是变更密码或给没有加密的数据库添加密码或清空密码,变更密码或清空密码前必须先正确执行sqlite3_key

在正确执行sqlite3_rekey之后在sqlite3_close关闭数据库之前可以正常操作数据库,不需要再执行sqlite3_key

sqlite3_rekey(sqlite3 *db,int nKey),参数同上。清空密钥为sqlite3_rekey( db,0)

如果实在是想进行加密解密的话,那么我也不妨提出自己的两个建议吧!

建议一:掏钱购买吧!没什么可说的。

建议二:自己实现加密解密方法。对即将存入数据库的数据进行加密之后再进行保存,取数据的时候先进行解密再进行获取

sqlite的管理

管理工具也有不少,这里介绍几款:

1sqlite Manager开放源代码SQLite管理工具,用来管理本地电脑上的sqlite数据库,可以独立运行(以XULRunner方式),也可以作为FirefoxThunderbirdSeamonkeySongbirdKomodoGecko等的插件

2sqlite Administrator一个用来管理SQLite数据库文件的图形化工具,可进行创建、设计和管理操作。提供代码编辑器具有自动完成和语法着色,支持中文,适合初学者。

3sqlite Database browser一个sqlite数据库的轻量级GUI客户端,基于Qt库开发,界面清洁,操作简单,主要是为非技术用户创建、修改和编辑sqlite数据库的工具,使用向导方式实现。


注:
技术在于交流、沟通,转载请注明出处并保持作品的完整性。

相关文章

SQLite架构简单,又有Json计算能力,有时会承担Json文件/RES...
使用Python操作内置数据库SQLite以及MySQL数据库。
破解微信数据库密码,用python导出微信聊天记录
(Unity)SQLite 是一个软件库,实现了自给自足的、无服务器...
安卓开发,利用SQLite实现登陆注册功能