0

我打电话mysql_select_db(),它没有返回任何错误,但是当我打电话时,mysql_query()我得到了以下错误:

未选择数据库。

我找到了一个 MYSQL 结构的参数。这是一些 char* 调用 db。它代表什么? mysql_select_db()是否更改此参数?

谢谢。

编辑:

我的主要():

#include t-mysql.h
...
  T_MYSQL *db = (T_MYSQL*) malloc(sizeof(T_MYSQL));

  strcpy(db->database_name,mysql_database_name);
  strcpy(db->password,mysql_password);
  strcpy(db->root_password,mysql_root_password);
  strcpy(db->server,mysql_server);
  strcpy(db->table_name,mysql_table_name);
  strcpy(db->user,mysql_user);
  strcpy(db->query_create_db,query_create_db);
  strcpy(db->query_create_table,query_create_table);

  t_mysql_init(db);
  t_mysql_connect(db);
  t_mysql_createdb(db);
  t_mysql_setdb(db);
  t_mysql_createtable(db);
...

t-mysql.h:

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

#ifndef T_MYSQL_H_
#define T_MYSQL_H_

#define MAX_NAME_LENGTH     128
#define MAX_QUERY_LENGTH    1024

typedef struct {
     MYSQL *con;
     char password[MAX_NAME_LENGTH];
     char root_password[MAX_NAME_LENGTH];
     char user[MAX_NAME_LENGTH];
     char server[MAX_NAME_LENGTH];
     char database_name[MAX_NAME_LENGTH];
     char table_name[MAX_NAME_LENGTH];
     char query_create_table[MAX_QUERY_LENGTH];
     char query_create_db[MAX_QUERY_LENGTH];
     char query_data[MAX_QUERY_LENGTH];

}T_MYSQL;

void finish_with_error(MYSQL *con);
int t_mysql_init(T_MYSQL *mysql);
int t_mysql_connect(T_MYSQL *mysql);
int t_mysql_disconnect(T_MYSQL *mysql);
int t_mysql_createdb(T_MYSQL *mysql);
int t_mysql_createtable(T_MYSQL *mysql);
int t_mysql_setdb(T_MYSQL *mysql);
int t_mysql_insertdata(T_MYSQL *mysql);
int t_mysql_quickinsert(T_MYSQL *mysql);

#endif /* T_MYSQL_H_ */

t-mysql.c:

#include <stdio.h>
#include <mysql.h>
#include <my_global.h>
#include "t-mysql.h"


void finish_with_error(MYSQL *con){
    fprintf(stderr, "%s\n", mysql_error(con));
    mysql_close(con); //close connection to database
}

int t_mysql_init(T_MYSQL *mysql){

    mysql->con = mysql_init(NULL);

    if (mysql->con == NULL){
        finish_with_error(mysql->con);
        return 0;
    }

    return 1;
}


int t_mysql_connect(T_MYSQL *mysql){
    // connect to MySQL
    if (mysql_real_connect(mysql->con, mysql->server, mysql->user, mysql->password,NULL, 0, NULL, 0) == NULL){
            finish_with_error(mysql->con);
            return 0;
    }
    return 1;
}

int t_mysql_disconnect(T_MYSQL *mysql){
    mysql_close(mysql->con);
    if ( mysql->con == NULL){
        finish_with_error(mysql->con);
        return 0;
    }
return 1;
}


int t_mysql_createdb(T_MYSQL *mysql){

    t_mysql_disconnect(mysql); //disconnect normal user
    //connect as root
    t_mysql_init(mysql);
    if (mysql_real_connect(mysql->con, mysql->server, "root", mysql->root_password,NULL, 0, NULL, 0) == NULL){
                finish_with_error(mysql->con);
                return 0;
        }

    // create DB, if it doesn't exist
    if (mysql_select_db(mysql->con, mysql->database_name)){
        if (mysql_query(mysql->con, mysql->query_create_db)){
          finish_with_error(mysql->con);
          return 0;
        }
        else {
        fprintf(stdout,"Database %s created\n",mysql->database_name);
        }
    }else{
        fprintf(stdout,"Database %s exists\n",mysql->database_name);
    }

    //disconnect root user
    t_mysql_disconnect(mysql);
    //connect as normal user
    t_mysql_init(mysql);
    t_mysql_connect(mysql);

    return 1;
}

int t_mysql_setdb(T_MYSQL *mysql){

    if (mysql_select_db(mysql->con, mysql->database_name) == NULL){
        finish_with_error(mysql->con);
        return 0;
    }
    return 1;
}

int t_mysql_createtable(T_MYSQL *mysql){
    //create table:
    if(mysql_query(mysql->con, mysql->query_create_table)){
        finish_with_error(mysql->con);
        return 0;
    }
    return 1;
}

int t_mysql_insertdata(T_MYSQL *mysql){
    if (mysql_query(mysql->con, mysql->query_data)) {
        finish_with_error(mysql->con);
        return 0;
    }
    return 1;
}
4

1 回答 1

2

I didn't test your code on my system, but I think, the error is here:

int t_mysql_setdb(T_MYSQL *mysql){

    if (mysql_select_db(mysql->con, mysql->database_name) == NULL){//<-?
        finish_with_error(mysql->con);
        return 0;
    }
    return 1;
}

You are thinking, that 0 is the returned, when there is an error, while connecting. While MySQL docs say, that the opposite is true:

int mysql_select_db(MYSQL *mysql, const char *db)

Return Values:
Zero for success. Nonzero if an error occurred. 

http://dev.mysql.com/doc/refman/5.5/en/mysql-select-db.html

So, you are checking for a wrong value. It means, that this statement:

I called mysql_select_db() and it returned no errors

Is false, and there is an error in this function, that you don't catch.

于 2013-07-03T20:54:49.190 回答