|
用gcc开发mysql数据库(mysql c api)
/*#define mysql_field_count mysql_num_fields*/
#include <stdio.h>
#include <string.h>
#include <mysql.h>
#define def_host_name "localhost"
#define def_user_name "root"
#define def_password NULL
#define def_db_name "dbtest"
MYSQL *conn;
void process_result_set(MYSQL *conn,MYSQL_RES *res_set)
{
MYSQL_ROW row;
unsigned int i;
while ((row = mysql_fetch_row (res_set)) != NULL)
{
for (i = 0; i< mysql_num_fields (res_set); i++)
{
if (i>0)
fputc('\t',stdout);
printf("%s",row != NULL ? row : "NULL");
}
fputc ('\n',stdout);
}
if (mysql_errno (conn) !=0)
printf ("mysql_fetch_row() failed");
else
printf("%lu rows returned\n",(unsigned long) mysql_num_rows (res_set));
}
void process_query(MYSQL * conn,char *query)
{
MYSQL_RES *res_set;
unsigned int field_count;
if(mysql_query (conn,query) != 0)
{printf("process_query() failed");
return;
}
res_set=mysql_store_result(conn);
if (res_set == NULL)
{
if (mysql_field_count (conn) > 0)
{ printf("problem processing result set");}
else
{ printf ("%lu rows affected\n", (unsigned long) mysql_affected_rows(conn));}
}
else
{process_result_set(conn,res_set);
mysql_free_result(res_set);
}
}
int main (int argc,char *argv[])
{
conn=mysql_init(NULL);
mysql_real_connect(
conn,
def_host_name,
def_user_name,
def_password,
def_db_name,
0,
NULL,
0);
process_query(conn,"select * from xj");
mysql_close(conn);
exit(0);
}
编译:
gcc -c -I/usr/local/mysql/include myclient.c
连接:
gcc -o myclient myclient.o -L/usr/local/mysql/lib -lmysqlclient -lm
在Free BSD,mysql-standard-4.1.7下,测试通过。 |
|