Chinaunix首页 | 论坛 | 博客
  • 博客访问: 275696
  • 博文数量: 113
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 1044
  • 用 户 组: 普通用户
  • 注册时间: 2015-02-15 16:09
文章分类

全部博文(113)

文章存档

2016年(5)

2015年(108)

我的朋友

分类: C/C++

2015-10-08 23:14:08


点击(此处)折叠或打开

  1. #include <stdio.h>
  2. #include <mysql/mysql.h>

  3. int mian(int argc,char*argv[]){
  4.     MYSQL mysql;
  5.     char *query="UPDATE book SET title=UPPER(title)";

  6.     mysql_init(&mysql);

  7.     /*connect to server*/
  8.     if(!mysql_real_connect(&mysql,"localhost","root","1qaz2wsx","test",0,NULL,0)){
  9.         fprintf(stderr,"Failed to connect to database.Error :%s\n",mysql_error(&mysql));
  10.         exit(1);
  11.     }

  12. /*mysql_affect_rows 返回最后一次查询所影响的行数*/
  13.     /*update some rows*/
  14.     if(mysql_query(&mysql,query)){
  15.         fprintf(stderr,"Query failed:%s\n",mysql_error(&mysql));
  16.     }
  17.     else {
  18.         printf("%d titles changed.",mysql_affect_rows(&mysql));
  19.     }



  20.     /*mysql_change_user更改登录用户和数据库*/
  21.     /*keep connection ,but remove association to user*/
  22.     if(mysql_change_user(&mysql,"adustdu","1qaz2wsx","TEST1")){
  23.         fprintf(stderr,"Failed to chnage user .Error :%s\n",mysql_error(&mysql));
  24.     }
  25.     /*mysql_character_set_name返回指定连接的默认字符集的名称*/
  26.     /*这里没有给出该函数的具体用法*/
  27.     /*close connection*/
  28.     mysql_close(&mysql);
  29. }

点击(此处)折叠或打开

  1. #include<stdio.h>
  2. #include <mysql/mysql.h>
  3. /*mysql_change_user更改登录用户和数据库*/
  4. int main(){
  5.     /*mysql_data_seek跳转到指定行,匹配和mysql_store_result使用,获得该行内容*/
  6.     MYSQL mysql;
  7.     MYSQL_RES* result;
  8.     MYSQL_ROW row;
  9.     char *query="SELECT State FROM tax ORDER BY State";
  10.     mysql_init(&mysql);
  11.     mysql_real_connect(&mysql,"lcoalhost","adustdu","1qaz2wsx","TEST1");

  12.     /*mysql_errno生成错误编码,用作调试*/
  13.     if(mysql_query(&mysql,query)){
  14.         fprintf(stderr,"Query failed :(%d)%s\n",mysql_errno(&mysql),mysql_error(&mysql));
  15.     }
  16.     else {
  17.         /*get result in buffer */
  18.         result =mysql_store_result(&mysql);
  19.         /*Jump to 15th row*/
  20.         mysql_data_seek(result,14);
  21.         /*get row*/
  22.         row=mysql_fetch_row(result);
  23.         /*print state name*/
  24.         printf("%s\n",row[0]);


  25.         /*free result*/
  26.         mysql_free_result(result);
  27.         mysql_close(&mysql);

  28.     }
  29. }

点击(此处)折叠或打开

  1. #include <stdio.h>
  2. #include <mysql/mysql.h>
  3. /*mysql_fetch_field提取结果集中有关列的信息,一次一列*/
  4. int mian(int argc,char*argv[]){
  5.     MYSQL mysql;
  6.     MYSQL_RED*result;
  7.     MYSQL_FIELD*field;
  8.     mysql_init(&mysql);

  9.     /*connect to server*/
  10.     if(!mysql_real_connect(&mysql,"localhost","root","1qaz2wsx","test",0,NULL,0)){
  11.         fprintf(stderr,"Failed to connect to database.Error :%s\n",mysql_error(&mysql));
  12.         exit(1);
  13.     }
  14.     mysql_query(&mysql,"SELECT * FROM tax");
  15.     result=mysql_use_result(&mysql);
  16.     /*get information about fields*/
  17.     while((field=mysql_fetch_field(result))){
  18.         printf("Name:%s\n",field->name);
  19.         printf("Table:%s\n",field->table);
  20.         printf("Default value:%s\n\n",field->def);
  21.     }
  22.     /*close connection*/
  23.     mysql_close(&mysql);
  24. }



点击(此处)折叠或打开

  1. if(mysql_query(&mysql,"SELECT User ,Host FROM user ORDER BY 1,2"))
  2.     //ORDER BY 1,2意思是先按第一列排序.如果第一列相同的,按第二列降序排序..


阅读(1362) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~