如何连接Python中的MySQL数据库?如何使用python程序连接到MySQL数据库?
3 回答
交互式爱情
TA贡献1712条经验 获得超3个赞
#!/usr/bin/pythonimport MySQLdb# Connectdb = MySQLdb.connect(host="localhost", user="appuser", passwd="", db="onco")cursor = db.cursor()# Execute SQL select statementcursor.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 resultsetnumrows = cursor.rowcount# Get and display one row at a timefor x in range(0, numrows): row = cursor.fetchone() print row[0], "-->", row[1]# Close the connectiondb.close()
添加回答
举报
0/150
提交
取消