python
import MySQLdb
db = MySQLdb.connect(host='localhost', user='root', passwd='password', db='mydatabase')
python
cursor = db.cursor()
cursor.execute("SELECT * FROM mytable")
results = cursor.fetchall()
for row in results:
print(row)
python
query = "SELECT * FROM mytable WHERE column1 = %s AND column2 = %s"
cursor.execute(query, (value1, value2))
results = cursor.fetchall()
for row in results:
print(row)
python
try:
db.begin()
# ...
db.commit()
except:
db.rollback()