c++ - 使用 mysql_fetch_row 后应用程序崩溃

标签 c++ mysql sql freebsd

我遇到了一个关于简单的 MySQL 函数的问题,它是 mysql_fetch_row,每当我使用它时,我的应用程序都会崩溃,它会在执行时崩溃。 无论我运行什么查询,它都会崩溃。核心转储内容如下:

(gdb) bt full
#0  0x2866397f in mysql_store_result () from /usr/home/ld/application
#1  0x28637905 in main () from /usr/home/ld/application
#2  0x08441d3a in CRC_GetCodeSize20 ()

代码看起来很简单:

int main()
{
  MYSQL *conn;      // the connection
  MYSQL_RES *res;   // the results
  MYSQL_ROW row;    // the results row (line by line)
 
  struct connection_details mysqlD;
  mysqlD.server = "localhost";  // where the mysql database is
  mysqlD.user = "mysqlusername";        // the root user of mysql   
  mysqlD.password = "mysqlpassword"; // the password of the root user in mysql
  mysqlD.database = "mysql";    // the databse to pick
 
  conn = mysql_connection_setup(mysqlD);
 
  res = mysql_perform_query(conn, "select 1, 2");
 
  printf("Result:\n");
  while ((row = mysql_fetch_row(res)) !=NULL)
      printf("%s\n", row[0]);
 
  mysql_free_result(res);
  mysql_close(conn);
 
  return 0;
}

问题是什么?

编辑

mysql_perform_query:

MYSQL_RES* mysql_perform_query(MYSQL *connection, char *sql_query)
{
   if (mysql_query(connection, sql_query))
   {
      printf("MySQL query error : %s\n", mysql_error(connection));
      exit(1);
   }
 
   return mysql_use_result(connection);
}

最佳答案

好的。所以我花了相当多的时间来重现这个问题。我假设您使用了本教程中的示例:http://www.codingfriends.com/index.php/2010/02/17/mysql-connection-example/因为它完全一样。

采取的步骤:

  1. cd/usr/ports/databases/mysql56-client && make install

  2. 将上述教程中的确切代码复制粘贴到 test.cpp

  3. g++ -I/usr/local/include/mysql -L/usr/local/lib/mysql -lmysqlclient test.cpp

  4. ./a.out

  5. 输出:

    数据库中的Mysql表: 入口 老

我使用了我的远程 mysql 服务器和一个测试帐户。首先,我确保我可以通过控制台连接到它 mysql -h mydomain.com -u test -p

程序似乎运行正常。我唯一注意到的是,有时执行需要 1 秒,而有时出于某种原因最多需要 10 秒。

基于 PC-BSD Isotope Edition(9.1 发行版)构建,具有最新的端口树。

所以现在有 2 个人成功构建(我和你的 friend )。代码相同,我唯一能想到的错误是 libmysqlclient.so 库。尝试更新您的端口树并进行全新构建。或者尝试不同的版本。

关于c++ - 使用 mysql_fetch_row 后应用程序崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20023686/

相关文章:

php - 我需要清理数据库查询的结果吗?

sql - 为什么我重构的 SQL 运行速度要快几个数量级?

SQL 如何查找一年中每天 2 个不同组的平均出行次数

sql - excel中的东西功能

c++ - 为什么 `A & a = a` 有效?

C++动态数组在分配时导致段错误

c++ - 调用 lambda 的结构/类成员时程序崩溃并显示 "bad_function_call"

c++ - Boost Spirit 报错

java - 无法从spring项目连接到mysql数据库

php - 使用 PHP 处理 Follow/Followers