如何連接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
提交
取消