Home  >  Article  >  Database  >  How to connect python to mysql database

How to connect python to mysql database

王林
王林forward
2023-06-03 09:32:041630browse

1. Import module

#导入模块
import pymysql

2. Open database connection

#打开数据库连接
#注意:这里已经假定存在数据库testdb,db指定了连接的数据库,当然这个参数也可以没有
db = pymysql.connect(host='127.0.0.1', port=3306, user='root', passwd='1234', db='testdb', charset='utf8')

3. Create cursor object cursor

#使用cursor方法创建一个游标
cursor = db.cursor()

The above is the detailed content of How to connect python to mysql database. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:yisu.com. If there is any infringement, please contact admin@php.cn delete