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()

相关推荐

  1. 连接 SQLite 数据库

    2024-03-09 23:22:05       10 阅读
  2. c# sqlite导出导入数据表 作为sql文件

    2024-03-09 23:22:05       21 阅读
  3. Python SQLite数据库处理空值几种方法

    2024-03-09 23:22:05       13 阅读
  4. PythonSQLite的应用:从入门进阶

    2024-03-09 23:22:05       7 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-03-09 23:22:05       18 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-03-09 23:22:05       19 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-03-09 23:22:05       18 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-03-09 23:22:05       20 阅读

热门阅读

  1. Spring Authorization Server简介

    2024-03-09 23:22:05       20 阅读
  2. QNX开发用什么语言?QNX和FreeRTOS什么关系?

    2024-03-09 23:22:05       22 阅读
  3. 深入了解C#中的垃圾回收(Garbage Collection)

    2024-03-09 23:22:05       23 阅读