mysql - "DROP INDEX"语句的 RMySQL 语法错误

标签 mysql r rmysql

我似乎无法弄清楚这里出了什么问题。

我试图在 R 中运行 sql 查询,但出现以下错误:

Error in .local(conn, statement, ...) :    could not run statement: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'DROP INDEX `PRIMARY` ON temporary_table' at line 2

以下是我的代码:

library(RMySQL)

con <- dbConnect(MySQL(),
                 user="XXX", password="XXX",
                 dbname="test", host="localhost")

query <- "CREATE TEMPORARY TABLE temporary_table LIKE test.helloworld;
         DROP INDEX `PRIMARY` ON temporary_table;"

rs <- dbSendQuery(con, query) #Where the error occurs

我在 mySQL 中运行了完全相同的查询,它完全正常。

谁能指出我做错了什么?

非常感谢!

最佳答案

分两部分执行查询。第一部分创建表,第二部分删除索引。对于前-

query <- "CREATE TEMPORARY TABLE temporary_table LIKE test.helloworld;"
rs <- dbSendQuery(con, query) #Where the error occurs

query <- "DROP INDEX `PRIMARY` ON temporary_table;"
rs <- dbSendQuery(con, query) #Where the error occurs

我已经在 php 中进行了测试。这在 php 中有同样的问题。运行良好。

<?php
$mysqli = new mysqli("localhost", "root", "root", "test");
$result = $mysqli->query("CREATE TEMPORARY TABLE `temporary_table` LIKE t;");
printf("Errormessage: %s\n", $mysqli->error);
$result = $mysqli->query(" DROP INDEX `PRIMARY` ON temporary_table;");
#$result = $mysqli->query("DROP table temporary_table;");
printf("Errormessage: %s\n", $mysqli->error);
?>
root@smart:/var/www# php test.php
Errormessage: 
Errormessage:

当我同时运行两个查询时,就会出现您所说的错误。

<?php
// mysqli
$mysqli = new mysqli("localhost", "root", "root", "test");
$result = $mysqli->query("CREATE TEMPORARY TABLE `temporary_table` LIKE t;DROP INDEX `PRIMARY` ON temporary_table;");
printf("Errormessage: %s\n", $mysqli->error);
?>
root@smart:/var/www# php test.php
Errormessage: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'DROP INDEX `PRIMARY` ON temporary_table' at line 1

关于mysql - "DROP INDEX"语句的 RMySQL 语法错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32626709/

相关文章:

php - MySQL连接结果集中的两个表并根据第一个表添加新列

r - 在 R 中使用 foreach 进行并行提升

mysql - R RMySQL 将大整数作为字符串获取

mysql - 需要 2 个 MySql 查询的帮助。加入与子查询

数据库 |将 CONCAT 函数格式化为带有小括号

R - 比较 R 中 2 个数据帧之间的数据

r - 为基数 R 中的一个因子选择最大值

mysql - Unknown column in field list报错Rmysql

RMySQL dbReadTable 耗时太长

mysql - 您如何处理用户的删除