1

I have the following code:

#include <iostream>
#include <mysql/mysql.h>
using namespace std;

#define SERVER "localhost"
#define USER "root"
#define PASS "password"
#define DB "日本語"

int main(){
    MYSQL *connect;
    connect = mysql_init(NULL);
    if(!connect){
        cerr << "Error\n";
        return 1;
    }

    connect = mysql_real_connect(connect, SERVER, USER, PASS, DB, 0, NULL, 0);
    if(!connect){
        cerr << "Error\n";
        return 1;
    }
    return 0;
}

The mysql_real_connect causes the error message to be displayer. The problem is that the DB is in Japanese. I can connect perfectly fine, if I connect to a database in English.

I also tried to put DB="" and then to specify the table as db.table (i.e. 日本語.単語) but I received a segmentation fault when I tried to read from the table.

Any help will be appreciated.

3
  • Did you try passing an utf-8 string? Nov 20, 2013 at 11:28
  • What do you mean? Do you mean like using a string DB instead? I tried that but you need to call DC.c_str() because mysql_real_connect requires const char*. Also the MySQL database uses the eucjpms schema not the utf-8, which may be important. Nov 20, 2013 at 11:38
  • No matter which encoding you use in your database, to can use "SET NAMES" to setup utf8 encoding and mysql will do the translation. But here, the problem is to establish a connection. Try converting the wide-strings used in servername, username and dbname to utf-8. Nov 20, 2013 at 12:19

1 Answer 1

0

You probably know that but there exists different UTF encoding. I'd check the following points:

  • The encoding of your .cpp file.
  • Printing the DB constant as an array of byte.
  • Retrieve the name of the database from the MySQL database (just connect and retrieve all db names — I guess you can do that). Compare with (2).
  • Check the MySQL documentation and your database to know the encoding currently used.
1
  • I tried your suggestions. Firstly, I have no problem outputting the DB name to the standard output. Interestingly, I can connect properly to the MySQL server, when I don't specify the database name, however, when I run show tables I get: information_schema ??? mysql performance_schema test Nov 20, 2013 at 13:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.