Chinaunix首页 | 论坛 | 博客
  • 博客访问: 2396856
  • 博文数量: 328
  • 博客积分: 4302
  • 博客等级: 上校
  • 技术积分: 5486
  • 用 户 组: 普通用户
  • 注册时间: 2010-07-01 11:14
个人简介

悲剧,绝对的悲剧,悲剧中的悲剧。

文章分类

全部博文(328)

文章存档

2017年(6)

2016年(18)

2015年(28)

2014年(73)

2013年(62)

2012年(58)

2011年(55)

2010年(28)

分类: C/C++

2013-07-06 11:13:06

1. 安装相关包

先安装mysql
sudo apt-get install mysql-server mysql-client

再装开发包
sudo apt-get install libmysqlclient15-dev

2. 测试安装结果

编译方法:
gcc $(mysql_config --cflags) xxx.c -o xxx $(mysql_config --libs)

  1. /* Simple C program that connects to MySQL Database server*/
  2. #include <mysql.h>
  3. #include <stdio.h>

  4. main() {
  5.     MYSQL *conn;
  6.     MYSQL_RES *res;
  7.     MYSQL_ROW row;

  8.    char *server = "localhost";
  9.    char *user = "root";
  10.    char *password = ""; /* 此处改成你的密码 */
  11.    char *database = "mysql";

  12.     conn = mysql_init(NULL);

  13.    /* Connect to database */
  14.    if (!mysql_real_connect(conn, server,
  15.           user, password, database, 0, NULL, 0)) {
  16.       fprintf(stderr, "%s\n", mysql_error(conn));
  17.       exit(1);
  18.    }

  19.    /* send SQL query */
  20.    if (mysql_query(conn, "show tables")) {
  21.       fprintf(stderr, "%s\n", mysql_error(conn));
  22.       exit(1);
  23.    }

  24.     res = mysql_use_result(conn);

  25.    /* output table name */
  26.    printf("MySQL Tables in mysql database:\n");
  27.    while ((row = mysql_fetch_row(res)) != NULL)
  28.       printf("%s \n", row[0]);

  29.    /* close connection */
  30.     mysql_free_result(res);
  31.     mysql_close(conn);
  32. }

3. 操作数据库

a 创建表并插入数据

  1. conn = mysql_init(NULL);
  2.   mysql_real_connect(conn, "localhost", "zetcode", "passwd", "testdb", 0, NULL, 0);
  3.   mysql_query(conn, "CREATE TABLE writers(name VARCHAR(25))");

  4.   char sql[100];
  5.   sprintf(sql,"INSERT INTO ZFile VALUES(%u,%u,%u)",inta,intb,intc);
      mysql_query(conn, sql);
  6.   mysql_query(conn, "INSERT INTO writers VALUES('Jack London')");
  7.   mysql_query(conn, "INSERT INTO writers VALUES('Honore de Balzac')");

b 查询数据

  1. int main(int argc, char **argv)
  2. {
  3.   MYSQL *conn;
  4.   MYSQL_RES *result;
  5.   MYSQL_ROW row;
  6.   int num_fields;
  7.   int i;

  8.   conn = mysql_init(NULL);
  9.   mysql_real_connect(conn, "localhost", "zetcode", "passwd", "testdb", 0, NULL, 0);
  10.   mysql_query(conn, "SELECT * FROM writers");
  11.   result = mysql_store_result(conn);
  12.   num_fields = mysql_num_fields(result);
  13.   while ((row = mysql_fetch_row(result)))
  14.   {
  15.       for(i = 0; i < num_fields; i++)
  16.       {
  17.           printf("%s ", row[i] ? row[i] : "NULL");
  18.       }
  19.       printf("\n");
  20.   }
  21.   mysql_free_result(result);
  22.   mysql_close(conn);
  23. }

c 获取列头

  1. #include <mysql.h>
  2. int main(int argc, char **argv)
  3. {
  4.   MYSQL *conn;
  5.   MYSQL_RES *result;
  6.   MYSQL_ROW row;
  7.   MYSQL_FIELD *field;
  8.  
  9.   int num_fields;
  10.   int i;
  11.   conn = mysql_init(NULL);
  12.   mysql_real_connect(conn, "localhost", "zetcode", "passwd", "testdb", 0, NULL, 0);
  13.   mysql_query(conn, "SELECT * FROM friends");
  14.   result = mysql_store_result(conn);
  15.   num_fields = mysql_num_fields(result);
  16.   while ((row = mysql_fetch_row(result)))
  17.   {
  18.       for(i = 0; i < num_fields; i++)
  19.       {
  20.           if (i == 0) {
  21.              while(field = mysql_fetch_field(result)) {
  22.                 printf("%s ", field->name);
  23.              }
  24.           printf("\n");
  25.           }
  26.          printf("%s ", row[i] ? row[i] : "NULL");
  27.       }
  28.   }
  29.   printf("\n");
  30.   mysql_free_result(result);
  31.   mysql_close(conn);
  32. }

d 使用事务

如果插入的数据比较多,可以使用事务来批量处理数据库操作。
  1.    if (mysql_autocommit(conn_ddt, 0)) {
  2.       fprintf(stderr, "%s\n", mysql_error(conn_ddt));
  3.       exit(1);
  4.    }

  5.    while(1){
  6.       char * sql = "some operation";
  7.       mysql_query(conn_ddt,sql );
  8.    }

  9.    if (mysql_commit(conn_ddt)) {
  10.       fprintf(stderr, "%s\n", mysql_error(conn_ddt));
  11.       exit(1);
  12.    }
一段C代码,初始化一段内存,然后动态扩大。
  1. unsigned int nMax = 1024;
  2. unsigned int *w, *d;

  3. void init() {
  4.     w = malloc(nMax * sizeof(*w));
  5.     d = malloc(nMax * sizeof(*d));
  6. // memset(w,0,nMax * sizeof(unsigned int));
  7.     for (p = 0; p < nMax; p++) {
  8.         w[p] = 0;
  9.         d[p] = 0;
  10.     }
  11. }

  12. void resizeNarrays() {
  13.     int i, nMaxOld;
  14.     nMaxOld = nMax;
  15.     nMax = N*2;
  16.     if (verbose)
  17.         printf("Increasing NMAX to %d.\n", nMax);
  18.     w = realloc(w, nMax * sizeof(*w));
  19.     d = realloc(d, nMax * sizeof(*d));
  20.     for (i = nMaxOld; i < nMax; i++) {
  21.         w[i] = -1;
  22.         d[i] = -1;
  23.     }
  24. }

  25. void main() {
  26.     while(something){
  27.         d[N-1] = some;
  28.         if (N >= nMax)
  29.             resizeNarrays();
  30.     }
  31. }

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