Code Planet

为了发现更大的世界


  • 首页

  • 关于

  • 标签

  • 分类

  • 归档

c++调用sqlite3数据库

发表于 2013-07-28 | 更新于 2015-08-15 | 分类于 c/cpp
#include "stdio.h"
#include "stdlib.h"
extern "C"
{
#include "sqlite3.h"
};

int open_database(sqlite3** db,char* dbName);
int close_database(sqlite3* db);
int query(void* para,int n_column,char** column_value,char **column_name);

int main()
{
    char* errmsg=NULL;
    sqlite3* db;
    open_database(&db,"C:\\test.db");
    int ret = sqlite3_exec(db, "select * from stu;" ,query, NULL, &errmsg );
    close_database(db);

    system("pause");
    return 0;
}
int open_database(sqlite3** db,char* dbName)
{
    int result;
    char * errmsg = NULL;
    result = sqlite3_open(dbName,db );
    if( result != SQLITE_OK )
    {
        // 数据库打开失败
        printf("打开数据库【%s】失败\n",dbName);
        return 0;
    }
    printf("打开数据库【%s】成功\n",dbName);
    return 1;
}

int query_nn(sqlite3** db,char* sql)
{
    char** dbResult;
    int nRow,nColumn;
    int index;
    int result;
    char* errmsg = NULL;
    //
    result = sqlite3_get_table(*db,sql,&dbResult,&nRow,&nColumn,&errmsg );
    if( SQLITE_OK == result )
    {
        // 查询成功
        index = nColumn; // 前面说过 dbResult 前面第一行数据是字段名称,从 nColumn 索引开始才是真正的数据
        printf( "查到 %d 条记录 \n " , nRow );
        for(int i = 0; i < nRow ; i++ )
        {
            printf( "第 %d 条记录 \n " , i+1 );
            for(int j = 0 ; j < nColumn; j++ )
            {
                printf( "字段名 :%s > 字段值 :%s\n " , dbResult[j], dbResult [index] );
                ++index; // dbResult 的字段值是连续的 , 从第 0 索引到第 nColumn - 1 索引都是字段名称 , 从第nColumn 索引开始 , 后面都是字段值 , 它把一个二维的表 ( 传统的行列表示法 ) 用一个扁平的形式来表示
            }
            printf( "-------\n " );
        }
    }
    //printf( "!!!错误码 :%d ,错误原因 :%s\n ", result, errmsg );
    // 到这里 , 不论数据库查询是否成功 , 都释放 char** 查询结果 , 使用 sqlite 提供的功能来释放
    sqlite3_free_table( dbResult );
    return 1;
}

int close_database(sqlite3* db)
{
    int result;
    char * errmsg = NULL;
    result = sqlite3_close(db);
    if( result != SQLITE_OK )
    {
        // 数据库打开失败
        printf("关闭数据库失败\n");
        return 0;
    }
    printf("关闭数据库成功\n");
    return 1;
}
int query(void* para,int n_column,char** column_value,char **column_name)
{
    //para是你在sqlite3_exec里传入的void*参数
    //n_column是这一条记录有多少个字段
    //char** column_value是个关键值,查出来的数据都保存在这里(实际上是个 1 维数组)
    //char** column_name跟 column_value 是对应的,表示这个字段的字段名称
    int i;
    printf("记录包含 %d 个字段\n", n_column );
    for(i = 0 ; i < n_column; i++ )
    {
        printf("字段名 :%s > 字段值 :%s\n" , column_name[i], column_value[i] );
    }
    printf("-------------------------------\n ");
    return 0;
}

/*
//创建一个测试表,表名叫stu
result = sqlite3_exec( db, "create table stu( id integer primary key autoincrement, name varchar(10),age integer);" , NULL, NULL, &errmsg );
if(result != SQLITE_OK )
{
printf( "创建表失败,错误码 :%d,错误原因 :%s\n" , result, errmsg );
}

// 插入一些记录
result = sqlite3_exec( db, "insert into stu(name,age) values ('Alex',18);" , 0, 0, &errmsg );
if(result != SQLITE_OK )
{
printf( "插入记录失败,错误码 :%d ,错误原因 :%s\n " , result, errmsg );
}
result = sqlite3_exec( db, "insert into stu(name,age) values ('Bob',19);" , 0, 0, &errmsg );
if(result != SQLITE_OK )
{
printf( "插入记录失败,错误码 :%d ,错误原因 :%s\n ", result, errmsg );
}
result = sqlite3_exec( db, "insert into stu(name,age) values ('Cindy',20);", 0, 0, &errmsg);
if(result != SQLITE_OK )
{
printf( "插入记录失败,错误码 :%d ,错误原因 :%s\n " , result, errmsg );
}
// 开始查询数据库
//result = sqlite3_exec( db, "select * from stu;" , LoadMyInfo, NULL, &errmsg );

*/

查询结果输出到文件:

sqlite> .output ‘result’ // 指定导出文件
sqlite> select from class; // 查询数据,输出到文件
sqlite> .read ‘result’ // 查看到处的文件数据
sqlite> .output stdout // 重新设置输出终端
sqlite> select
from class; // 查询数据,可在终端看到数据

pygame初探

发表于 2013-07-18 | 更新于 2015-08-15 | 分类于 python

Pygame是跨平台Python模块,专为电子游戏设计。包含图像、声音。

要使用pygame主页下载并安装pygame,windows平台直接安装即可。

我使用的是eclipse环境,然后需要重新设置下引用包[详见python引用第三方包],重启eclipse就可以使用了。

阅读全文 »

python引用第三方包

发表于 2013-07-17 | 更新于 2015-08-15 | 分类于 python

python安装第三方包完成后,Eclipse环境下pyDev并不能识别引用。

具体做法应该是:
重新设置Eclipse中的python解释器。
Preferences->pyDev->Interpreter-python里重新配置Python的编译器,因为当前没有包含新加进来的包

阅读全文 »

python读取配置文件

发表于 2013-07-16 | 更新于 2015-08-15 | 分类于 python

配置文件:

配置文件使用[section]和key-value对来区分;

key:value和key=value都可以;

和;是注释行;

SafeConfigParser类可以实现插值;

[numbers]
pi:3.14159

[messages]
greeting: Welcome to the area calculation program!
question: Please enter the radius:
result_message: The area is:
阅读全文 »

Hello, World

发表于 2013-07-11 | 更新于 2015-08-15 | 分类于 essay

之前的博客代码被SVN搞坏了,重新开个博客。

好好写博,不折腾;节约云豆,希望能长期维持。

第一文,结束。

step by step教你SAE+WP建站

发表于 2013-07-04 | 更新于 2015-08-15 | 分类于 web
step 1:登录[http://sae.sina.com.cn](http://sae.sina.com.cn),在页面右上角注册sinaappengine(过程不再赘述,我的已经注册好)。注册中使用到微博,没有的先去申请个吧。注意一个手机号只能绑定一个sae帐号。
![](http://codeplanet-wordpress.stor.sinaapp.com/uploads/2013/08/wpid-8ad644157c29befa221167fdcf785c54_15273075.png) 
阅读全文 »
1…1112
Lu Xiaohua

Lu Xiaohua

116 日志
33 分类
86 标签
GitHub E-Mail
© 2019 Lu Xiaohua
由 Hexo 强力驱动 v3.8.0
|
主题 – NexT.Muse v7.0.0