首页 > IT职业 > IT图书 > python连接mysql数据库步骤

1、导入模块
#导入模块 import pymysql
相关课程推荐:Python基础视频教程(python语言基础)
2、打开数据库连接
#打开数据库连接 #注意:这里已经假定存在数据库testdb,db指定了连接的数据库,当然这个参数也可以没有 db = pymysql.connect(host='127.0.0.1', port=3306, user='root', passwd='1234', db='testdb', charset='utf8')
3、创建游标对象cursor
#使用cursor方法创建一个游标 cursor = db.cursor()
(推荐操作系统:windows7系统、Python 3.9.1,DELL G3电脑。)
来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/4692/viewspace-2830411/ ,如需转载,请注明出处,否则将追究法律责任。