3

In codeAnywhere I'm trying to run pre written script files to create a table. When using codeAnywhere one must import the file to the shell for the code first, as I have done. However I have been unable to use the SOURCE command to run these files. I have currently attempted this syntax:

USE exams SOURCE students.txt;

What is the correct syntax here? Do I need to name the database in the syntax? Are there other commands which run text files containing code?

EDIT: I tried using this syntax, to the following result:

ERROR: Failed to open file 'exams(question5.txt)', error: 2

Sebastian Brosch
  • 42,106
  • 15
  • 72
  • 87
fishyninja1
  • 133
  • 1
  • 1
  • 6
  • MySQL does not have any commands / statements named `source`, therefore there is no way you could make it work there. You need to import the textual data into a table in MySQL to be able to use it from there. I do not really understand what you are trying to achive with this question. – Shadow Oct 05 '16 at 10:42
  • @Shadow I am trying to run a a script contain the code necessary to create a table. – fishyninja1 Oct 05 '16 at 10:46
  • is using \. an option? – fishyninja1 Oct 05 '16 at 10:57
  • @Shadow "MySQL does not have any commands / statements named source..." This is from the docs: "If you are already running mysql, you can execute an SQL script file using the source command or \. command: mysql> source file_name". It's been around since at least 5.6 (2013) – jbobbins Sep 16 '20 at 18:09
  • @jbobbins like the OP you are mixing up mysql server with mysql command line tool. – Shadow Sep 16 '20 at 18:52

2 Answers2

4

Put the commands on separate lines, without semi-colons for the shell commands, and if this doesn't work, then prefix with \ as well (I don't need to on my setup, but it's in the docs):

USE exams

SOURCE students.txt

https://dev.mysql.com/doc/mysql-shell-excerpt/5.7/en/mysql-shell-commands.html

Adam
  • 5,215
  • 5
  • 51
  • 90
2

On the shell you can use the following command to execute the queries from a text file:

mysql db_name < text_file

Hint: If the USE command (with correct database name) is specified on the textfile you don't need to specify the database. The SOURCE command is not available on MySQL instead you need the <.

You can find more information about executing queries from text files here: https://dev.mysql.com/doc/refman/5.7/en/mysql-batch-commands.html

Sebastian Brosch
  • 42,106
  • 15
  • 72
  • 87