📅  最后修改于: 2020-04-10 14:57:19             🧑  作者: Mango
推荐:
在先前的文章中,数据库的记录仅限于小型和单元组。本文将说明如何使用涵盖所有异常的模块SQLite3从数据库写入和获取大数据。
一种简单的方法是执行查询并使用fetchall()。1中已经对此进行了讨论。
sqlite3.connect.executescript(script)
import sqlite3
# 与数据库连接
# 'library.db'
connection = sqlite3.connect("library.db")
cursor = connection.cursor()
# SQL代码已执行
cursor.executescript("""
CREATE TABLE people(
firstname,
lastname,
age
);
CREATE TABLE book(
title,
author,
published
);
INSERT INTO
book(title, author, published)
VALUES (
'Dan Clarke''s GFG Detective Agency',
'Sean Simpsons',
1987
);
""")
sql = """
SELECT COUNT(*) FROM book;"""
cursor.execute(sql)
# 通过fetchall()获取并以列表形式返回的输出
result = cursor.fetchall()
print(result)
sql = """
SELECT * FROM book;"""
cursor.execute(sql)
result = cursor.fetchall()
print(result)
# 更改保存到数据库
connection.commit()
# 与数据库的连接已关闭(断开)
connection.close()
输出:
[(1,)]
[("Dan Clarke's GFG Detective Agency", 'Sean Simpsons', 1987)]
import sqlite3
# 连接数据库
# 'library.db'
connection = sqlite3.connect("library.db")
cursor = connection.cursor()
# SQL代码已执行
cursor.execute("""
CREATE TABLE book(
title,
author,
published);""")
List = [('A', 'B', 2008), ('C', 'D', 2008),
('E', 'F', 2010)]
connection. executemany("""
INSERT INTO
book(title, author, published)
VALUES (?, ?, ?)""", List)
sql = """
SELECT * FROM book;"""
cursor.execute(sql)
result = cursor.fetchall()
for x in result:
print(x)
# 更改保存到数据库
connection.commit()
# 与数据库的连接已关闭(断开)
connection.close()
输出:
Traceback (most recent call last):
File "C:/Users/GFG/Desktop/SQLITE3.py", line 16, in
List[2][3] =[['A', 'B', 2008], ['C', 'D', 2008], ['E', 'F', 2010]]
NameError: name 'List' is not defined
使用executemany()可以使一段代码起作用。
import sqlite3
# 与数据库的连接
# 'library.db'
connection = sqlite3.connect("library.db")
cursor = connection.cursor()
# SQL代码已执行
cursor.execute("""
CREATE TABLE book(
title,
author,
published);""")
List = [('A', 'B', 2008), ('C', 'D', 2008),
('E', 'F', 2010)]
connection. executemany("""
INSERT INTO
book(title, author, published)
VALUES (?, ?, ?)""", List)
sql = """
SELECT * FROM book;"""
cursor.execute(sql)
result = cursor.fetchall()
for x in result:
print(x)
# 更改保存到数据库
connection.commit()
# 与数据库的连接已关闭(断开)
connection.close()
输出:
('A', 'B', 2008)
('C', 'D', 2008)
('E', 'F', 2010)
获取大数据
import sqlite3
# 使用sqlite3.connect()使用数据库创建的连接
connection = sqlite3.connect("company.db")
cursor = connection.cursor()
# 执行创建表命令
sql = """
CREATE TABLE employee (
ID INTEGER PRIMARY KEY,
fname VARCHAR(20),
lname VARCHAR(30),
gender CHAR(1),
dob DATE);"""
cursor.execute(sql)
# 插入单个元组
sql = """
INSERT INTO employee
VALUES (1007, "Will", "Olsen", "M", "24-SEP-1865");"""
cursor.execute(sql)
# 插入了多行
List = [(1008, 'Rkb', 'Boss', 'M', "27-NOV-1864"),
(1098, 'Sak', 'Rose', 'F', "27-DEC-1864"),
(1908, 'Royal', 'Bassen', "F", "17-NOV-1894")]
connection. executemany(
"INSERT INTO employee VALUES (?, ?, ?, ?, ?)", List)
print("Method-1\n")
# 从数据库中获取了多行
for row in connection.execute('SELECT * FROM employee ORDER BY ID'):
print (row)
print("\nMethod-2\n")
# 方法2获取多个行
sql = """
SELECT * FROM employee ORDER BY ID;"""
cursor.execute(sql)
result = cursor.fetchall()
for x in result:
print(x)
connection.commit()
connection.close()
输出:
Method-1
(1007, 'Will', 'Olsen', 'M', '24-SEP-1865')
(1008, 'Rkb', 'Boss', 'M', '27-NOV-1864')
(1098, 'Sak', 'Rose', 'F', '27-DEC-1864')
(1908, 'Royal', 'Bassen', 'F', '17-NOV-1894')
Method-2
(1007, 'Will', 'Olsen', 'M', '24-SEP-1865')
(1008, 'Rkb', 'Boss', 'M', '27-NOV-1864')
(1098, 'Sak', 'Rose', 'F', '27-DEC-1864')
(1908, 'Royal', 'Bassen', 'F', '17-NOV-1894')
注意:由于具有创建/写入数据库的权限,因此这段代码可能不适用于在线解释器。