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()
相关推荐
Black_Rock_br20 分钟前
打通 PyTorch Monarch 与 ROCm:单 Controller 架构的异构算力实战
人工智能·pytorch·python·开源
其实防守也摸鱼26 分钟前
Kimi K3深度测评:长文本之外的真实力
运维·开发语言·网络·人工智能·python·学习·安全
weixin_BYSJ198729 分钟前
springboot3家政平台小程序--附源码00904
java·javascript·spring boot·python·django·flask·php
风痕天际1 小时前
Pytorch开发教程1——CUDA安装
人工智能·pytorch·python
geovindu1 小时前
python: Breadth First Search Algorithm and Depth First Search Algorithm
开发语言·后端·python·算法·搜索算法
程序员果子1 小时前
CrewAI :当 Agent 学会团队协作
人工智能·git·python·多智能体·agent框架
玉鸯1 小时前
向量检索不是记忆:Agent 记忆的三层进化
python·agent
大模型码小白1 小时前
Java 部署:Jenkins Pipeline 构建 Java 项目(自动化)
java·人工智能·python·机器学习
runafterhit1 小时前
python基础语法命令(C程序员刷leetcode)
c语言·python·leetcode
paeamecium1 小时前
【PAT甲级真题】- Kuchiguse (20)
数据结构·c++·python·算法·pat考试·pat