C Programer  - 讨论区

标题:APR dbd_mysql 模块编程示例

2014年01月15日 星期三 17:17

APR的dbd模块提供操作关系数据库的功能,目前APR支持多种关系数据库,包括MySQL,PostgreSQL,SQLite等,以下代码示例演示了如何通过APR dbd模块操作MySQL数据库,包括建立连接,Escape操作,Prepared模式,Transaction等。

 

#include <stdio.h>
#include <apr.h>
#include <apr_pools.h>
#include <apr_errno.h>
#include <apr_strings.h>
#include <apr_dbd.h>

void apr_err(const char *s, apr_status_t rv)
{
    char buf[120];
    fprintf(stderr, "%s: %s (%d)\n", s, 
            apr_strerror(rv, buf, sizeof buf), rv); 
}

int main(int argc,char **argv) {
    apr_initialize();
    apr_pool_t *pool;
    apr_pool_create(&pool,NULL);
    apr_status_t st;
    
    apr_dbd_t *db;
    apr_dbd_init(pool);
    const apr_dbd_driver_t *driver=NULL;
    st=apr_dbd_get_driver(pool,"mysql",&driver);
    if(st != APR_SUCCESS) {
        apr_err("apr_dbd_get_driver()",st);
        return st;
    }
    char *params=apr_psprintf(pool,
            "host=%s,port=%d,user=%s,pass=%s,dbname=%s",
            "10.1.9.140",3306,"user","pass","test");
    st=apr_dbd_open(driver,pool,params,&db);
    if(st != APR_SUCCESS) {
        apr_err("apr_dbd_open()",st);
        return st;
    }

    char *fmt="insert into friends (name,email) values ('%s','%s')";
    const char *e_name=apr_dbd_escape(driver,pool,"laomeng",db);
    const char *e_email=apr_dbd_escape(driver,pool,"laomeng188@163.com",db);
    char *sql=apr_psprintf(pool,fmt,e_name,e_email);
    int nrows=0;
    st=apr_dbd_query(driver,db,&nrows,sql);
    printf("nrows = %d\n",nrows);

    apr_dbd_prepared_t *pdt=NULL;
    char *pdq="insert into friends (name,email) values (%s,%s)";
    st=apr_dbd_prepare(driver,pool,db,pdq,NULL,&pdt);
    if(st != APR_SUCCESS) {
        apr_err("apr_dbd_prepare()",st);
        return st;
    }
    apr_dbd_transaction_t *trs=NULL;
    st=apr_dbd_transaction_start(driver,pool,db,&trs);
    st=apr_dbd_pvquery(driver,pool,db,&nrows,pdt,"laoli","laoli@qq.com");
    if(st != APR_SUCCESS) {
        apr_err("apr_dbd_pvquery()",st);
        return st;
    }
    st=apr_dbd_transaction_end(driver,pool,trs);

    char *sel="select name,email from friends";
    apr_dbd_results_t *res=NULL;
    st=apr_dbd_select(driver,pool,db,&res,sel,-1);
    if(st != APR_SUCCESS) {
        apr_err("apr_dbd_select()",st);
        fprintf(stderr, "%s\n", apr_dbd_error(driver,db,st));
        return st;
    }
    nrows=apr_dbd_num_tuples(driver,res);
    printf("rows of result: %d\n",nrows);
    int ncols=apr_dbd_num_cols(driver,res);
    printf("cols of result: %d\n",ncols);
    apr_dbd_row_t *row=NULL;
    for(int i=1; -1 != apr_dbd_get_row(driver,pool,res,&row,i);i++ ) {
        printf("name = %s, email = %s\n",apr_dbd_get_entry(driver,row,0),apr_dbd_get_entry(driver,row,1)); 
    } 

    apr_dbd_close(driver,db);

    apr_pool_destroy(pool);
    apr_terminate();
    return 0;
}

 

参考资料:

http://apr.apache.org/docs/apr-util/1.3/group___a_p_r___util___d_b_d.html

 

如下红色区域有误,请重新填写。

    你的回复:

    请 登录 后回复。还没有在Zeuux哲思注册吗?现在 注册 !

    Zeuux © 2024

    京ICP备05028076号