Syntax error received when trying to source a file from mysql command line - mysql

I have a file that is filled with statements that appear like the following:
update table set column1=NULL where column2 in (select id from users where user_id in ('user1','user2'));
As you can see this is correct syntax. When I run this command manually on the mysql command line it works as intended.
However when I attempt to source the file I get syntax errors. Has anyone seen this issue before? I'm looking for a resolution...
mysql> \! source /tmp/update_users.sql
/tmp/update_users.sql: line 2: syntax error near unexpected token `('
/tmp/update_users.sql: line 2: `(select id from users where user_id in ('user1','user2'));'

You have to use the MySQL source command:
mysql> source /tmp/update_users.sql
When you use \!, you instead invoke a shell command. The shell source command expects a shell script, not a sql file.

Related

MYSQL SHELL - SQL MODE from SHELL SCRIPT

I am writing a shell script to run a sql select query using mysqlsh.
So I had some issues and I found that I need to use "\sql" to change it from
mysql-js> to mysql-sql>
I need to run /sql before I run the query from a file.
"C:\mysql-shell-1.0.11-windows-x86-64bit\bin\mysqlsh.exe" echo \sql | mysql://user#IP:PORT?schema=SCHEMANAME&password=PASSWORD
and then execute this to run the select query and save it to a tmpfile.txt
"C:\mysql-shell-1.0.11-windows-x86-64bit\bin\mysqlsh.exe" --file "$EDIR"/data/tmp.sql > "$EDIR"/data/tmpfile.txt 2>&1
I am not sure what am missing. I tried different alternatives,
I am getting the below error on the first line
"data_extractss.sh: line 53: mysql://user#IP:PORT?schema=SCHEMANAME&password=PASSWORD: No such file or directory"
Any suggestion is appreciated.
First, please upgrade to the latest version of the shell 8.0.12
Second, start the shell like 'mysqlsh --sql' or 'mysqlsh --sqlc' to have it start in SQL mode

Best place for .sql data and method for retrieving them in mysql database?

I have some confusion with .bash_profile, terminal, MAMP and MySQL Workbench.
My goal is to load .sql data files into a database from terminal. Working on tutorials from Paul Dubois' book on MySQL. The Appendix A says to add a PATH setting to .bash_profile:
PATH=/usr/local/mysql/bin:/bin:/usr/bin:/usr/local/bin
I added the above path to the .bash_profile. Although in the first chapter it says a folder of sample .sql files called (sampdb) can be located anywhere. So I tried:
export PATH="/Users/trevorpan/Documents/MySQL/sampdb:$PATH"
Adding this PATH did not find the file with this command:
mysql> sampdb < create_grade_event.sql;
It gave me this error. And I feel it must be related to the path, because all over stack overflow, the book, and the online MySQL manual say the above is the proper syntax.
ERROR 1064 (42000): 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 'sampdb < create_grade_event.sql' at line 1
I also tried:
mysql> source create_grade_event.sql;
ERROR:
Failed to open file 'create_grade_event.sql', error: 2
This didn't work either.
Now, I'm thinking the files should just go in the bin folder. But I'm not sure if they should stay in the folder (sampdb) or what.
Here's the .bash_profile:
export PATH=/Applications/MAMP/bin/php/php7.1.12/bin:/usr/local/opt/gnu-tar/lib$
export MAMP_PHP=/Applications/MAMP/bin/php/php7.1.12/bin
export PATH="$MAMP_PHP:$PATH"
ICU_DIR=/usr/local/Cellar/icu4c/60.2/
EXT_DIR=/Applications/MAMP/bin/php/php7.1.12/lib/php/extensions/no-debug-non-zt$
if [ -f ~/.bashrc ]; then
source ~/.bashrc
fi
export PATH="/usr/local/opt/m4/bin:$PATH"
PATH=/usr/local/mysql/bin:/bin:/usr/bin:/usr/local/bin
Thank you.
EDIT:
I tried dragging a file to terminal and had success.
mysql> source /Applications/MAMP/bin/sampdb/insert_member.sql
Query OK, 0 rows affected (0.05 sec)
Query OK, 1 row affected (0.02 sec)
Query OK, 1 row affected (0.00 sec)
Query OK, 1 row affected (0.04 sec)
Query OK, 1 row affected (0.01 sec) etc. etc.
EDIT #2
aha. Michael,
I tried changing the directory and received this error. cd is not able to point to a directory while mysql is loaded in terminal, so I'm unsure the method you use to load it from a current directory:
mysql> cd /Applications/MAMP/bin/sampdb;
ERROR 1064 (42000): 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 'cd /Applications/MAMP/bin/sampdb' at line 1
My goal is to point to a folder location and be able to upload .sql data files without having to enter the path each time. The following post had a similar issue, but it never seemed to resolve the simple import command of:
mysql> source create_grade_event.sql
How to import an SQL file using the command line in MySQL?
My original post name was trying to get to that. Should the .sql data files be placed in bin? If so, how do I direct mysql to load them with a query like above? Is there a better place you use to store project .sql files?
Thank you.
Loading a .sql file into an existing database is done like this:
shell> mysql sampdb < create_grade_event.sql
That is very different than this:
mysql> sampdb < create_grade_event.sql;
...which is indeed wrong.
This issue is not related to your path.
mysql> source create_grade_event.sql
...should have worked, but you did not indicate whether you did that, or this:
shell> source create_grade_event.sql;
...which would also be incorrect. In any event, "didn't work" is not enough detail.
Here, as in the MySQL documentation, shell> refers to your system shell prompt, which typically ends with $.

Mysql: "\." doesn't work?

I was trying to upload an .sql file to mysql console with \. but it throwed me:
mysql> \. /home/krest/s.sql;
ERROR:
Failed to open file '/home/krest/s.sql;', error: 2
However, "source" command works properly!
What I am doing wrong?
I am running console through ubuntu 14.04 terminal.
Look closer to the error message, everything you need to know is there:
Failed to open file '/home/krest/s.sql;', error: 2
Error 2 is "No such file or directory". You can use the perror command line tool to find out the message from an error code returned by MySQL.
Did you figure out what's wrong? The semicolon after the file name is not supposed to be there. MySQL thinks it is part of the file name and this is why it cannot find the file.
As a side note, it seems that some of the short commands work if the ending semicolon is preceded by a space and do not work without the space (\u, for example, works this way). Others take everything until the end of line as parameter (and fail with an error because of the ending semicolon).

Errors during import of sql file to MySQL database

I am new to database and have just started to learn MYSQL. I have downloaded a sql file called rating.sql and would like to import it to the MYSQL database installed on the local machine. The operation system is Windows 7. The path for the sql file is:
"E:\rating.sql".
I am using the following commands in the MySQL Command Line Client
mysql>use database_name;
mysql>source E:\rating.sql;
The system gives the following error message:
ERROR 1049 <42000>: Unknown database 'ating.sql'
It is definitely something related to the path. Can anyone explain how this error is generated?Thanks
You have use \ and this is an escape character (omits the immediate following character. So to fix this you can use \ instead.
mysql>resour E:\\rating.sql;
Mysql recognises \r as an escape for a carriage return character.
To make it simpler, could you rename your file to something beginning with another letter...mustn't be b n r t or z...

can not run a sql file windows command line using SOURCE command

hi i am using windows 7, xampp
. i am trying to run a sql file using CMD ut it gives me an error
ERROR:
Unknown command '\x'.
this is what i am doing
this command is not working
mysql> SOURCE C:\xampp\htdocs\elephanti2\db\mas_gis_city_ip.sql;
*** one other problem with this works fine when i tried by go to phpmyadmin and import the file .
why this happening , i have no idea , please help ..................
My original answer:
mysql> SOURCE C:\\xampp\\htdocs\\elephanti2\\db\\mas_gis_city_ip.sql;
You could also try executing the command like this:
mysql> SOURCE C:/xampp/htdocs/elephanti2/db/mas_gis_city_ip.sql;
(Source: a comment in the reference manual suggests using forward slashes on Windows machines)
Try quoting your file name. Notice your command:
mysql> SOURCE C:\xampp\htdocs...
is probably being interpreted as:
mysql SOURCE C: \xampp \htdocs...
(see the \x there?)
If you quote the file name, I bet it'll work. (not sure if mysql requires ' or " for quoting filenames in this context, try both)
Actually this worked. I think it is dwuff's previous answer.
mysql> SOURCE C:\\xampp\\htdocs\\elephanti2\\db\\mas_gis_city_ip.sql;
I had the same problem recently trying to install FossTrak, and this was the syntax that worked on windows 10, with Mysql 5.7
\. C:/share/epcis-repository-0.5.0/epcis_schema.sql
using \source did not, and neither did \\, and by accident i removed the ; and it worked.