libdbi 的 dbi_conn_connect 在 c 中不起作用
我尝试使用 gcc test.c -o test.o -ldbi 命令执行以下代码。
#include <stdio.h>
#include <dbi/dbi.h>
int main() {
dbi_conn conn;
dbi_result result;
double threshold = 4.333333;
unsigned int idnumber;
const char *fullname;
dbi_initialize(NULL);
conn = dbi_conn_new("mysql");
dbi_conn_set_option(conn, "host", "localhost");
dbi_conn_set_option(conn, "username", "root");
dbi_conn_set_option(conn, "password", "root123");
dbi_conn_set_option(conn, "dbname", "test");
dbi_conn_set_option(conn, "encoding", "UTF-8");
if (dbi_conn_connect(conn) < 0) {
printf("Could not connect. Please check the option settings\n");
}
else {
result = dbi_conn_queryf(conn, "SELECT id, name FROM users");
if (result) {
while (dbi_result_next_row(result)) {
idnumber = dbi_result_get_uint(result, "id");
fullname = dbi_result_get_string(result, "name");
printf("%i. %s\n", idnumber, fullname);
}
dbi_result_free(result);
}
dbi_conn_close(conn);
}
dbi_shutdown();
return 0;
}
我只有“无法连接”。请始终检查选项设置的结果。
我可以使用以下命令和密码 root123
连接我的数据库
mysql -h localhost -u root -p
,并且我可以在命令行中使用测试数据库。
请帮我。可能出现哪些问题?
I try to execute following code using gcc test.c -o test.o -ldbi
command.
#include <stdio.h>
#include <dbi/dbi.h>
int main() {
dbi_conn conn;
dbi_result result;
double threshold = 4.333333;
unsigned int idnumber;
const char *fullname;
dbi_initialize(NULL);
conn = dbi_conn_new("mysql");
dbi_conn_set_option(conn, "host", "localhost");
dbi_conn_set_option(conn, "username", "root");
dbi_conn_set_option(conn, "password", "root123");
dbi_conn_set_option(conn, "dbname", "test");
dbi_conn_set_option(conn, "encoding", "UTF-8");
if (dbi_conn_connect(conn) < 0) {
printf("Could not connect. Please check the option settings\n");
}
else {
result = dbi_conn_queryf(conn, "SELECT id, name FROM users");
if (result) {
while (dbi_result_next_row(result)) {
idnumber = dbi_result_get_uint(result, "id");
fullname = dbi_result_get_string(result, "name");
printf("%i. %s\n", idnumber, fullname);
}
dbi_result_free(result);
}
dbi_conn_close(conn);
}
dbi_shutdown();
return 0;
}
I have only 'Could not connect. Please check the option setting
s' as result always.
I can connect my data base using following command with password root123
mysql -h localhost -u root -p
And I can used test database in command line.
Please help me. What are the possible issues?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
安装
libdbd-mysql
后它就可以工作了。现在我已经安装了以下软件包。谢谢大家。
It has worked after install
libdbd-mysql
. Now I had install following packages.Thank all of you.