惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

有赞技术团队
有赞技术团队
M
MIT News - Artificial intelligence
Hugging Face - Blog
Hugging Face - Blog
博客园 - 聂微东
量子位
S
SegmentFault 最新的问题
V
Visual Studio Blog
博客园 - 【当耐特】
Apple Machine Learning Research
Apple Machine Learning Research
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
小众软件
小众软件
Stack Overflow Blog
Stack Overflow Blog
Vercel News
Vercel News
D
Docker
J
Java Code Geeks
博客园 - 三生石上(FineUI控件)
博客园 - Franky
Recent Announcements
Recent Announcements
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
MongoDB | Blog
MongoDB | Blog
D
DataBreaches.Net
Y
Y Combinator Blog
云风的 BLOG
云风的 BLOG
V
V2EX

博客园 - 玛雅人

五大学习方法 Python AI 与深度学习:每周任务拆分及代码练手 Python AI 与深度学习 - D2.MNIST 手写数字识别 Python基础 - 常用类库汇总 Python AI 与深度学习 - D1.PyTorch 深度学习环境一键配置 ARDUINO - P2:按钮控制LED ARDUINO - P1:BLink IIS8.5 安装证书 Kendo 计算字段 Kendo UI 的 k-template UpdatePanel中用后台CS代码调用JS代码,先执行控件事件,后触发JS SQL常用 Node.js 安装 生成缩略图 有用的JS函数 vs2010 mvc3 运算符 || && 如何阻止ASP.NET的按钮控件提交页面 IIS Form 认证 保护HTML页面
Python调用SQLLite3
玛雅人 · 2026-01-29 · via 博客园 - 玛雅人

import sqlite3

#with sqlite3.connect('test.db') as conn:

with sqlite3.connect(':memory:') as conn:

    cursor = conn.cursor()

    # 3. 执行SQL:创建表

    create_sql = '''

    CREATE TABLE IF NOT EXISTS student (

        id INTEGER PRIMARY KEY AUTOINCREMENT,

        name TEXT NOT NULL,

        age INTEGER,

        score REAL

    )

    '''

    cursor.execute(create_sql)

    # 4. 执行SQL:新增数据(单条)

    cursor.execute("INSERT INTO student (name, age, score) VALUES (?, ?, ?)", ('张三', 18, 95.5))

    # 批量新增(executemany + 列表元组)

    data_list = [('李四', 19, 92.0), ('王五', 17, 88.5), ('赵六', 18, 90.0)]

    cursor.executemany("INSERT INTO student (name, age, score) VALUES (?, ?, ?)", data_list)

    # 5. 提交事务(增/删/改必须提交,查询无需)

    conn.commit()

    print("数据操作成功!")

    # 6. 执行SQL:查询数据

    cursor.execute("SELECT * FROM student WHERE age >= 18")

    # 获取查询结果:fetchall()(所有)/ fetchone()(单条)/ fetchmany(n)(n条)

    results = cursor.fetchall()

    print("查询结果:")

    for row in results:

        print(f"ID:{row[0]}, 姓名:{row[1]}, 年龄:{row[2]}, 成绩:{row[3]}")