1

My system requirement is to execute sequence of mysql commands from ubuntu terminal,
For testing I create a file query.sql on the desktop that have a content like:

create table aa (id int);  
create tab bb (id int);  
create table cc (id int);  

I execute this with command like:

zero@zero-desktop:~/Desktop$ mysql --user=root --password=admin --database=zero <query.sql

My problem is if any query fail, script stop executing.
Can anybody tell me how I prevent it[ here only second have wrong syntax, first and third should be run] I want to execute whole script, if any fail avoid it and start again from next query and generate a file where it shows error list........

2
  • Make sure first it's really a good idea to run queries with syntax errors. Commented Mar 29, 2011 at 7:46
  • some queries fails while there are no syntactical mistake,[ table have restriction to insert only 5 row and when we go for 6 then what? it is just an example i think we should think if one fail how we overcome] Commented Mar 29, 2011 at 8:00

3 Answers 3

1

if you want to excute whole script file , no matter query is syntactically correct or not use this command

zero@zero-desktop:~/Desktop$ mysql --force  --user=root --password=admin   --database=zero  <query.sql

it will skip query if have problem and move to execute next query...
and finally you will get error list if there had any syntatically wrong query...... error will display on terminal like:- ERROR 1064 (42000) at line 2: 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 'tab bb (id int)' at line 1 ERROR 1050 (42S01) at line 3: Table 'cc' already exists
-----------IS THERE ANY WAY TO KEEP THIS ERROR ON FILE RATHER THAN TERMINAL?----------

Sign up to request clarification or add additional context in comments.

1 Comment

yes complete query is below use it....zero@zero-desktop:~/Desktop$ mysql --force --user=root --password=admin --database=zero <query.sql 2>error.log
0

I've never used it, but the --force key should ignore all errors

Again, haven't tested this as well, but appending the mysql line with "2> error.log" should redirect all error output of mysql to a file error.log

Comments

0

If you only have CREATE TABLEStatements in your file you can add a IF NOT EXISTS to avoid problems when the table already exists. That way you can rerun your script without any problems after you received an error.

create table if not exists aa (id int);  
create tab if not exists bb (id int);  
create table if not exists cc (id int);  

See: CREATE TABLE Syntax

1 Comment

I am just trying to know the concept that's why i made syntactical mistake, i will try to write the query which will never fail but if it happened then what ? I only want -execute those who can , and show error report if any query fail.

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.