Python中可以使用sqlite3模块来操作SQLite数据库。下面是一个简单的例子:

  1. 连接数据库

首先需要连接到SQLite数据库:

import sqlite3

conn = sqlite3.connect('example.db')
  1. 创建表
c = conn.cursor()

c.execute('''CREATE TABLE stocks
             (date text, trans text, symbol text, qty real, price real)''')
  1. 插入数据
c.execute("INSERT INTO stocks VALUES ('2006-01-05','BUY','RHAT',100,35.14)")

conn.commit()
  1. 查询数据
c.execute("SELECT * FROM stocks")
print(c.fetchall())
  1. 关闭连接
conn.close()

完整代码:

import sqlite3

conn = sqlite3.connect('example.db')

c = conn.cursor()

c.execute('''CREATE TABLE stocks
             (date text, trans text, symbol text, qty real, price real)''')

c.execute("INSERT INTO stocks VALUES ('2006-01-05','BUY','RHAT',100,35.14)")

conn.commit()

c.execute("SELECT * FROM stocks")
print(c.fetchall())

conn.close()
``
python 操作SQLite数据库

原文地址: https://www.cveoy.top/t/topic/hvCJ 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录