全方位认识SQLite(二)

来源:互联网 发布:淘宝买家微信付款骗局 编辑:程序博客网 时间:2024/05/29 04:16

示例代码

示例代码一:

#include 

#include "sqlite3.h"

using namespace std;

 

int main( )

{

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

     const char *zErrMsg = 0;//错误信息

     int rc;

     rc = sqlite3_open("E:/Program code/SQLite code/testSQLite.db", &db);

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

     {

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

         cout<<zErrMsg<<endl;

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

         return -1;

     }

     cout<<"open testSQLite.db successfully!"<<endl;

     sqlite3_close(db);

     return 0;

}

示例代码二:

头文件:my_db.hsqlite3.h

源文件:sqlitedb.cppmain.cpp

My_db.h代码:

#ifndef MY_DB_H

#define MY_DB_H

 

int open_db();

int create_table();

int drop_table();

int insert_data(int id,char *name,int age);

int search_data(int id);

int search_data(char *name);

int delete_data(int age);

 

#endif

Sqlitedb.cpp代码:

#include "sqlite3.h"

#include "my_db.h"

#include 

using namespace std;

 

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

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

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

 

//打开数据库

int open_db()

{

     int rc = sqlite3_open("E:/Program code/SQLite code/sqlitejdbc.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)";

 

     

     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<<"删除表成功"<<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(?,?,?)";

     

     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);

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

     sqlite3_bind_text(stmt,2,name,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);

     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

         {

              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

              {

                   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;

}

main.cpp代码:

#include "my_db.h"

#include 

using namespace std;

int main()

{

     //open_db();

     //create_table();

     //drop_table();

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

     //search_data(1);

     //search_data("wang");

     //delete_data(28);

     return 0;

}

SQLite加密解密

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

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

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

加密:

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

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

解密:

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

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

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

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

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

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

SQLite的管理

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

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

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

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


注:
    技术在于交流、沟通,转载请注明出处并保持作品的完整性。
    作者:╰☆奋斗ing❤孩子`  原文:http://blog.sina.com.cn/s/blog_a6fb6cc90101ffmy.html。

示例代码

示例代码一:

#include 

#include "sqlite3.h"

using namespace std;

 

int main( )

{

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

     const char *zErrMsg = 0;//错误信息

     int rc;

     rc = sqlite3_open("E:/Program code/SQLite code/testSQLite.db", &db);

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

     {

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

         cout<<zErrMsg<<endl;

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

         return -1;

     }

     cout<<"open testSQLite.db successfully!"<<endl;

     sqlite3_close(db);

     return 0;

}

示例代码二:

头文件:my_db.hsqlite3.h

源文件:sqlitedb.cppmain.cpp

My_db.h代码:

#ifndef MY_DB_H

#define MY_DB_H

 

int open_db();

int create_table();

int drop_table();

int insert_data(int id,char *name,int age);

int search_data(int id);

int search_data(char *name);

int delete_data(int age);

 

#endif

Sqlitedb.cpp代码:

#include "sqlite3.h"

#include "my_db.h"

#include 

using namespace std;

 

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

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

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

 

//打开数据库

int open_db()

{

     int rc = sqlite3_open("E:/Program code/SQLite code/sqlitejdbc.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)";

 

     

     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<<"删除表成功"<<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(?,?,?)";

     

     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);

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

     sqlite3_bind_text(stmt,2,name,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);

     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

         {

              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

              {

                   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;

}

main.cpp代码:

#include "my_db.h"

#include 

using namespace std;

int main()

{

     //open_db();

     //create_table();

     //drop_table();

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

     //search_data(1);

     //search_data("wang");

     //delete_data(28);

     return 0;

}

SQLite加密解密

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

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

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

加密:

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

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

解密:

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

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

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

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

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

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

SQLite的管理

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

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

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

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


注:
    技术在于交流、沟通,转载请注明出处并保持作品的完整性。
    作者:╰☆奋斗ing❤孩子`  原文:http://blog.sina.com.cn/s/blog_a6fb6cc90101ffmy.html。
原创粉丝点击