您的位置:首页 > 数据库 > MySQL

C语言访问MYSQL数据库的完整的代码例子

2012-12-05 11:16 393 查看
1、手写安装带mysql sdk 的mysql

2、新建控制台项目,项目属性中把

C:\Program Files\MySQL\MySQL Server 5.5\include 加入“包含目录”

把C:\Program Files\MySQL\MySQL Server 5.5\lib 加入“库目录”

3、stdafx.h中加入

#include <winsock.h> //注意顺序,要放在mysql.h前

#include <mysql.h>//控制台项目中要在mysql.h之前include <winsock.h>

//注意lib添加到“库目录”中,而不是“引用目录”中

#pragma comment(lib, "libmysql.lib")

4、把libmysql.dll放到生成的exe目录下

5、主要的几个例子:

//执行基本查询

void test1()

{

MYSQL *pConn;

pConn = mysql_init(NULL);

//第2、3、4、5参数的意思分别是:服务器地址、用户名、密码、数据库名,第6个为mysql端口号(0为默认值3306)

if(!mysql_real_connect(pConn,"localhost","root","root","test",0,NULL,0))

{  

  printf("无法连接数据库:%s",mysql_error(pConn));

  return;

}

mysql_query(pConn,"set names gbk");//防止乱码。设置和数据库的编码一致就不会乱码

//SET NAMES x 相当于 SET character_set_client = x;SET character_set_results = x;SET character_set_connection = x;

//写set character set gbk;查询不会乱码,但是参数化插入会报错。而set names gbk则都不会乱码

//mysql_real_query比mysql_query多了个参数: 字符串query的长度, 所以适合有二进制数据的query, 而mysql_query的字符串query不能包含二进制,因为它以\0为结尾

//mysql_query() 不能传二进制BLOB字段,因为二进制信息中的\0会被误判为语句结束。 mysql_real_query() 则可以。

if(mysql_query(pConn,"select * from persons"))

{

  printf("查询失败:%s",mysql_error(pConn));

  return;

}

//mysql_store_result是把查询结果一次性取到客户端的离线数据集,当结果比较大时耗内存。

//mysql_use_result则是查询结果放在服务器上,客户端通过指针逐行读取,节省客户端内存。但是一个MYSQL*连接同时只能有一个未关闭的mysql_use_result查询

MYSQL_RES *result = mysql_store_result(pConn);

MYSQL_ROW row;

while(row = mysql_fetch_row(result))

{

  printf("%s %s\n",row[1],row[2]);

}

mysql_free_result(result);

mysql_close(pConn);

}

//获得更新行数

void test2()

{

MYSQL *pConn;

pConn = mysql_init(NULL);

if(!mysql_real_connect(pConn,"127.0.0.1","root","root","test",0,NULL,0))

{

  printf("无法连接数据库:%s",mysql_error(pConn));

  return;

}

if(mysql_query(pConn,"update persons set Age=Age+1"))

{

  printf("执行失败:%s",mysql_error(pConn));

  return;

}

printf("更新成功,共更新完成%d条",mysql_affected_rows(pConn));

mysql_close(pConn);

}

//获得自增id

void test3()

{

MYSQL *pConn;

pConn = mysql_init(NULL);

if(!mysql_real_connect(pConn,"127.0.0.1","root","root","test",0,NULL,0))

{

  printf("无法连接数据库:%s",mysql_error(pConn));

  return;

}

mysql_query(pConn,"set names gbk");

if(mysql_query(pConn,"insert into persons(Name,Age) values('传智播客',100)"))

{

  printf("执行insert失败%s",mysql_error(pConn));

  return;

}

printf("执行insert成功,新id=%d",mysql_insert_id(pConn));

mysql_close(pConn);

}

//参数化查询

void test4()

{

MYSQL* pConn;

pConn = mysql_init(NULL);

if(!mysql_real_connect(pConn,"127.0.0.1","root","root","test",0,NULL,0))

{

  printf("数据库连接失败:%s",mysql_error(pConn));

  return;

}

mysql_query(pConn,"set names gbk");

MYSQL_STMT    *stmt;   

    MYSQL_BIND    bind[2];   

memset(bind,0,sizeof(bind));//把is_null、length等字段默认值设置为NULL等默认值,否则执行会报错

stmt = mysql_stmt_init(pConn);  

char* insertSQL="insert into persons(Name,Age) values(?,?)";

    if (mysql_stmt_prepare(stmt, insertSQL, strlen(insertSQL)))   

    {   

        fprintf(stderr, " mysql_stmt_prepare(), INSERT failed,%s\n",mysql_error(pConn));

        return;   

    }   

bind[0].buffer_type= MYSQL_TYPE_STRING;   

    bind[0].buffer= "黑马";   

    bind[0].buffer_length= strlen("黑马"); //如果设定了buffer_length,则可以不试用length

   

    int age=3;

bind[1].buffer_type= MYSQL_TYPE_LONG;   

    bind[1].buffer= &age;  

bind[1].buffer_length = sizeof(age);

     

    if (mysql_stmt_bind_param(stmt, bind))   

    {   

        fprintf(stderr, " mysql_stmt_bind_param() failed %s\n", mysql_stmt_error(stmt));   

        return;

    }   

   

    if (mysql_stmt_execute(stmt))   

    {   

        fprintf(stderr, " mysql_stmt_execute(), failed %s\n", mysql_stmt_error(stmt));   

        return;   

    }   

mysql_stmt_close(stmt);

mysql_close(pConn);  

printf("参数化执行SQL结束");

}

源:http://www.rupeng.com/forum/thread-39423-1-1.html  
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: