Skip to content Skip to sidebar Skip to footer

Executing Several Sql Queries With Mysqldb

How would you go about executing several SQL statements (script mode) with python? Trying to do something like this: import MySQLdb mysql = MySQLdb.connect(host='host...rds.amazona

Solution 1:

Here is how you could use executemany():

import MySQLdb
connection = MySQLdb.connect(host='host...rds.amazonaws.com', db='dbName', user='userName', passwd='password')
cursor= connection.cursor()

my_data_to_insert = [['maxim0', 'was here0'], ['maxim1', 'was here1'], ['maxim2', 'was here1']]
sql= "insert into rollout.version (`key`, `value`) VALUES (%s, %s);"

cursor.executemany(sql, my_data_to_insert)

connection.commit()
connection.close()

Solution 2:

Call the executemany method on the cursor object. More info here: http://mysql-python.sourceforge.net/MySQLdb.html

Post a Comment for "Executing Several Sql Queries With Mysqldb"