Home > Database > Mysql Tutorial > body text

How to connect python to mysql database

王林
Release: 2023-06-03 09:32:04
forward
1635 people have browsed it

1. Import module

#导入模块
import pymysql
Copy after login

2. Open database connection

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

3. Create cursor object cursor

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

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!

Related labels:
source:yisu.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template