如何在Windows上运行sql脚本?

时间:2017-07-18 21:26:28

标签: mysql

我试过

mysqlsh -h localhost -u mixtape-dating -p -f setup.sql

和setup.sql由

组成
USE MixtapeDating;

-- Tables
-- The Playlist table gets featured on the front page. 
CREATE TABLE Playlist (
    Id INT PRIMARY KEY AUTO_INCREMENT, 
    Title VARCHAR(255),
    Email VARCHAR(255),
    CreatedAt TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);

-- The PlaylistItem gets featured at /playlist/:id
CREATE TABLE PlaylistItem (
    Id INT PRIMARY KEY AUTO_INCREMENT,
    PlaylistId INT, 
    Title VARCHAR(255),
    Link VARCHAR(255)
);

-- Stored Procedures
-- Insert playlist
CREATE PROCEDURE InsertPlaylist
(
    IN Title VARCHAR(255),
    IN Email VARCHAR(255),
    OUT PlaylistId INT
)
BEGIN
    INSERT INTO Playlist (Title, Email)
    VALUES (@Title, @Email);
    SELECT LAST_INSERT_ID() AS @PlaylistId;
END

CREATE PROCEDURE InsertPlaylistItem
(
    IN PlaylistId INT, 
    IN Title VARCHAR(255),
    IN Link VARCHAR(255)
)
BEGIN
    INSERT INTO PlaylistItem (PlaylistId, Title, Link)
    VALUES (@PlaylistId, @Title, @Link);
END

-- Fetch all playlists
CREATE PROCEDURE GetPlaylists
BEGIN
    SELECT Id, Title, Email 
    FROM Playlist;
END

-- Fetch all playlist items for a playlist
CREATE PROCEDURE GetPlaylist
(IN Id INT)
BEGIN
    SELECT PlaylistId, Id, Title, Link
    FROM PlaylistItem
    WHERE Id = @Id; 
END

但是我收到以下错误:

C:\Users\moore\Desktop\playlist-dating>mysqlsh -h localhost -u mixtape-dating -p -f setup.sql
Enter password: ****************
SyntaxError: Unexpected identifier at setup.sql:1:4
in USE MixtapeDating;
       ^^^^^^^^^^^^^

我做错了什么?

3 个答案:

答案 0 :(得分:1)

mysqlsh在javascript模式下启动默认值。如果要运行SQL语句,则需要切换到SQL模式。您可以将--sql参数添加到mysqlsh

mysqlsh -h localhost -u mixtape-dating -p -f setup.sql --sql

答案 1 :(得分:0)

应该可以做到:

mysql -u username -p databasename < file.sql

我不知道我曾见过mysqlsh用于此目的吗?

答案 2 :(得分:0)

您的setup.sql存在一些问题。首先,默认分隔符为;,因此shell会部分解释您的create procedure。将分隔符更改为其他内容,例如//。此外,以@开头的变量为User-Defined Variables,不适用于内部程序。

修正setup.sql。您可以使用以下命令运行此脚本:mysqlsh -h localhost -u mixtape-dating -p -f setup.sql

-- DROP SCHEMA IF EXISTS MixtapeDating;
-- CREATE SCHEMA MixtapeDating;
USE MixtapeDating;

-- Tables
-- The Playlist table gets featured on the front page. 
CREATE TABLE Playlist (
    Id INT PRIMARY KEY AUTO_INCREMENT, 
    Title VARCHAR(255),
    Email VARCHAR(255),
    CreatedAt TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);

-- The PlaylistItem gets featured at /playlist/:id
CREATE TABLE PlaylistItem (
    Id INT PRIMARY KEY AUTO_INCREMENT,
    PlaylistId INT, 
    Title VARCHAR(255),
    Link VARCHAR(255)
);

DELIMITER //

-- Stored Procedures
-- Insert playlist
CREATE PROCEDURE InsertPlaylist
(
    IN Title_ VARCHAR(255),
    IN Email_ VARCHAR(255),
    OUT PlaylistId_ INT
)
BEGIN
    INSERT INTO Playlist (Title, Email)
    VALUES (Title_, Email_);
    SELECT LAST_INSERT_ID() AS PlaylistId_;
END
//

CREATE PROCEDURE InsertPlaylistItem
(
    IN PlaylistId_ INT, 
    IN Title_ VARCHAR(255),
    IN Link_ VARCHAR(255)
)
BEGIN
    INSERT INTO PlaylistItem (PlaylistId, Title, Link)
    VALUES (PlaylistId_, Title_, Link_);
END
//

-- Fetch all playlists
CREATE PROCEDURE GetPlaylists()
BEGIN
    SELECT Id, Title, Email 
    FROM Playlist;
END
//

-- Fetch all playlist items for a playlist
CREATE PROCEDURE GetPlaylist (IN Id_ INT)
BEGIN
    SELECT PlaylistId, Id, Title, Link
    FROM PlaylistItem
    WHERE Id = Id_;
END
//