python 连接mysql
时间: 2023-04-10 22:02:55 浏览: 79
可以使用 Python 的 MySQLdb 模块来连接 MySQL 数据库。以下是一个简单的示例代码:
```python
import MySQLdb
# 打开数据库连接
db = MySQLdb.connect("localhost", "username", "password", "database_name")
# 使用 cursor() 方法创建一个游标对象 cursor
cursor = db.cursor()
# 使用 execute() 方法执行 SQL 查询
cursor.execute("SELECT VERSION()")
# 使用 fetchone() 方法获取单条数据
data = cursor.fetchone()
print("Database version : %s " % data)
# 关闭数据库连接
db.close()
```
注意:在实际使用中,需要根据具体情况修改连接参数。
阅读全文
相关推荐













