我有下表:
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)
)
当我在命令行上运行以下命令时:
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;
...这是我得到的输出:
+----+--------+------+-------+---------------+-------+
| id | closed | user | level | comment | count |
+----+--------+------+-------+---------------+-------+
| 9 | 0 | 1 | 50 | First insert | 1 |
| 10 | 0 | 1 | 75 | Second insert | 1 |
+----+--------+------+-------+---------------+-------+
当我使用mysql_query()
运行这些命令时,这就是我得到的。
+----+--------+------+-------+---------------+-------+
| id | closed | user | level | comment | count |
+----+--------+------+-------+---------------+-------+
| 11 | 0 | 1 | 50 | Second insert | 2 |
+----+--------+------+-------+---------------+-------+
因此,当我使用mysql_query()
函数时,查询正在更新而不是插入新行,因为它不应该是,因为这两个插入具有不同的级别,因此它们是唯一的... ?我错了,还是有什么东西在这里?
编辑:我使用它的代码片段是:
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 );
我知道链接是正确的,并且正在运行此代码,因为它确实有效(即运行查询),除了它正在更新而不是插入的问题。
答案 0 :(得分:1)
我无法重现任何问题。我刚用上面的表测试了这个(感谢您创建一个简单的测试用例)和一个快速的C程序。它按预期工作,插入两行。使用MySQL 8.0.0-dmr。
#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);
}
输出显示:
Rows: 1
Rows: 1
表格中的数据:
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行,这是正常的。
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 |
+----+--------+------+-------+---------------+-------+