• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

SQLite学习笔记

武飞扬头像
这个实现不了
帮助2

  • SQLite数据库是一种嵌入式数据库,它的数据库就是一个文件scores.db
  • 经常被集成到各种应用程序中,甚至ios、Android、Mac OS、Linux
  • python中内置了Sqlite数据库,直接使用
  • 数据库:关系型数据库,一个数据库中会有多张表,表和表之间通过主外键进行关联
  • python中操作sqlite数据库
  • 获取connection
  • 连接之后需打开游标,cursor,通过cursor执行sql语句
  • 关闭连接,释放资源

例:

#导入模块 sqlite3
import sqlite3
#数据库文件
db_file = 'scores.db'
#获取与数据库的连接
conn = sqlite3.connect(db_file)
#编写sql语句
sql = 'select * from scores'
#执行sql语句
cur = conn.cursor()
cur.execute(sql)
#打印结果
print(cur.fetchall())
#关闭连接
conn.close()

插入数据:

#插入sql语句
insert into   表名 (列1,列2, ...) values(?,?,...)

删除sql语句

#删除sql语句
delete from   表名 where 列=?

修改

#修改sql语句
update 表名 set 列名=? ... where 条件

插入多条数据
执行sql语句,插入多条数据使用的函数时executemany

score_list = [('jack,80,90'),('bob',75,95),('rose',60,70)]
def insert():
    # 获取连接
    conn = sqlite3.connect(db_file)
    #打开游标
    cur = conn.cursor()
    #sql语句
    sql = 'insert into score(name,score,chinese) values(?,?,?) '
    #执行sql语句,插入多条数据使用的函数时executemany
    cur.executemany(sql,score_list)
    #关闭资源
    cur.close()
    conn.close()

这篇好文章是转载于:学新通技术网

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /boutique/detail/tanhfhfcgc
系列文章
更多 icon
同类精品
更多 icon
继续加载