0

所以我对数据库很陌生,但我做了一些研究,我认为我已经弄清楚了我需要的一切,但是当我尝试运行我的脚本时遇到了错误。

这是我的脚本

  mysql -h portal-rds -u $user --password=$mysqlpw <<QUERY_INPUT
  CREATE DATABASE IF NOT EXISTS $DATABASE
  use $DATABASE
  CREATE TABLE IF NOT EXISTS backUpFiles (fileName VARCHAR(20), archiveId VARCHAR(500), checkSum VARCHAR(100), glacierVault VARCHAR(100), timeStamp date);
  INSERT INTO backUpFiles
  VALUES ('$archive_file_name', '$archiveID', '$CURRENTVAULT', '$checkSum', CURDATE());
  QUERY_INPUT

我收到的错误是

ERROR 1064 (42000) at line 1: 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 'use glacier

CREATE TABLE IF NOT EXISTS backUpFiles (fileName VARCHAR(20), archiveId VARCHAR(500), checkSum VARCHAR(100), glacierVault VARCHAR(100), timeStamp date);
Query OK, 0 rows affected, 1 warning (0.00 sec)

然后我决定手动登录mysql并运行这个命令,除了这个不应该破坏任何东西的警告之外,它们工作得很好(我相信)

mysql> show warnings;
+-------+------+------------------------------------+
| Level | Code | Message                            |
+-------+------+------------------------------------+
| Note  | 1050 | Table 'backupfiles' already exists |
+-------+------+------------------------------------+
1 row in set (0.28 sec)
4

2 回答 2

1

;在语句后缺少 as 语句分隔符CREATE DATABASE

该语句应如下所示:

mysql -h portal-rds -u $user --password=$mysqlpw <<QUERY_INPUT
CREATE DATABASE IF NOT EXISTS $DATABASE; <---- were missing
use $DATABASE
CREATE TABLE IF NOT EXISTS backUpFiles (fileName VARCHAR(20), archiveId VARCHAR(500), checkSum VARCHAR(100), glacierVault VARCHAR(100), timeStamp date);
INSERT INTO backUpFiles
VALUES ('$archive_file_name', '$archiveID', '$CURRENTVAULT', '$checkSum', CURDATE());
QUERY_INPUT
于 2013-07-10T13:42:35.637 回答
0

您错过了前两个 sql 语句的 ;(分号)。

如果不存在则创建数据库 $DATABASE; 使用 $DATABASE;

于 2013-07-10T13:44:29.713 回答