dmysql一段命令
下面贴一个mysql的库,叫做dmysql解压缩以后,make ; make install 安装
然后编译程序的时候,包含dmysql.h头文件,加上 -ldmysql标识,即可
头里面定义了一个mysql数据库的结构体,
typedef struct _dmysql_info
{
char *host; /*host for database*/
char *user; /*user name for database*/
char *pswd; /*password to the account*/
char *dbbs; /*name of database*/
} dmysql_info;
记录集的数据类型
typedef struct _dmysql_record
{
char *s_str;
} dmysql_record;
typedef struct _dmysql_row
{
dmysql_record *s_record;
} dmysql_row;
typedef struct _d_mysql_res
{
int row;
int field;
dmysql_row *s_row;
} dmysql_res;
还有程序运行时候的出错代码
#define DMYSQL_QUERY_OKAY 0
#define DMYSQL_SELECT_OKAY 0
#define DMYSQL_CONNECT_ERROR -1
#define DMYSQL_INIT_ERROR -2
#define DMYSQL_QUERY_ERROR -3
#define DMYSQL_RES_ERROR -4
程序一共就3个函数
extern int dmysql_query( const char * s_query, const dmysql_info mysql_info );
给出一句执行的SQL命令,比如UPDATE、INSERT,指定一个数据库,就可以了
extern int dmysql_select( dmysql_res **rel_res, const char *s_select, const dmysql_info mysql_info );
传入一个数据库记录集的地址,给出一个SELECT记录的SQL命令,再制定一个数据库,就可以了
用来释放内存中在栈里面申明了的记录集空间
extern void dmysql_free( dmysql_res *res );
下面是演示的程序代码:
#include
#include
int main( void )
{
dmysql_info db;
db.host="127.0.0.1"; /*host for database*/
db.user="root"; /*user name for database*/
db.pswd=""; /*password to the account*/
db.dbbs="mysql"; /*name of database*/
char *s_query="GRANT ALL ON *.* to 'dorainm'@'127.0.0.1' IDENTIFIED BY '******';";
char *s_sql="SELECT `user`,`host`,`password` FROM `user`;";
int i, j;
dmysql_res *res;
dmysql_query( s_query, db );
printf( "select out : %d\n", dmysql_select( &res, s_sql, db ) );
for( i=0; irow; i++ )
{
for( j=0; jfield; j++ )
{
printf("%s\t", ((res->s_row+i)->s_record+j)->s_str );
}
printf( "\n" );
}
dmysql_free( res );
return 0;
};
make test后运行./dmysql
dorainm@desktop:~/workroom/c/mylib/dmysql$ make test gcc -Wall -O3 -o dmysql main.c -ldmysql `mysql_config --libs` `mysql_config --cflags` dorainm@desktop:~/workroom/c/mylib/dmysql$ ./dmysql
select out : 5
root localhost %^$#!@%*&!
root desktop.dorainm.org %^$#!@%*&!
desktop.dorainm.org %^$#!@%*&!
localhost %^$#!@%*&!
dorainm 127.0.0.1 %^$#!@%*&! dorainm@desktop:~/workroom/c/mylib/dmysql$
页:
[1]