Python 数据访问教程

Python 数据访问主页

Python MySQL

Python MySQL 简介 Python MySQL 数据库连接 Python MySQL 创建数据库 Python MySQL 创建表 Python MySQL 插入数据 Python MySQL 选择数据 Python MySQL Where 子句 Python MySQL 排序 Python MySQL 更新表 Python MySQL 删除数据 Python MySQL 删除表 Python MySQL Limit 子句 Python MySQL 连接 Python MySQL 游标对象

Python PostgreSQL

Python PostgreSQL 简介 Python PostgreSQL 数据库连接 Python PostgreSQL 创建数据库 Python PostgreSQL 创建表 Python PostgreSQL 插入数据 Python PostgreSQL 选择数据 Python PostgreSQL Where 子句 Python PostgreSQL 排序 Python PostgreSQL 更新表 Python PostgreSQL 删除数据 Python PostgreSQL 删除表 Python PostgreSQL Limit 子句 Python PostgreSQL 连接 Python PostgreSQL 游标对象

Python SQLite

Python SQLite 简介 Python SQLite 建立连接 Python SQLite 创建表 Python SQLite 插入数据 Python SQLite 选择数据 Python SQLite Where 子句 Python SQLite 排序 Python SQLite 更新表 Python SQLite 删除数据 Python SQLite 删除表 Python SQLite Limit 子句 Python SQLite 连接 Python SQLite 游标对象

Python MongoDB

Python MongoDB 简介 Python MongoDB 创建数据库 Python MongoDB 创建集合 Python MongoDB 插入文档 Python MongoDB 查找 Python MongoDB 查询 Python MongoDB 排序 Python MongoDB 删除文档 Python MongoDB 删除集合 Python MongoDB 更新 Python MongoDB Limit 子句

Python 数据访问资源

Python 数据访问 快速指南 Python 数据访问 有用资源 Python 数据访问 讨论


Python SQLite - Join 连接

将数据分成两个表后,可以使用连接从这两个表中获取组合记录。

示例

假设我们使用以下查询创建了一个名为 CRICKETERS 的表 −

sqlite> CREATE TABLE CRICKETERS (
   First_Name VARCHAR(255),
   Last_Name VARCHAR(255),
   Age int,
   Place_Of_Birth VARCHAR(255),
   Country VARCHAR(255)
);
sqlite>

让我们再创建一个表 OdiStats,描述 CRICKETERS 表中每个球员的单日统计数据。

sqlite> CREATE TABLE ODIStats (
   First_Name VARCHAR(255),
   Matches INT,
   Runs INT,
   AVG FLOAT,
   Centuries INT,
   HalfCenturies INT
);
sqlite>

以下语句检索结合这两个表中的值的数据 −

sqlite> SELECT
   Cricketers.First_Name, Cricketers.Last_Name, Cricketers.Country,
   OdiStats.matches, OdiStats.runs, OdiStats.centuries, OdiStats.halfcenturies
   from Cricketers INNER JOIN OdiStats ON Cricketers.First_Name = OdiStats.First_Name;
First_Name Last_Name    Country  Matches    Runs          Centuries  HalfCenturies
---------- ----------   -------  ---------- ------------- ---------- ----------
Shikhar    Dhawan       Indi     133        5518          17         27
Jonathan   Trott        Sout     68         2819          4          22
Kumara     Sangakkara   Sril     404        14234         25         93
Virat      Kohli        Indi     239        11520         43         54
Rohit      Sharma       Indi     218        8686          24         42
sqlite>

使用 python 的 Join 子句

以下 SQLite 示例演示了使用 python 的 JOIN 子句 −

import sqlite3

#连接到 sqlite
conn = sqlite3.connect('example.db')

#使用 cursor() 方法创建游标对象
cursor = conn.cursor()

#检索数据
sql = '''SELECT * from EMP INNER JOIN CONTACT ON EMP.CONTACT = CONTACT.ID'''

#执行查询
cursor.execute(sql)

#从表中获取第一行
result = cursor.fetchall();
print(result)

#在数据库中提交你的更改
conn.commit()

#关闭连接
conn.close()

输出

[('Ramya', 'Rama priya', 27, 'F', 9000.0, 101, 101, 'Krishna@mymail.com', 'Hyderabad'), 
   ('Vinay', 'Battacharya', 20, 'M', 6000.0, 102, 102,'Raja@mymail.com', 'Vishakhapatnam'), 
   ('Sharukh', 'Sheik', 25, 'M', 8300.0, 103, 103, 'Krishna@mymail.com', 'Pune'), 
   ('Sarmista', 'Sharma', 26, 'F', 10000.0, 104, 104, 'Raja@mymail.com', 'Mumbai')]