2

我从 MySQL 参考中尝试了这段代码,但是当涉及到这一点时它崩溃了mysql_options(mysql, MYSQL_READ_DEFAULT_GROUP, "libmysqld_client");。有人有嵌入式服务器的工作示例吗?

#include <mysql.h>

static char *server_options[] = {"mysql_test", "--defaults-file=my.ini", NULL};
int num_elements = (sizeof(server_options) / sizeof(char *)) - 1;
static char *server_groups[] = {"libmysqld_server", "libmysqld_client", NULL};

int main(int argc, char *argv[])
{
    QSqlDatabase mydb;
    MYSQL *mysql;

    mysql_library_init(num_elements, server_options, server_groups);
    mysql = mysql_init(NULL);
    mysql_options(mysql, MYSQL_READ_DEFAULT_GROUP, "libmysqld_client");
    mysql_options(mysql, MYSQL_OPT_USE_EMBEDDED_CONNECTION, NULL);

    mysql_real_connect(mysql, NULL, NULL, NULL, "database1", 0, NULL, 0);

    mydb = QSqlDatabase::addDatabase("QMYSQL", "con1");

    mydb.setDatabaseName("database1");

    if(!mydb.open())
    {
        qDebug() << mydb.lastError();
    }

    mysql_library_end();
    return 0;
}
4

1 回答 1

1

之前使用这行代码mysql_real_connect()

mysql_init(mysql);
mysql_options(mysql, MYSQL_READ_DEFAULT_GROUP,"your program name"); 
于 2016-08-22T07:12:19.553 回答