sqlite3 - How to get output of sqlite command in shell script -


i trying make rollback if went wrong during execution of sql crud operations or if can not make commit shell scripting.

i have test2.sh , test.sh test.sh:

 #!/bin/sh  sqlite3 db.sqlite << eof  begin;  select * table1;  

and test2.sh

#!/bin/sh if echo `./test.sh`|grep -q "sql error";   rollback; else   err=commit;   if echo $err |grep -q "error";     rollback;   fi fi 

there no table called table1 , expected sql error output of test.sh , rollback.

but gives error : rollback: command not found. how can error , make rollback? or way follow right?

your test2.sh script fails because shell trying execute program called rollback, hence "command not found". want use sqlite instruction rollback, whichs means you'd have @ least :

sqlite3 db.sqlite << eof rollback; eof 

but don't think work. see it, rollback has occur within same sqlite session 1 started test.sh have effect. test.sh script makes sqlite plow through commands until reaches eof, when grep errors test2.sh, it's late. there's no way can rollback test2.sh script, sits outside sqlite session.

so, complete answer, recommend step away bash scripting , use programming language, allow open sqlite session, execute commands , control transactions. quick example in python :

import sqlite3  conn = sqlite3.connect('db.sqlite')  c = conn.cursor()  try:     c.execute('insert table1 values(1)')     conn.commit() except:     conn.rollback()  c.close() 

Popular posts from this blog

How to calculate SNR of signals in MATLAB? -

c# - Attempting to upload to FTP: System.Net.WebException: System error -

ios - UISlider customization: how to properly add shadow to custom knob image -