时间:2021-07-01 10:21:17 帮助过:24人阅读
# 可以指定创建数据库的路径,比如可以写成sqlite3.connect(r"E:\DEMO.db")
con = sqlite3.connect("DEMO.db")
cur = con.cursor()
sql = "CREATE TABLE IF NOT EXISTS test(id INTEGER PRIMARY KEY,name TEXT,age INTEGER)"
cur.execute(sql)
# ①:添加单条数据
data = "1,‘Desire‘,5"
cur.execute(‘INSERT INTO test VALUES (%s)‘ % data)
# ②:添加单条数据
cur.execute("INSERT INTO test values(?,?,?)", (6, "zgq", 20))
# ③:添加多条数据
cur.executemany(‘INSERT INTO test VALUES (?,?,?)‘, [(3, ‘name3‘, 19), (4, ‘name4‘, 26)])
# 方式一
cur.execute("UPDATE test SET name=? WHERE id=?", ("nihao", 1))
# 方式二
cur.execute("UPDATE test SET name=‘haha‘ WHERE id=3")
# 方式一
cur.execute("DELETE FROM test WHERE id=?", (1,))
# 方式二
cur.execute("DELETE FROM test WHERE id=3")
cur.execute("select * from Test")
print(cur.fetchall())
cur.execute("select * from Test")
print(cur.fetchone())
print(cur.fetchmany(3))
con.commit()
con.rollback()
# 关闭游标
cur.close()
# 断开数据库连接
con.close()
sql = "CREATE TABLE IF NOT EXISTS user(clo_1,clo_2,clo_3)"
cur.execute(sql)
sql = ‘INSERT INTO user VALUES (?,?,?)‘
data = [(9, ‘name3‘, 19), (‘name4‘, 26, "nihao"), (‘nihao‘, 3, 2.89)]
cur.executemany(sql, data)
con.commit()
-----------------------------------------------------------------------------
*以上便是SQLite在Python中简单的操作,查看表结构可以用Navicat for SQLite可视化工具
Python3操作SQLite数据库
标签:相对 可视化工具 back products 管理系统 导入 tab 自带 add