09.Python数据库编程
SQLLite的增删查改操作
导入SQLLite模块
# 导入SQLLite模块
import sqlite3
获取连接对象connection
# 获取连接对象connection
conn = sqlite3.connect("questions.db")
print(conn)
获取游标对象cursor
# 游标对象cursor
c = conn.cursor()
关闭连接对象和游标
c.close()
conn.close()
创建table
c.execute("""
create table question(
_id integer primary key autoincrement,
question test,
optionA test,
optionB test,
optionC test,
optionD test,
answer
)
""")
在这里我们打开questions.db是看不懂的,这个时候我们需要需要用到辅助工具下图两个任意一个即可
增
添加单条数据
c.execute("insert into question values(null,?,?,?,?,?,?)", ("你好,世界", "1月", "2月", "3月", "4月", "A"))
添加多条数据
方式一
for i in range(10):
c.execute("insert into question values(null,?,?,?,?,?,?)", ("你好,世界", "1月", "2月", "3月", "4月", "A"))
方式二
c.executemany("insert into question values(null,?,?,?,?,?,?)", (
("你好,世界", "1月", "2月", "3月", "4月", "A"),
("你好啊,世界", "1月", "2月", "3月",