How do I connect to a MySQL database using a python program?
转载于:https://stackoverflow.com/questions/372885/how-do-i-connect-to-a-mysql-database-in-python
How do I connect to a MySQL database using a python program?
转载于:https://stackoverflow.com/questions/372885/how-do-i-connect-to-a-mysql-database-in-python
Here's one way to do it:
#!/usr/bin/python
import MySQLdb
# Connect
db = MySQLdb.connect(host="localhost",
user="appuser",
passwd="",
db="onco")
cursor = db.cursor()
# Execute SQL select statement
cursor.execute("SELECT * FROM location")
# Commit your changes if writing
# In this case, we are only reading data
# db.commit()
# Get the number of rows in the resultset
numrows = cursor.rowcount
# Get and display one row at a time
for x in range(0, numrows):
row = cursor.fetchone()
print row[0], "-->", row[1]
# Close the connection
db.close()