免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 5408 | 回复: 3
打印 上一主题 下一主题

[C] Linux下C语言连接查询Mysql [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2009-11-20 22:49 |只看该作者 |倒序浏览
c语言连接Mysql的代码

#include <stdio.h>
#include <stdlib.h>
#include "mysql/mysql.h"
/*
* compile with this command on 64bite Linux:
* gcc -g conn.mysql.c -lmysqlclient -L /usr/lib64/mysql/
*/


int main(void){

    MYSQL *my_connection;
    MYSQL *conn_ptr;
    MYSQL_RES *results;
    MYSQL_ROW sqlrow;

    int i;
    char sql[1024];
    sprintf(sql,"select * from tablename");

    const char *host = "localhost";
    const char *user = "your username";
    const char *passwd = "your password";
    const char *dbname = "your database";
    unsigned int port_no = 0;
    const char *socket_name = NULL;
    unsigned int flags = 0;

    my_connection = mysql_init(NULL); //initialized connection handle

    if(my_connection)
    {
        printf("Initializing connection to database...n");
        conn_ptr = mysql_real_connect(my_connection,host,user,passwd,dbname,port_no,socket_name,flags); //making a connection

        if(conn_ptr)
        {
            printf("Connection succeed!n");

//making a query

            int query = mysql_query(my_connection,sql);//0 is returned if succeed.

            if(query)
                fprintf(stderr,"Query failed %d: %sn",mysql_errno(my_connection), mysql_error(my_connection));
            else
            {
                printf("Query succeed...n");
                results = mysql_store_result(my_connection);//mysql_use/store_result()

                if(results)
                {
                    printf("Retrieved %lu rowsn", mysql_num_rows(results));
                    while(sqlrow = mysql_fetch_row(results))
                    {
                        for(i=0;i<mysql_num_fields(results);i++)
                            printf("%dt",atoi(sqlrow[i]));
                        printf("n");
                    }
                    if(mysql_errno(my_connection))
                        fprintf(stderr,"Retrive error: %sn",mysql_error(my_connection));
                }
                mysql_free_result(results);
            }
        }
        else
            fprintf(stderr,"Connecting failed %d: %sn",mysql_errno(my_connection),mysql_error(my_connection));

    }
    else
    {
        fprintf(stderr, "Initializing Mysql failed!n");
        return EXIT_FAILURE;
    }


    //close connection
    mysql_close(my_connection);
    return EXIT_SUCCESS;
}




[ 本帖最后由 飞行员舒克 于 2009-11-20 22:55 编辑 ]

论坛徽章:
5
未羊
日期:2014-08-07 15:42:10双子座
日期:2014-09-23 15:42:172015年辞旧岁徽章
日期:2015-03-03 16:54:152015年迎新春徽章
日期:2015-03-04 09:55:282022北京冬奥会纪念版徽章
日期:2015-08-10 16:30:32
2 [报告]
发表于 2009-11-20 23:19 |只看该作者
哈哈,俺来个windows下连接mysql

  1. #include <stdio.h>
  2. #include <mysql.h>
  3. #include <mysql_com.h>
  4. #include <mysql_version.h>
  5. #include <my_alloc.h>


  6. #define tableName "user_t"
  7. main()
  8. {
  9. char mysqlServer[20] = "localhost";
  10. char query[300];
  11. MYSQL *conn;
  12. MYSQL_RES *res;
  13. MYSQL_FIELD *fd;
  14. MYSQL_ROW row;
  15. int rowCount = 0;
  16. int colCount = 0;
  17. int i, j;  


  18.     conn = mysql_init(NULL);
  19.    //连接数据库mysql
  20.    if (!mysql_real_connect(conn,"localhost", "root", "root", "mydb",3306,NULL,0))
  21.     {
  22.     printf("connect mysql error!\n");
  23.     return 0;
  24.     }
  25.     //==================================
  26.    
  27.     sprintf(query,"select * from %s",tableName);
  28.     if( mysql_query(conn, query) != 0 )
  29.     {
  30.     printf("query error!\n");
  31.     return 0;
  32.     }else
  33.         {
  34.             res = mysql_store_result(conn);
  35.             rowCount = (int) mysql_num_rows( res );
  36.             colCount = (int) mysql_num_fields( res );
  37.             printf(" result: %d records found\n fields: %d \n", rowCount, colCount);
  38.             row = mysql_fetch_row( res );
  39.             for(i = 0; i < rowCount; i++)
  40.             {
  41.                 printf(" show: ");
  42.                 for( j = 0; j < colCount; j++)
  43.                 {
  44.                    printf("[ %s ] ", row[j] );
  45.                 } // end for
  46.                 printf(" \n ");
  47.             } // end for
  48.         }
  49.         mysql_free_result(res);    //释放结果集
  50.     mysql_close(conn);

  51. //return 0;
  52. }
复制代码

论坛徽章:
0
3 [报告]
发表于 2009-11-21 00:07 |只看该作者

论坛徽章:
0
4 [报告]
发表于 2009-11-21 05:50 |只看该作者
收了,哈哈
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP