libdbi 的 dbi_conn_connect 在 c 中不起作用

发布于 2024-12-17 02:15:41 字数 1365 浏览 1 评论 0原文

我尝试使用 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 settings' 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

淡紫姑娘! 2024-12-24 02:15:41

安装libdbd-mysql后它就可以工作了。现在我已经安装了以下软件包。

libdbd-mysql                   
libdbi0  
libdbi0-dev

谢谢大家。

It has worked after install libdbd-mysql. Now I had install following packages.

libdbd-mysql                   
libdbi0  
libdbi0-dev

Thank all of you.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文