0

I tried

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

and setup.sql consists of

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

But I get the following error:

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

What am I doing wrong?

2
  • Not too familiar with windows command prompt, but if you're on windows 10, you can use the ubuntu subsystem. Commented Jul 18, 2017 at 21:28
  • Why not simply install MySQL Workbench (dev.mysql.com/downloads/file/?id=468295) and debug sql file like a boss ? (: Commented Jul 18, 2017 at 22:55

3 Answers 3

1

mysqlsh starts default in javascript mode. If you want to run SQL statements you need to switch to SQL mode. You can do this adding --sql argument to mysqlsh.

mysqlsh -h localhost -u mixtape-dating -p -f setup.sql --sql
Sign up to request clarification or add additional context in comments.

Comments

0

Should be able to do:

mysql -u username -p databasename < file.sql

I don't know that I've ever seen mysqlsh used for this purpose?

5 Comments

Is the database named MixtapeDating, or should there be a hyphen like the user's name? If the name is correct, try using backticks to surround the DB name in the file.
The database name is correct, I tried the backticks, to no avail.
Can you try commenting-out, or temporarily delete, the USE line altogether. Then try this command: mysql -u mixtape-dating -p MixtapeDating < setup.sql. I'm curious to know if the first table create statement will complain as well.
Are you sure it says database already exists? If so, then I don't understand why you're having so much trouble, as it's not this difficult. That command says to import the sql to the specified database MixtapeDating, which MUST already exist (it does NOT create the DB). Unless there's more to the script that we don't know (such as containing a create database ... statement) then, yes, it WILL complain. All you have are create tables and procedures, based on what you originally posted, so more detail may be needed.
mysqlsh is a new MySQL Shell. MySQL Shell (part of MySQL Server), an interactive JavaScript, Python and SQL console interface, supporting development and administration for the MySQL Server. It provides built in scriptable APIs that support the creation and management of MySQL InnoDB clusters, as well as a modern fluent CRUD API for the MySQL Document Store. Connections to MySQL server can use X Protocol or classic MySQL protocol.
0

Your setup.sql has some issues. First of all, default delimiter is ; therefore shell interpret your create procedure partially. Change your delimiter to something else, for example to //. Also variables which start with @ are User-Defined Variables and are not applicable to use inside procedures.

Fixed setup.sql. You can run this script using: 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
//

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.