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
29,809 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() 

Hope this helps!

If you need to know more about Python, join Python online course certification today.

Thanks!

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,270 points
967 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
• 58,090 points
739 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
• 58,090 points
738 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,270 points
1,593 views
0 votes
2 answers
+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 7, 2019 in Python by Neha
• 330 points

edited Jul 8, 2019 by Kalgi 4,067 views
0 votes
1 answer
+5 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,790 points
3,486 views
+1 vote
3 answers

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,720 points
93,349 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 1,143 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP