如何将c程序连接到mysql

时间:2013-11-04 11:49:28

标签: mysql c

我只是在学习mysql。我在这里编写了非常简单的程序。我需要将它连接到mysql数据库。我的意思是说输出必须存储在mysql数据库中。我该怎么做。我提到了mysql教程,但我找不到答案。所以想到将开始实施一个简单的程序。请有人指导我。谢谢

#include<stdio.h>
int main( ) 
{ 
int num1, num2, sum; 
printf("Enter a two integers: "); 
scanf("%d %d",&num1,&num2); num1=num1+num2; 
printf("Sum: %d",num1); 
return 0;
} 

2 个答案:

答案 0 :(得分:1)

#include <mysql.h>
#include <stdio.h>
main() {
   MYSQL *conn;
   MYSQL_RES *res;
   MYSQL_ROW row;
   char *server = "localhost";
   char *user = "root";
   char *password = "PASSWORD"; /* set me first */
   char *database = "mysql";
   conn = mysql_init(NULL);
   /* Connect to database */
   if (!mysql_real_connect(conn, server,
         user, password, database, 0, NULL, 0)) {
      fprintf(stderr, "%s\n", mysql_error(conn));
      exit(1);
   }
   /* send SQL query */
   if (mysql_query(conn, "show tables")) {
      fprintf(stderr, "%s\n", mysql_error(conn));
      exit(1);
   }
   res = mysql_use_result(conn);
   /* output table name */
   printf("MySQL Tables in mysql database:\n");
   while ((row = mysql_fetch_row(res)) != NULL)
      printf("%s \n", row[0]);
   /* close connection */
   mysql_free_result(res);
   mysql_close(conn);
}

详细解释请参阅此处http://www.cyberciti.biz/tips/linux-unix-connect-mysql-c-api-program.html

http://dev.mysql.com/downloads/connector/c/

答案 1 :(得分:1)

这是我刚刚写过的一个程序的复制粘贴(或多或少),它使用他们的C API连接到MySQL:

#include <stdio.h>
#include <stdlib.h>
#include <my_global.h>
#include <mysql.h>

typedef struct
{
    char host[20];
    char user[25];
    char pass[50];
}DB_CONN_PARAMS;

MYSQL * connect_db(DB_CONN_PARAMS *params)
{
    MYSQL *connection = mysql_init(NULL);//init connection
    if (connection == NULL)
    {//check init worked
        fprintf(stderr, "%s\n", mysql_error(connection));
        exit(EXIT_FAILURE):
    }
    //connect:
    if (mysql_real_connect(
            connection,
            params->host,
            params->user,
            params->pass,
            NULL,0,NULL,0)
        ==NULL)
    {//connection failed?
        fprintf(stderr, "%s\n", mysql_error(connection));
        mysql_close(connection);
        exit(EXIT_FAILURE):
    }
    return connection;
}

int main()
{
    MYSQL *db;
    DB_CONN_PARAMS *params = calloc(1,sizeof(DB_CONN_PARAMS));
    //just an alternative way of passing connection params, find a struct easier
    params->host = "127.0.0.1";
    params->user = "root";
    params->pass = "mySuperSecretPass";
    MYSQL * connect_db(DB_CONN_PARAMS *params);
    db = connect_db(params);
    //we don't need the struct anymore
    free(params);
    params = NULL;
    //do stuff
    mysql_close(db);//close connection, of course!
    return EXIT_SUCCESS;
}

我从the code samples on the MySQL website获取了大部分灵感。他们确实工作,诚实 编译时,你必须记住一些事情。上面的代码应该使用gcc编译就好了:

gcc yourCode.c -std=c99 `mysql_config --cflags --libs` -o yourBin

在执行查询时,我建议您将The C-API documentation on mysql.com添加为书签 如果不使用预准备语句,您的代码最终可能会看起来像:

int main()
{
    MYSQL *db;
    char query_str[150];//query string here
    int num1, num2, q_status;//your ints, + 1 to check success of query
    DB_CONN_PARAMS *params = calloc(1,sizeof(DB_CONN_PARAMS));
    params->host = "127.0.0.1";
    params->user = "root";
    params->pass = "mySuperSecretPass";
    MYSQL * connect_db(DB_CONN_PARAMS *params);
    db = connect_db(params);
    free(params);
    //get numbers
    scanf("%d %d", num1, num2);
    //insert num1, num2 and sum into query string
    sprintf(query_str,
            "INSERT INTO your_db.table (num1, num2, sum) VALUES (%d,%d,%d);",
            num1,num2,(num1 + num2)
    );
    //run our query
    q_status = mysql_query(db, query_str);
    if (q_status)
    {//insert failed
        fprintf(stderr, "Failed to execute \"%s\"\n%s\n", query_str, mysql_error(db));
        mysql_close(db);
        exit(EXIT_FAILURE);
    }
    //YaY, it worked!
    printf("Insert successful:\nQuery used: \"%s\"\n", query_str);
    mysql_close(db);
    return EXIT_SUCCESS;
}

但是这会遗漏结果集,准备好的陈述以及在现实生活中有用的所有其他事情。请在我的回答中浏览我链接到的文档,这不难理解,有时候只是一点点,MySQL C API ......