1

我用mysql用c语言写了一个程序。它不好用。

我创建了一个连接函数来帮助我连接 mysql

MYSQL *ts_mysql_connect(char *host, char *user, char *pass, char *database)
{

MYSQL *conn = mysql_init(NULL);
fprintf(stderr, "[ts_mysql_connect] conn-> %ld\n", conn);
if (!mysql_real_connect(conn, host, user, pass, database, 0, NULL, 0)){
    fprintf(stderr, "%s\n", mysql_error(conn));
    exit(1);
}

//set auto commit to false
if (mysql_autocommit(conn, 0))
    fprintf(stderr, "%s\n", "SET MYSQL autocommit to off faild!");
fprintf(stderr, "[ts_mysql_connect] conn-> %ld\n", conn);
return conn;

 }

我使用下面的代码来调用我的函数

    MYSQL *test;
    test = ts_mysql_connect(conf->run_conf->mysql_host,
                                  conf->run_conf->mysql_user,
                                  conf->run_conf->mysql_pass,
                                  conf->run_conf->mysql_database);
    conf->mysql_start = time(NULL);
    if (verbose)
        fprintf(stderr, "[ts_mysql_insert] mysql conn init at %ld\n",
                                             &test);
    exit(1);

我不知道为什么ts_mysql_connect返回 conn 的不同地址。

运行日志:

[ts_mysql_connect] conn-> 140065527302336
[ts_mysql_connect] conn-> 140065527302336
[ts_mysql_insert] mysql conn init at -1946154816

那么为什么MYSQL *conn在 [ts_mysql_insert] 和 [ts_mysql_connect] 有不同的地址

4

2 回答 2

0

您没有ts_mysql_connect在第二个程序中声明函数原型,因此编译器假定该函数返回一个int类型。

如果您使用-Wall指令启用所有警告,您将看到如下警告消息:

warning: implicit declaration of function ‘ts_mysql_connect’
于 2013-06-19T06:27:02.940 回答
0
#include <stdio.h>
#include <stdlib.h>
#include <my_global.h>
#include <mysql.h>

MYSQL *DatabaseConnection;
const char *ServerName = "192.168.1.12";
const char *User = "dbuser";
const char *Password = "dbpassword";
const char *Name = "test";
const char *DB_Socket_Path = "/var/run/mysqld/mysql.sock";

int execute_db_query(const char *sql_query)

{
    MYSQL_RES *result = NULL;
    MYSQL_ROW row;
    int num_fields;
    int i;

    if (!DatabaseConnection)
        return -1;

    mysql_query(DatabaseConnection, sql_query);

    result = mysql_store_result(DatabaseConnection);

    if(result == NULL)
    {
        mysql_free_result(result);
                return -1;
    }

    if (!result->eof) {
        mysql_free_result(result);
        return -1;
    }

    num_fields = mysql_num_fields(result);

    int f_count = result->field_count;
    int index =1 ;
    while ((row = mysql_fetch_row(result))) {
        for (i = 0; i < num_fields; i++) {
            printf("%s ", row[i] ? row[i] : "NULL");

        }
        printf("\n");
        index++;

    }


    mysql_free_result(result);

    return 0;
}
void close_db_connection() {
    mysql_close(DatabaseConnection);
    mysql_library_end();
}
int init_connect() {

    DatabaseConnection = mysql_init(NULL);
    printf("MySQL client version: %s\n", mysql_get_client_info());

    if (!mysql_real_connect(DatabaseConnection, ServerName, User, Password,
            Name, 0, DB_Socket_Path, 0)) {
        puts(mysql_error(DatabaseConnection));
        close_db_connection();

        return -1;
    }

    printf("Host : %s \n", mysql_get_host_info(DatabaseConnection));
    printf("Server : %s: \n", mysql_get_server_info(DatabaseConnection));
    printf("Protocol : %d\n", mysql_get_proto_info(DatabaseConnection));

    return 0;
}

int main(void) {
    puts("SQL Example"); 
    init_connect() ;
    execute_db_query("select * from test");
    return EXIT_SUCCESS;
}

我看到您使用的是无套接字连接,因此在这种情况下您可以传递 NULL 。

于 2013-06-19T06:50:11.940 回答