我试过了

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;
       ^^^^^^^^^^^^^


我究竟做错了什么?

最佳答案

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

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

关于mysql - 如何在Windows上运行sql脚本?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/45177390/

10-11 01:15