📜  Python 中的 SQL 连接 - SQL (1)

📅  最后修改于: 2023-12-03 14:46:11.837000             🧑  作者: Mango

Python 中的 SQL 连接 - SQL

SQL (Structured Query Language) 是一种用于管理关系数据库(RDBMS)的标准语言,Python 提供了多个 SQL 连接库,其中最受欢迎的是 sqlite3, mysql-connector-python, psycopg2(PostgreSQL), pyodbc(Microsoft SQL Server) 等。

sqlite3

sqlite3 是 Python 标准库的一部分,是一个轻量级的关系数据库管理系统(RDBMS)。它不需要单独的服务进程或操作系统,可以在大多数操作系统上直接使用。以下是一个简单的 sqlite3 连接示例:

import sqlite3

# 连接到数据库
conn = sqlite3.connect('example.db')

# 创建表
conn.execute('''CREATE TABLE COMPANY
             (ID INT PRIMARY KEY NOT NULL,
             NAME TEXT NOT NULL,
             AGE INT NOT NULL,
             ADDRESS CHAR(50),
             SALARY REAL);''')
print("Table created successfully")

# 插入数据
conn.execute("INSERT INTO COMPANY (ID, NAME, AGE, ADDRESS, SALARY) \
             VALUES (1, 'Paul', 32, 'California', 20000.00)")
conn.execute("INSERT INTO COMPANY (ID, NAME, AGE, ADDRESS, SALARY) \
             VALUES (2, 'Allen', 25, 'Texas', 15000.00)")
conn.execute("INSERT INTO COMPANY (ID, NAME, AGE, ADDRESS, SALARY) \
             VALUES (3, 'Teddy', 23, 'Norway', 20000.00)")
conn.execute("INSERT INTO COMPANY (ID, NAME, AGE, ADDRESS, SALARY) \
             VALUES (4, 'Mark', 25, 'Rich-Mond', 65000.00)")
conn.commit()
print("Records created successfully")

# 查询数据
cursor = conn.execute("SELECT id, name, address, salary from COMPANY")
for row in cursor:
    print("ID = ", row[0])
    print("NAME = ", row[1])
    print("ADDRESS = ", row[2])
    print("SALARY = ", row[3], "\n")

# 关闭连接
conn.close()
mysql-connector-python

mysql-connector-python 是 MySQL 官方提供的 Python 驱动,支持 Python 3。它可以通过 pip 安装:

pip install mysql-connector-python

以下是一个 mysql-connector-python 连接示例:

import mysql.connector

# 连接到 MySQL 数据库
cnx = mysql.connector.connect(user='username', password='password',
                              host='127.0.0.1',
                              database='database_name')

# 创建表
cursor = cnx.cursor()
cursor.execute('''CREATE TABLE COMPANY
             (ID INT PRIMARY KEY NOT NULL,
             NAME TEXT NOT NULL,
             AGE INT NOT NULL,
             ADDRESS CHAR(50),
             SALARY REAL);''')
print("Table created successfully")

# 插入数据
cursor.execute("INSERT INTO COMPANY (ID, NAME, AGE, ADDRESS, SALARY) \
             VALUES (1, 'Paul', 32, 'California', 20000.00)")
cursor.execute("INSERT INTO COMPANY (ID, NAME, AGE, ADDRESS, SALARY) \
             VALUES (2, 'Allen', 25, 'Texas', 15000.00)")
cursor.execute("INSERT INTO COMPANY (ID, NAME, AGE, ADDRESS, SALARY) \
             VALUES (3, 'Teddy', 23, 'Norway', 20000.00)")
cursor.execute("INSERT INTO COMPANY (ID, NAME, AGE, ADDRESS, SALARY) \
             VALUES (4, 'Mark', 25, 'Rich-Mond', 65000.00)")
cnx.commit()
print("Records created successfully")

# 查询数据
cursor.execute("SELECT id, name, address, salary from COMPANY")
for row in cursor:
    print("ID = ", row[0])
    print("NAME = ", row[1])
    print("ADDRESS = ", row[2])
    print("SALARY = ", row[3], "\n")

# 关闭连接
cursor.close()
cnx.close()
psycopg2

psycopg2 是 PostgreSQL 官方提供的 Python 驱动,支持 Python 3。它可以通过 pip 安装:

pip install psycopg2

以下是一个 psycopg2 连接示例:

import psycopg2

# 连接到 PostgreSQL 数据库
conn = psycopg2.connect(database="database_name", user="username",
                        password="password", host="127.0.0.1", port="5432")

# 创建表
cur = conn.cursor()
cur.execute('''CREATE TABLE COMPANY
             (ID INT PRIMARY KEY NOT NULL,
             NAME TEXT NOT NULL,
             AGE INT NOT NULL,
             ADDRESS CHAR(50),
             SALARY REAL);''')
print("Table created successfully")

# 插入数据
cur.execute("INSERT INTO COMPANY (ID, NAME, AGE, ADDRESS, SALARY) \
             VALUES (1, 'Paul', 32, 'California', 20000.00)")
cur.execute("INSERT INTO COMPANY (ID, NAME, AGE, ADDRESS, SALARY) \
             VALUES (2, 'Allen', 25, 'Texas', 15000.00)")
cur.execute("INSERT INTO COMPANY (ID, NAME, AGE, ADDRESS, SALARY) \
             VALUES (3, 'Teddy', 23, 'Norway', 20000.00)")
cur.execute("INSERT INTO COMPANY (ID, NAME, AGE, ADDRESS, SALARY) \
             VALUES (4, 'Mark', 25, 'Rich-Mond', 65000.00)")
conn.commit()
print("Records created successfully")

# 查询数据
cur.execute("SELECT id, name, address, salary from COMPANY")
rows = cur.fetchall()
for row in rows:
    print("ID = ", row[0])
    print("NAME = ", row[1])
    print("ADDRESS = ", row[2])
    print("SALARY = ", row[3], "\n")

# 关闭连接
cur.close()
conn.close()
pyodbc

pyodbc 是一个用于连接 Microsoft SQL Server 的 Python 包。它可以通过 pip 安装:

pip install pyodbc

以下是一个 pyodbc 连接示例:

import pyodbc

# 连接到 Microsoft SQL Server 数据库
conn = pyodbc.connect('DRIVER={SQL Server};SERVER=localhost\SQLEXPRESS;DATABASE=database_name;UID=username;PWD=password')

# 创建表
cursor = conn.cursor()
cursor.execute('''CREATE TABLE COMPANY
             (ID INT PRIMARY KEY NOT NULL,
             NAME TEXT NOT NULL,
             AGE INT NOT NULL,
             ADDRESS CHAR(50),
             SALARY REAL);''')
print("Table created successfully")

# 插入数据
cursor.execute("INSERT INTO COMPANY (ID, NAME, AGE, ADDRESS, SALARY) \
             VALUES (1, 'Paul', 32, 'California', 20000.00)")
cursor.execute("INSERT INTO COMPANY (ID, NAME, AGE, ADDRESS, SALARY) \
             VALUES (2, 'Allen', 25, 'Texas', 15000.00)")
cursor.execute("INSERT INTO COMPANY (ID, NAME, AGE, ADDRESS, SALARY) \
             VALUES (3, 'Teddy', 23, 'Norway', 20000.00)")
cursor.execute("INSERT INTO COMPANY (ID, NAME, AGE, ADDRESS, SALARY) \
             VALUES (4, 'Mark', 25, 'Rich-Mond', 65000.00)")
conn.commit()
print("Records created successfully")

# 查询数据
cursor.execute("SELECT id, name, address, salary from COMPANY")
rows = cursor.fetchall()
for row in rows:
    print("ID = ", row[0])
    print("NAME = ", row[1])
    print("ADDRESS = ", row[2])
    print("SALARY = ", row[3], "\n")

# 关闭连接
cursor.close()
conn.close()

以上就是 Python 中一些主要 SQL 连接库的使用介绍。需要根据实际情况选择适合自己的库进行数据库操作。