我从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;
}
答案 0 :(得分:1)
在mysql_real_connect()
之前使用此行代码。
mysql_init(mysql);
mysql_options(mysql, MYSQL_READ_DEFAULT_GROUP,"your program name");