SQLite数据库操作全解析
1. 基本查询操作
在SQLite数据库操作中,我们常常需要从表中获取数据。下面通过几个具体的程序示例来详细了解基本的查询操作。
1.1 使用 fetchone 方法逐行获取数据
以下是一个使用 fetchone 方法逐行获取数据的Python代码示例:
import sqlite3
def main():
# Connect to the database.
conn = sqlite3.connect('chocolate.db')
# Get a cursor.
cur = conn.cursor()
# Select all columns from each row from the Products table.
cur.execute('SELECT Description, RetailPrice FROM Products')
# Fetch the first row of the results.
row = cur.fetchone()
while row != None:
# Display the row.
print(f'{row[0]:30} {row[1]:5}')
# Fetch the next row.
row = cur.fetchone()
# Close the database connection.
超级会员免费看
订阅专栏 解锁全文
687

被折叠的 条评论
为什么被折叠?



