开发者

mysql script file (.sql) running problem

开发者 https://www.devze.com 2023-02-20 23:37 出处:网络
My system requirement is to execute sequence of开发者_如何学编程 mysql commands from ubuntu terminal,

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........


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?----------


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


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

0

精彩评论

暂无评论...
验证码 换一张
取 消