首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >mysql_query()的行为与命令行上的mysql不同

mysql_query()的行为与命令行上的mysql不同
EN

Stack Overflow用户
提问于 2016-11-01 05:12:16
回答 1查看 78关注 0票数 3

我有下表:

代码语言:javascript
复制
insert_test | CREATE TABLE insert_test (
  id int(11) NOT NULL AUTO_INCREMENT,
  closed int(11) NOT NULL DEFAULT '0',
  user int(11) DEFAULT '-1',
  level int(11) DEFAULT '-1',
  comment text,
  count int(11) DEFAULT '1',
  PRIMARY KEY (id,closed),
  UNIQUE KEY user (user,level,closed)
)

当我在命令行上运行以下命令时:

代码语言:javascript
复制
INSERT INTO db.insert_test (closed, user, level, comment, count) VALUES (0, 1, 50, 'First insert', 1) ON DUPLICATE KEY UPDATE comment=VALUES(comment), count=count+1;
INSERT INTO db.insert_test (closed, user, level, comment, count) VALUES (0, 1, 75, 'Second insert', 1) ON DUPLICATE KEY UPDATE comment=VALUES(comment), count=count+1;

...this是我得到的输出:

代码语言:javascript
复制
+----+--------+------+-------+---------------+-------+
| id | closed | user | level | comment       | count |
+----+--------+------+-------+---------------+-------+
|  9 |      0 |    1 |    50 | First insert  |     1 |
| 10 |      0 |    1 |    75 | Second insert |     1 |
+----+--------+------+-------+---------------+-------+

当我使用mysql_query()运行这些命令时,得到的结果如下所示。

代码语言:javascript
复制
+----+--------+------+-------+---------------+-------+
| id | closed | user | level | comment       | count |
+----+--------+------+-------+---------------+-------+
| 11 |      0 |    1 |    50 | Second insert |     2 |
+----+--------+------+-------+---------------+-------+

因此,当我使用mysql_query()函数时,查询正在更新,而不是插入新行,因为这两个插入具有不同的级别,所以它们是unique...right?是我错了,还是这里发生了什么?

编辑:我使用的代码片段如下:

代码语言:javascript
复制
char* query = "INSERT INTO db.insert_test (closed, user, level, comment, count) VALUES (0, 1, 50, 'First insert', 1) ON DUPLICATE KEY UPDATE comment=VALUES(comment), count=count+1;";
char* query2 = "INSERT INTO db.insert_test (closed, user, level, comment, count) VALUES (0, 1, 75, 'Second insert', 1) ON DUPLICATE KEY UPDATE comment=VALUES(comment), count=count+1;";
mysql_query( link, query );
mysql_query( link, query2 );

我知道链接是正确的,并且这段代码正在运行,因为它确实可以工作(即运行查询),除了它正在更新而不是插入的问题。

EN

回答 1

Stack Overflow用户

发布于 2016-11-02 05:38:46

我不能重现任何问题。我刚刚使用上面的表格(感谢您创建了一个简单的测试用例)和一个快速的C程序对此进行了测试。它按照预期工作,插入了两行。使用MySQL 8.0.0-dmr。

代码语言:javascript
复制
#include <my_global.h>
#include <mysql.h>
#include <string.h>

int main(int argc, char **argv)
{
  MYSQL *con = mysql_init(NULL);

  if (con == NULL)
  {
      fprintf(stderr, "%s\n", mysql_error(con));
      exit(1);
  }

  mysql_real_connect(con, "localhost", "root", "password", "test", 0, NULL, 0);

  if (strlen(mysql_error(con)))
  {
      fprintf(stderr, "%s\n", mysql_error(con));
      mysql_close(con);
      exit(1);
  }

  char* query = "INSERT INTO insert_test (closed, user, level, comment, count) VALUES (0, 1, 50, 'First insert', 1) ON DUPLICATE KEY UPDATE comment=VALUES(comment), count=count+1;";
  char* query2 = "INSERT INTO insert_test (closed, user, level, comment, count) VALUES (0, 1, 75, 'Second insert', 1) ON DUPLICATE KEY UPDATE comment=VALUES(comment), count=count+1;";

  mysql_query(con, query);

  if (mysql_errno(con))
  {
      fprintf(stderr, "Error: %s\n", mysql_error(con));
      mysql_close(con);
      exit(1);
  }

  printf("Rows: %ld\n", (long) mysql_affected_rows(con));

  if (mysql_warning_count(con))
  {
      fprintf(stderr, "Warnings: %s\n", mysql_error(con));
      mysql_close(con);
      exit(1);
  }

  mysql_query(con, query2);

  if (mysql_errno(con))
  {
      fprintf(stderr, "Error: %s\n", mysql_error(con));
      mysql_close(con);
      exit(1);
  }

  printf("Rows: %ld\n", (long) mysql_affected_rows(con));

  if (mysql_warning_count(con))
  {
      fprintf(stderr, "Warnings: %s\n", mysql_error(con));
      mysql_close(con);
      exit(1);
  }

  mysql_close(con);
  exit(0);
}

输出显示:

代码语言:javascript
复制
Rows: 1
Rows: 1

表中的数据:

代码语言:javascript
复制
mysql> select * from insert_test;
+----+--------+------+-------+---------------+-------+
| id | closed | user | level | comment       | count |
+----+--------+------+-------+---------------+-------+
|  5 |      0 |    1 |    50 | First insert  |     1 |
|  6 |      0 |    1 |    75 | Second insert |     1 |
+----+--------+------+-------+---------------+-------+

多次运行该程序会使两行的count列递增。当发生这种情况时,它会显示每个语句受影响的2行,这是正常的。

代码语言:javascript
复制
mysql> select * from insert_test;
+----+--------+------+-------+---------------+-------+
| id | closed | user | level | comment       | count |
+----+--------+------+-------+---------------+-------+
| 23 |      0 |    1 |    50 | First insert  |     2 |
| 24 |      0 |    1 |    75 | Second insert |     2 |
+----+--------+------+-------+---------------+-------+
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/40350650

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档