在前几篇文章中,数据库的记录仅限于较小的数据和单个元组。本文将解释如何使用涵盖所有异常的模块SQLite3从数据库中写入和获取大数据。
一种简单的方法是执行查询并使用fetchall()。
1.executescript()
这是一次可以同时执行多个SQL语句的便捷方法。它执行作为参数获取的SQL脚本。
语法:
sqlite3.connect.executescript(script)
import sqlite3 # Connection with the DataBase # 'library.db' connection = sqlite3.connect("library.db") cursor = connection.cursor() # SQL piece of code Executed # SQL piece of code Executed 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) # The output in fetched and returned # as a List by fetchall() result