开发者

how connect mysql using c++

开发者 https://www.devze.com 2023-03-26 06:56 出处:网络
Editor\'s Note: Original Text: how connect mysql using c++ #include <iostream> #include <fstream>

Editor's Note: Original Text:

how connect mysql using c++

#include <iostream>
#include <fstream>
#include <string>
#include "/usr/local/mysql5/include/mysql.h"
using namespace std;

int main() {
    MYSQL *mysql;
    MYSQL_RES *result;
    MYSQL_ROW row;

    string server = "192.168.1.92";
    string username = "useradmin";
    string password = "useradmin";
    string database = "market";
    int port = 3306;

    mysql = mysql_init(0);
    if ( !mysql_real_connect( mysql, server.c_str(), username.c_str(), password.c_str(), database.c_str(), port, NULL, 0 ) ) {
        fprintf( stderr, "%s\n", mysql_error( mysql ) );
        return 0;
    }
    if ( !mysql_query( mysql, "SELECT text, prequency FROM ma_dict" ) ) {
        fprintf(stderr, "%s\n", mysql_error( mysql ) );
        return 0;
    }

    result = mysql_use_result( mysql );
    ofstream SaveFile("/tmp/dict.txt");

    while ( ( row = mysql_fetch_row( result ) ) != NULL ) {
        //SaveFile << fprintf( stdout, "%s\t%d", row[0], $row[1] ) << endl;
        cout << row[0] << endl;
    }

    mysql_free_result( result );
    mysql_close( mysql );
    SaveFile.close();

    return 1;

}

undefined reference to mysql_init' undefined reference tomysql_real_connect' ...


Editor's Note: A translation was attempted:

I'm using Eclipse, and I get compilation errors using mysql.h. How do I link to mysql?

#include "mysql.h"

Editor's Note: English questions are required on Stack Overflow. Please do your best at translating your question to English as needed, even if you have to use an automatic translation 开发者_JAVA技巧service like Google Translate.


Editor's Note: Original Text:

信息太少了, 把编译器错误贴出来看看. 可能因为:

  • mysql.h不在配置的include目录中.
  • Mysql库文件不在link列表里面
  • 头文件与二进制库文件不配套

Editor's Note: A translation was attempted:

There's too little information in your question. Post the actual compiler error into your question.

That being said, the error may be caused by:

  • Mysql.h is not in the configuration or in the include directory.
  • MySql database file is not in the linker's path.
  • Header and library files are not consistent with each other.


An issue in the sample is that mysql_query returns 0 on SUCCESS – so it always thinks an error occurred on success.

Change it to:

if ( mysql_query( mysql, "SELECT text, prequency FROM ma_dict" ) ) {
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号