apr-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From liuxulin <liuxu...@gmail.com>
Subject About DBD Prepare Statement Problem
Date Wed, 27 Jan 2010 05:40:06 GMT
hi everybody,

I've got some problems, hope for your helps.

  1.
  When i try to connect Mysql with 'apr-util DBD' , But get some errors.
  After executing 'apr_dbd_pvselect', I got the result 'empty value'.
  But i can get correct result with the 'apr_dbd_select' method.

  in my code:
  entry = apr_dbd_get_entry(driver, row, n);
    but I got nothing.

  How to fix the 'empty value' problem?

  2.
  I try to query "select * from ..." but got a "Exec of prepared 
statement failed!"
  But "select userName from ..." makes the right results.

  Is there anything wrong?


Thanks for your helps and replies!

My code is as follows:

Environment:
  apr-util : 1.3.9
  ubuntu   : 9.04
  mysql    : 5.0.75
  TABLE  Structure:
  CREATE TABLE `testTable` {
   `id` int(11) NOT NULL,
   `userName` varchar(100) NOT NULL,
   PRIMARY KEY (`id`)
  }ENGINE=MyISAM DEFAULT CHARSET=latin1;
     Sourceļ¼š
#include "apu.h"
#include "apr_pools.h"
#include "apr_dbd.h"

#include <stdio.h>

int main(int argc, char **argv) {
   const char *name = "mysql";
   const char *params =
           "host=127.0.0.1,user=root,pass=123456,dbname=testDB,port=3306";
   apr_pool_t *pool = NULL;
   apr_dbd_t *sql = NULL;
   const apr_dbd_driver_t *driver = NULL;
   int rv;

   const char *query = "SELECT  userName  FROM testTable where 
id=%d";/*this query exec info:ok!!!*/
   //const char *query = "SELECT * FROM testTable where id=%d";/*this 
query exec info:"Exec of prepared statement failed!"???*/
   const char *label = "getUserName";
   apr_dbd_prepared_t *statement = NULL;
   apr_dbd_results_t *res = NULL;
   apr_dbd_row_t *row = NULL;
   const char *entry = NULL;
   int i, n;

   apr_initialize();
   apr_pool_create(&pool, NULL);

   apr_dbd_init(pool);
   rv = apr_dbd_get_driver(pool, name, &driver);

   switch (rv) {
   case APR_SUCCESS:
       printf("Loaded %s driver OK.\n", name);
       break;
   case APR_EDSOOPEN:
       printf("Failed to load driver file apr_dbd_%s.so\n", name);
       goto finish;
   case APR_ESYMNOTFOUND:
       printf("Failed to load driver apr_dbd_%s_driver.\n", name);
       goto finish;
   case APR_ENOTIMPL:
       printf("No driver available for %s.\n", name);
       goto finish;
   default: /* it's a bug if none of the above happen */
       printf("Internal error loading %s.\n", name);
       goto finish;
   }

   rv = apr_dbd_open(driver, pool, params, &sql);
   switch (rv) {
   case APR_SUCCESS:
       printf("Opened %s[%s] OK\n", name, params);
       break;
   case APR_EGENERAL:
       printf("Failed to open %s[%s]\n", name, params);
       goto finish;
   default: /* it's a bug if none of the above happen */
       printf("Internal error opening %s[%s]\n", name, params);
       goto finish;
   }

   rv = apr_dbd_prepare(driver, pool, sql, query, label, &statement);
   if (rv) {
       printf("Prepare statement failed!\n%s\n",
               apr_dbd_error(driver, sql, rv));
       return rv;
   }

   int user_id = 1;
   rv
           = apr_dbd_pvselect(driver, pool, sql, &res, statement, 0, 
&user_id,
                   NULL);

   if (rv) {
       printf("Exec of prepared statement failed!\n%s\n", apr_dbd_error(
               driver, sql, rv));
       return rv;
   }

   i = 0;
   for (rv = apr_dbd_get_row(driver, pool, res, &row, -1); rv == 0; rv
           = apr_dbd_get_row(driver, pool, res, &row, -1)) {
       printf("ROW %d:    ", ++i);
       for (n = 0; n < apr_dbd_num_cols(driver, res); ++n) {
           entry = apr_dbd_get_entry(driver, row, n);
           if (entry == NULL) {
               printf("(null)    ");
           } else {
               printf("%s    ", entry);/*printf Nothing???*/
           }
       }
       fputs("\n", stdout);
   }

   finish:
       printf("finish");
       apr_pool_destroy(pool);
       apr_terminate();              
       return 0;
}


Mime
View raw message