python导出数据到sqlite中

复制代码
import sqlite3

# 数据
data = [
    {'username': '张三', 'age': 33, 'score': 13},
    {'username': '李四', 'age': 44, 'score': 14},
    {'username': '王五', 'age': 55, 'score': 15},
]

# 连接SQLite数据库(如果不存在则创建)
conn = sqlite3.connect('test.db')

# 创建游标对象
cursor = conn.cursor()

# 创建users表,id为主键且自动增长
cursor.execute("""
    CREATE TABLE IF NOT EXISTS users (
        id INTEGER PRIMARY KEY AUTOINCREMENT,
        username TEXT NOT NULL,
        age INTEGER,
        score INTEGER
    );
""")

# 将数据插入到users表中
for user_dict in data:
    cursor.execute("""
        INSERT INTO users (username, age, score)
        VALUES (?, ?, ?)
    """, (user_dict['username'], user_dict['age'], user_dict['score']))

# 提交事务
conn.commit()

# 关闭游标和连接
cursor.close()
conn.close()
相关推荐
爱昏羔3 小时前
下篇:从检索到交互 — 物流RAG问答系统与WebUI全实现
python·langchain·agent
_Jimmy_3 小时前
Agent引用数据库知识过时的增量同步方案
人工智能·python·langchain
煎饼学大模型4 小时前
Agent 的“大脑-手“解耦架构:当推理层和工具执行层各自独立演进
数据库·人工智能·oracle·架构·agent
min(a,b)5 小时前
学习第 4 天:面向对象与异常处理
python·学习·学习方法
yangshicong6 小时前
第19章:AI安全防护与AI安全
人工智能·python·安全·prompt·ai编程
果汁华7 小时前
Function Calling 与 Python 实战完整指南
开发语言·网络·python
c_lb72887 小时前
2026年不同基础做量化,先找AI能参与的位置
人工智能·python
muddjsv8 小时前
SQLite数据库文件机制详解:journal、wal、shm文件作用与备份避坑
数据库·sqlite
chenment8 小时前
ComfyUI 自定义节点开发:从零扩展你的图像生成工作流
python·stable diffusion
IT空门:门主9 小时前
Python 基础语法学习路线图
网络·python·学习