python mysql数据库
时间: 2023-11-14 09:08:46 浏览: 40
好的,关于 Python 连接 MySQL 数据库,你可以使用 Python 的 MySQLdb 模块或者 PyMySQL 模块。其中,MySQLdb 模块需要安装 MySQL C API,而 PyMySQL 模块则不需要。下面是一个使用 PyMySQL 模块连接 MySQL 数据库的示例代码:
```python
import pymysql
# 打开数据库连接
db = pymysql.connect(host="localhost", user="root", password="password", database="test")
# 使用 cursor() 方法创建一个游标对象 cursor
cursor = db.cursor()
# 使用 execute() 方法执行 SQL 查询
cursor.execute("SELECT VERSION()")
# 使用 fetchone() 方法获取单条数据.
data = cursor.fetchone()
print("Database version : %s " % data)
# 关闭数据库连接
db.close()
```
相关问题
python MySQL数据库封装
Python MySQL数据库封装是将Python程序与MySQL数据库进行交互的一种方法,可以将常用的数据库操作封装成函数,以便在程序中使用。
以下是一个简单的Python MySQL数据库封装示例:
```python
import pymysql
class MySQL:
def __init__(self, host, user, password, database, port=3306, charset='utf8'):
self.host = host
self.user = user
self.password = password
self.database = database
self.port = port
self.charset = charset
self.conn = pymysql.connect(host=self.host, user=self.user, password=self.password, database=self.database, port=self.port, charset=self.charset)
self.cursor = self.conn.cursor()
def query(self, sql, params=None):
self.cursor.execute(sql, params)
return self.cursor.fetchall()
def execute(self, sql, params=None):
self.cursor.execute(sql, params)
self.conn.commit()
def close(self):
self.cursor.close()
self.conn.close()
```
在上面的代码中,我们首先使用pymysql库连接到MySQL数据库,然后定义了一个MySQL类,它包含了查询和执行两个方法。其中,查询方法query()接受一个SQL查询语句和可选的参数列表,并返回查询结果;执行方法execute()接受一个SQL语句和可选的参数列表,并执行这个SQL语句。
在使用这个封装的MySQL类时,只需要创建一个MySQL对象,并调用其方法即可,例如:
```python
db = MySQL('localhost', 'root', 'password', 'test')
result = db.query('SELECT * FROM users')
db.execute('INSERT INTO users (name, age) VALUES (%s, %s)', ('John', 25))
db.close()
```
在上面的示例中,我们首先创建了一个MySQL对象,然后使用查询方法query()查询了users表中的所有记录,并使用执行方法execute()向users表中插入了一条记录,最后关闭了数据库连接。
通过这种封装方法,我们可以在Python程序中使用更加简洁、易读的方式进行MySQL数据库操作。
python mysql 数据库接口的应用
Python 中有很多数据库接口模块可以与 MySQL 数据库进行交互,比如 `mysql-connector-python` 和 `pymysql` 等。这里以 `pymysql` 模块为例,演示如何使用 Python 连接 MySQL 数据库,并执行一些常见的操作。
首先,需要安装 `pymysql` 模块。你可以使用 pip 命令进行安装:
```
pip install pymysql
```
接下来,可以编写一个简单的 Python 脚本,连接到 MySQL 数据库,并执行一些操作。下面的代码演示了如何连接到数据库、创建表、插入数据、查询数据、更新数据和删除数据:
```python
import pymysql
# 连接到数据库
conn = pymysql.connect(
host='localhost',
port=3306,
user='root',
password='password',
database='test'
)
# 创建表
cursor = conn.cursor()
cursor.execute('''
CREATE TABLE IF NOT EXISTS users (
id INT(11) NOT NULL AUTO_INCREMENT,
name VARCHAR(255) NOT NULL,
age INT(11) NOT NULL,
PRIMARY KEY (id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
''')
# 插入数据
cursor.execute("INSERT INTO users (name, age) VALUES (%s, %s)", ('Alice', 20))
cursor.execute("INSERT INTO users (name, age) VALUES (%s, %s)", ('Bob', 25))
conn.commit()
# 查询数据
cursor.execute("SELECT id, name, age FROM users WHERE age > %s", (20,))
rows = cursor.fetchall()
for row in rows:
print(row)
# 更新数据
cursor.execute("UPDATE users SET age = %s WHERE name = %s", (30, 'Alice'))
conn.commit()
# 删除数据
cursor.execute("DELETE FROM users WHERE name = %s", ('Bob',))
conn.commit()
# 关闭连接
cursor.close()
conn.close()
```
在上面的代码中,首先使用 `pymysql.connect()` 方法连接到 MySQL 数据库。然后,使用 `cursor.execute()` 方法执行 SQL 语句,包括创建表、插入数据、查询数据、更新数据和删除数据等操作。执行更新和删除操作后,需要使用 `conn.commit()` 方法提交更改。最后,使用 `cursor.close()` 和 `conn.close()` 方法关闭连接。
这只是一个简单的示例,实际应用中可能需要更复杂的操作。但是,使用 `pymysql` 模块可以很方便地与 MySQL 数据库进行交互。