Solved

sql transaction in bash linux

Posted on 2011-03-02
6
1,523 Views
Last Modified: 2012-05-11
Hi,
I have a function in shell script which executes sql statements.
I want to use transactions for it.

function()
{
Begin transaction # i want SQL equivalent of this in shell scripting

source /etc/ipaccess/omcr-server/ipaccess-omcr;
mysql -u${MYSQL_ROOT_USER} -p${MYSQL_ROOT_PASSWORD} -e "Delete from omcr.AlarmNotification where alarmLifecycleId=50;"
 
commit transaction # i want SQL equivalent of this in shell scripting
}

How to execute the lines for "Begin transaction" and "commit"?????????
0
Comment
Question by:pvinodp
6 Comments
 
LVL 1

Assisted Solution

by:RealRaven
RealRaven earned 100 total points
ID: 35017414
Sorry, I don't think that is possible. Each connection to mysql will loose it's transactions on disconnect and it's not possible to first start a transaction, then connect again and do something and then connect a third time and do commit or rollback.

But if your example is complete and all you want to do is run ONE delete you'll have an automatic transaction around it whether you like it or not.
0
 

Author Comment

by:pvinodp
ID: 35017567
hi raven
the function() contains multiple SQL statements.
0
 
LVL 68

Assisted Solution

by:woolmilkporc
woolmilkporc earned 50 total points
ID: 35017665
You could put all your statements (including begin/commit) into a textfile to be read by mysql, like

mysql -u${MYSQL_ROOT_USER} -p${MYSQL_ROOT_PASSWORD} < /text/file

wmp
0
Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

 
LVL 1

Expert Comment

by:RealRaven
ID: 35020190
As woolmilkporc points out a script file is always possible but it doesn't look like a good solution in this case.

You would have to write to a temporary file, send that to mysql and then delete it. It's possible but not pretty.
0
 
LVL 3

Accepted Solution

by:
mwiercin earned 350 total points
ID: 35027001
Hi,

The simplest way would be to concatenate all the queries together delimited with semi colon (you can also USE mysql's DELIMITER keyword to  change it).  You can use a temp file for doing so, or combine use of SOURCE  command, i.e.

$ mysql -u${MYSQL_ROOT_USER} -p${MYSQL_ROOT_PASSWORD} -e "BEGIN; SOURCE /etc/ipaccess/omcr-server/ipaccess-omcr; Delete from omcr.AlarmNotification where alarmLifecycleId=50; COMMIT;"

Open in new window


If you want to maintain more control over execution, (named) pipes would be the way to go.

One way communication using a pipe:

$ (echo "SELECT NOW(); SELECT CONNECTION_ID(); "; sleep 1; echo "SELECT NOW();SELECT CONNECTION_ID();" ) | mysql
NOW()
2011-03-03 13:20:13
CONNECTION_ID()
2865
NOW()
2011-03-03 13:20:14
CONNECTION_ID()
2865

Open in new window


Named pipes (mypipe being MySQL entry, outpipe - output).:
$ cat mypipe |  mysql  > outpipe &
[1] 17660
$ echo "SELECT NOW();" > mypipe; cat outpipe;
NOW()
2011-03-03 13:30:03
[1]+  Done                    cat mypipe | mysql > outpipe
$

Open in new window


You can also use socat in similar manner as above.

I don't recommend making it too complicated as handling both output and input  streams in bash will mean that you are effectively implementing your own database driver, thus if you need more control it's better to use a proper driver (mean switch to Perl/PHP/Python/Ruby etc.).
0
 

Author Closing Comment

by:pvinodp
ID: 35065883
Thanks all.
Finally i did put in the solution by mwiercin :

$ mysql -u${MYSQL_ROOT_USER} -p${MYSQL_ROOT_PASSWORD} -e "BEGIN; SOURCE /etc/ipaccess/omcr-server/ipaccess-omcr; Delete from omcr.AlarmNotification where alarmLifecycleId=50; COMMIT;"
[/code]
0

Featured Post

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

It’s 2016. Password authentication should be dead — or at least close to dying. But, unfortunately, it has not traversed Quagga stage yet. Using password authentication is like laundering hotel guest linens with a washboard — it’s Passé.
Google Drive is extremely cheap offsite storage, and it's even possible to get extra storage for free for two years.  You can use the free account 15GB, and if you have an Android device..when you install Google Drive for the first time it will give…
Connecting to an Amazon Linux EC2 Instance from Windows Using PuTTY.
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question