为了账号安全,请及时绑定邮箱和手机立即绑定

如何连接Python中的MySQL数据库?

如何连接Python中的MySQL数据库?

桃花长相依 2019-06-04 17:09:39
如何连接Python中的MySQL数据库?如何使用python程序连接到MySQL数据库?
查看完整描述

3 回答

?
交互式爱情

TA贡献1712条经验 获得超3个赞

以下是一种方法,使用MySQLdb,它只支持Python 2:

#!/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()

参考文献


查看完整回答
反对 回复 2019-06-04
  • 3 回答
  • 0 关注
  • 605 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信