How do I execute multiple SQL statements at once? - Python

0 votes
How do I execute multiple SQL statements at once using python?
Jun 20, 2019 in Python by Vedant
11,262 views
Hi, it is looks grate,

here you are defined sql statement directly in python file, but in case if we want to execute multiple .sql scripts(SCR(structure changes request and DCR(data change request ) and pl/sql scripts) files in sequence order how can we do. (for automating DB deployment process). if you help on this really appreciate.

Use a loop and change script each time or maybe you could combine it into one script even

Hope it helps!!

1 answer to this question.

0 votes

You can use sqlite3 module. Have a look at this example:

import sqlite3 

# Connection with the DataBase 

# 'library.db' 

connection = sqlite3.connect("library.db") 

cursor = connection.cursor() 

# SQL piece of code Executed 

# SQL piece of code Executed 

cursor.executescript(""" 

CREATE TABLE people( 

firstname, 

lastname, 

age 

); 

CREATE TABLE book( 

title, 

author, 

published 

); 

INSERT INTO 

book(title, author, published) 

VALUES ( 

'Dan Clarke''s GFG Detective Agency', 

'Sean Simpsons', 

1987 

); 

""") 

sql = """ 

SELECT COUNT(*) FROM book;"""


cursor.execute(sql) 

# The output in fetched and returned 

# as a List by fetchall() 

result = cursor.fetchall() 

print(result) 

sql = """ 

SELECT * FROM book;"""

cursor.execute(sql) 

result = cursor.fetchall() 

print(result) 

# Changes saved into database 

connection.commit() 

# Connection closed(broken) 

# with DataBase 

connection.close() 
answered Jun 20, 2019 by Travis

Related Questions In Python

0 votes
2 answers

How do I copy a file in python?

copy a file in python  from shutil ...READ MORE

answered Mar 27, 2019 in Python by rajesh
• 1,230 points
166 views
0 votes
1 answer

How do I copy a file in python?

Use the shutil module. copyfile(src, dst) Copy the contents ...READ MORE

answered Jul 31, 2018 in Python by Priyaj
• 57,700 points
118 views
0 votes
1 answer

Writing unit tests in Python: How do I start?

If you're brand new to using unittests, ...READ MORE

answered Sep 18, 2018 in Python by Priyaj
• 57,700 points
182 views
0 votes
2 answers

How do I connect to a MySQL Database in Python?

connect mysql database with python import MySQLdb db = ...READ MORE

answered Mar 28, 2019 in Python by rajesh
• 1,230 points
555 views
+1 vote
2 answers

how can i count the items in a list?

Syntax :            list. count(value) Code: colors = ['red', 'green', ...READ MORE

answered Jul 6, 2019 in Python by Neha
• 330 points

edited Jul 8, 2019 by Kalgi 1,457 views
0 votes
0 answers
+4 votes
6 answers

Lowercase in Python

You can simply the built-in function in ...READ MORE

answered Apr 11, 2018 in Python by hemant
• 5,800 points
1,087 views
+1 vote
1 answer

How can I use python to execute a curl command?

For sake of simplicity, maybe you should ...READ MORE

answered Oct 11, 2018 in Python by charlie_brown
• 7,780 points
55,075 views
0 votes
1 answer

How do I generate some random numbers with a distribution using Python?

scipy.stats.rv_discrete is what you ned IMHO. You can supply ...READ MORE

answered Oct 31, 2018 in Python by Anirudh
• 2,080 points

edited Dec 14, 2018 by Anirudh 215 views