Python SQLite - 从表中选择数据
在本文中,我们将讨论Python SQLite 模块的 select 语句。此语句用于从 SQLite 表中检索数据,并返回表中包含的数据。
在 SQLite 中,Select 语句的语法是:
SELECT * FROM table_name;
* : means all the column from the table
To select specific column replace * with the column name or column names.
现在我们将在Python程序中使用 Select 语句并查看结果:
演示我们的 GEEK 表:
创建上表:
在这里,我们将使用上述方法创建表。
Python3
import sqlite3
# Connecting to sqlite
# connection object
connection_obj = sqlite3.connect('geek.db')
# cursor object
cursor_obj = connection_obj.cursor()
connection_obj.execute("""CREATE TABLE GEEK(
Email varchar(255),
Name varchar(50),
Score int
);""")
connection_obj.execute(
"""INSERT INTO GEEK (Email,Name,Score) VALUES ("geekk1@gmail.com","Geek1",25)""")
connection_obj.execute(
"""INSERT INTO GEEK (Email,Name,Score) VALUES ("geekk2@gmail.com","Geek2",15)""")
connection_obj.execute(
"""INSERT INTO GEEK (Email,Name,Score) VALUES ("geekk3@gmail.com","Geek3",36)""")
connection_obj.execute(
"""INSERT INTO GEEK (Email,Name,Score) VALUES ("geekk4@gmail.com","Geek4",27)""")
connection_obj.execute(
"""INSERT INTO GEEK (Email,Name,Score) VALUES ("geekk5@gmail.com","Geek5",40)""")
connection_obj.execute(
"""INSERT INTO GEEK (Email,Name,Score) VALUES ("geekk6@gmail.com","Geek6",36)""")
connection_obj.execute(
"""INSERT INTO GEEK (Email,Name,Score) VALUES ("geekk7@gmail.com","Geek7",27)""")
connection_obj.commit()
# Close the connection
connection_obj.close()
Python3
import sqlite3
# Connecting to sqlite
# connection object
connection_obj = sqlite3.connect('geek.db')
# cursor object
cursor_obj = connection_obj.cursor()
# to select all column we will use
statement = '''SELECT * FROM GEEK'''
cursor_obj.execute(statement)
print("All the data")
output = cursor_obj.fetchall()
for row in output:
print(row)
connection_obj.commit()
# Close the connection
connection_obj.close()
Python3
import sqlite3
# Connecting to sqlite
# connection object
connection_obj = sqlite3.connect('geek.db')
# cursor object
cursor_obj = connection_obj.cursor()
# to select all column we will use
statement = '''SELECT * FROM GEEK'''
cursor_obj.execute(statement)
print("Limited data")
output = cursor_obj.fetchmany(5)
for row in output:
print(row)
connection_obj.commit()
# Close the connection
connection_obj.close()
Python3
import sqlite3
# Connecting to sqlite
# connection object
connection_obj = sqlite3.connect('geek.db')
# cursor object
cursor_obj = connection_obj.cursor()
# to select all column we will use
statement = '''SELECT * FROM GEEK'''
cursor_obj.execute(statement)
print("Only one data")
output = cursor_obj.fetchone()
print(output)
connection_obj.commit()
# Close the connection
connection_obj.close()
读取所有行:
现在我们将使用 Select 语句从表中检索数据并获取所有记录。要获取所有记录,我们将使用 fetchall() 方法。
Syntax: cursor.fetchall()
where, cursor is an object of sqlite3 connection with database.
代码:
蟒蛇3
import sqlite3
# Connecting to sqlite
# connection object
connection_obj = sqlite3.connect('geek.db')
# cursor object
cursor_obj = connection_obj.cursor()
# to select all column we will use
statement = '''SELECT * FROM GEEK'''
cursor_obj.execute(statement)
print("All the data")
output = cursor_obj.fetchall()
for row in output:
print(row)
connection_obj.commit()
# Close the connection
connection_obj.close()
输出:
阅读一些行:
现在我们将使用 Select 语句从表中检索数据并获取许多记录,而不是全部。要获取许多记录,我们将使用 fetchmany() 方法。
Syntax: cursor.fetchmany(size)
Parameters: size – a limit to fetch records
where, cursor is an object of sqlite3 connection with database.
代码:
蟒蛇3
import sqlite3
# Connecting to sqlite
# connection object
connection_obj = sqlite3.connect('geek.db')
# cursor object
cursor_obj = connection_obj.cursor()
# to select all column we will use
statement = '''SELECT * FROM GEEK'''
cursor_obj.execute(statement)
print("Limited data")
output = cursor_obj.fetchmany(5)
for row in output:
print(row)
connection_obj.commit()
# Close the connection
connection_obj.close()
输出:
只读一行:
现在 e 将使用 Select 语句从表中检索数据并仅获取一条记录。要仅获取一条记录,我们将使用 fetchone() 方法。
Syntax: cursor.fetchone()
where, cursor is an object of sqlite3 connection with database.
蟒蛇3
import sqlite3
# Connecting to sqlite
# connection object
connection_obj = sqlite3.connect('geek.db')
# cursor object
cursor_obj = connection_obj.cursor()
# to select all column we will use
statement = '''SELECT * FROM GEEK'''
cursor_obj.execute(statement)
print("Only one data")
output = cursor_obj.fetchone()
print(output)
connection_obj.commit()
# Close the connection
connection_obj.close()
输出: