Benny Tjia Benny Tjia - 2 months ago 9
MySQL Question

what is the correct syntax for executing .sql script in MySQL command line?

I am confused. From references I have seen online, the command to execute a text file script is this:

mysql> --user=root --password=admin --database=zero <query.sql


However when I ran this, the command line said theres an error with mySQL syntax (error 1064).
I saved the query.sql script file within the C:program files...\MYSQL\MYSQL Server5.1.. (whichever folder directory that contains the mySQL command line terminal .exe)

I then did this:

mysql> USE db1 \g
mysql> source <query.sql \g


It also doesnt work; command line gave me the same error. mySQL version I have is different than other versions I have seen. As you can see, you have to add '\g' at the end of every query.

Please help, and let me know if the description is not very clear..thx

EDITED:
So this is the code I have inside the query.sql:

CREATE TABLE IF NOT EXISTS 'db1'(
'id' int(255) NOT NULL auto_increment,
'date' date NOT NULL,
'title' varchar(255) NOT NULL,
'introtext' text NOT NULL,
'maintext' text NOT NULL,
PRIMARY KEY ('id')
)

Answer

Remove the quotes: 'db1'. Use backquotes where necessary, like for field called date to identify it from type date. And add a ; at the end of the statement:

CREATE TABLE IF NOT EXISTS db1(
  id int(255) NOT NULL auto_increment,
  `date` date NOT NULL,
  title varchar(255) NOT NULL,
  introtext text NOT NULL,
  maintext text NOT NULL,
  PRIMARY KEY (id)
) ;