Coursera 笔记

Module 01 · Extract, Transform, Load (ETL)提取、转换与加载(ETL)

Reading: Querying SQLite3 database阅读:查询 SQLite3 数据库

Coursera ↗

What is SQLite3? 什么是 SQLite3?

SQLite3 is an in-process Python library that implements a self-contained, serverless, zero-configuration, transactional SQL database engine. It is a popular choice as an embedded database for local/client storage in application software.
SQLite3 是一个进程内 Python 库,它实现了一个独立的、无服务器的、零配置的事务性 SQL 数据库引擎。它作为应用程序软件中用于本地/客户端存储的嵌入式数据库,是一个受欢迎的选择。

How to connect to the SQLite3? 如何连接到 SQLite3?

You can connect to SQLite3 using the connect() function by passing the required database name as an argument.
您可以通过将所需的数据库名称作为参数传递给 connect() 函数来连接到 SQLite3。

import sqlite3
sql_connection = sqlite3.connect('database.db')

This makes the variable sql_connection an object of the SQL code engine. You can then use this to run the required queries on the database.
这使得变量 sql_connection 成为 SQL 代码引擎的一个对象。然后,您可以使用它在数据库上运行所需的查询。

How to create a database table using SQLite3 and Pandas? 如何使用 SQLite3 和 Pandas 创建数据库表?

You can directly load a Pandas dataframe to a SQLite3 database object using the following syntax.
您可以使用以下语法将 Pandas 数据框直接加载到 SQLite3 数据库对象中。

df.to_sql(table_name, sql_connection, if_exists = 'replace', index = False)

Here, you use the to_sql() function to convert the pandas dataframe to an SQL table.
在这里,您使用 to_sql() 函数将 pandas 数据框转换为 SQL 表。

The table_name and sql_connection arguments specify the name of the required table and the database to which you should load the dataframe.
table_name 和 sql_connection 参数指定所需表的名称以及应将数据帧加载到的数据库。

The if_exists parameter can take any one of three possible values:
if_exists 参数可以取以下三个可能值中的任何一个:
'fail': This denies the creation of a table if one with the same name exists in the database already.
'fail' :如果数据库中已存在同名表,则拒绝创建表。
'replace': This overwrites the existing table with the same name.
'replace' :这将覆盖具有相同名称的现有表。
'append': This adds information to the existing table with the same name.
'append' :这会将信息添加到具有相同名称的现有表中。

Keep the index parameter set to True only if the index of the data being sent holds some informational value. Otherwise, keep it as False.
仅当正在发送的数据的索引具有某些信息价值时,才将 index 参数设置为 True 。否则,请将其保留为 False 。

How to query a database table using SQLite3 and Pandas? 如何使用 SQLite3 和 Pandas 查询数据库表?

You can use the Pandas function read_sql() to query a database table.
您可以使用 Pandas 函数 read_sql() 来查询数据库表。

The function returns a Pandas dataframe with the output to the query. Use the function with the following syntax:
该函数返回一个包含查询输出的 Pandas 数据框。使用以下语法调用该函数:

df = pandas.read_sql(query_statement, sql_connection)

Here, the query_statement argument contains the complete query to the required table as a string.
此处, query_statement 参数包含对所需表的完整查询,以字符串形式表示。

Example Queries 查询示例

Some typical queries with their meanings are shown in the table below.
下表显示了一些典型查询及其含义。

Query statement  查询语句 Purpose  目的
SELECT * FROM table_name Retrieve all entries of the table.
检索表中的所有条目。
SELECT COUNT(*) FROM table_name Retrieve total number of entries in the table.
检索表中条目的总数。
SELECT Column_name FROM table_name Retrieve all entries of a specific column in the table.
检索表中特定列的所有条目。
SELECT * FROM table_name WHERE <condition> Retrieve all entries of the table that meet the specified condition.
检索符合指定条件的所有表条目。

Author(s) 作者

Abhishek Gagneja