关键词搜索

源码搜索 ×
×

Python学习之 SQLite 基本操作

发布2019-08-04浏览769次

详情内容

关于数据库的知识相关知识,可以点击参考数据库系统概论

一、基础知识

  1. connect 方法
    为了使用底层的数据库系统,首先需要连接它。这时需要在适当地环境下使用具名函数 connect 。
    connect 函数返回连接对象,这个对象标识目前与数据库的回话
    连接对象支持的方法:
方法名描述
close()关闭连接后,连接对象和它的游标均不可用
commit()如果支持的话,就提交挂起的事务,否则不做任何事
rollback()回滚挂起的事务
cursor()返回连接的游标对象

cursor 方法将我们引入另外一个主题:游标对象
2. 游标对象
通过游标执行 SQL 查询并检查结果,游标比连接支持更多的方法,而且可能在程序中更好用
游标对象方法:

名称描述
close()关闭游标后,游标不可用
execute()执行一个SQL操作,可能带有参数
executemany()对序列中的多个参数集执行SQL操作
fetchone()把查询的结果集中的下一行保存为序列,或者 None
fetchmany()查询结果集中的多行
fetchall()将所有(剩余)的行为作为序列的序列
  1. SQLite
    小型的数据库引擎 SQLite 不需要作为独立的服务器运行,也不基于集中式数据库存储机制,可以在本地文件上运行。
    同时它的一个优势在于它的一个包装(PySQLite)已经被包括在标准库内,可以直接使用

二、基本操作

  1. 新建数据库
import sqlite3
# 如果 test.db 不存在的话,会自动创建数据库 test.db
conn = sqlite3.connect("test.db")
# 获取连接的游标
c = conn.cursor()
# 执行SQL语句
sql = '''create table student
            (id int primary key ,score int,sex varchar(20),age int)'''
c.execute(sql)
# 进行提交,从而能将这些修改保存到文件中
conn.commit()
# 准备关闭数据库
conn.close()
    1. 插入数据
      一次插入多个数据时,存在问题
    import sqlite3
    # 如果 test.db 不存在的话,会自动创建数据库 test.db
    conn = sqlite3.connect("test.db")
    # 获取连接的游标
    c = conn.cursor()
    # 执行SQL语句
    students = [(1,80,'male',18),
                (2,70,'male',19),
                (3,60,'male',20),
                (4,50,'male',21)
                ]
    # 第一种:execute "INSERT"
    c.execute("insert into student(id,score,sex,age) values (0,90,'female',17)")
    # 第二种:execute multiple commands,一次性插入多个值有问题
    # c.execute("insert into student values (?,?,?,?)",students)
    # 第三种,如果属性列的次序和表中的次序一致,不需要在指定属性名
    c.execute("insert into student values (5,40,'female',22)")
    
    # 进行提交,从而能将这些修改保存到文件中
    conn.commit()
    # 准备关闭数据库
    conn.close()
    
      14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    1. 查询记录
    import sqlite3
    # 如果 test.db 不存在的话,会自动创建数据库 test.db
    conn = sqlite3.connect("test.db")
    # 获取连接的游标
    c = conn.cursor()
    # 第一种,获得一个记录
    sql = "select * from student order by score desc "
    c.execute(sql)
    print(c.fetchone())
    print(c.fetchone())
    
    # 第二种,获得一个包含所有记录的列表
    sql = "select * from student order by age"
    c.execute(sql)
    print(c.fetchall())
    # 按记录输出打印
    rs = c.execute(sql)
    for row in rs:
        print(row)
    
    # 进行提交,从而能将这些修改保存到文件中
    conn.commit()
    # 准备关闭数据库
    conn.close()
    
      14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    1. 修改
    import sqlite3
    # 如果 test.db 不存在的话,会自动创建数据库 test.db
    conn = sqlite3.connect("test.db")
    # 获取连接的游标
    c = conn.cursor()
    
    sql = "update student set score=100 where id = 5"
    c.execute(sql)
    
    sql = "select * from student where id =5"
    c.execute(sql)
    print(c.fetchone())
    # 进行提交,从而能将这些修改保存到文件中
    conn.commit()
    # 准备关闭数据库
    conn.close()
    
      14
    • 15
    • 16
    1. 删除
    import sqlite3
    # 如果 test.db 不存在的话,会自动创建数据库 test.db
    conn = sqlite3.connect("test.db")
    # 获取连接的游标
    c = conn.cursor()
    
    sql = "delete from student where id =2"
    c.execute(sql)
    
    # 进行提交,从而能将这些修改保存到文件中
    conn.commit()
    # 准备关闭数据库
    conn.close()
    
      1. 删除数据库表
      import sqlite3
      # 如果 test.db 不存在的话,会自动创建数据库 test.db
      conn = sqlite3.connect("test.db")
      # 获取连接的游标
      c = conn.cursor()
      
      c.execute("drop table student ")
      # 进行提交,从而能将这些修改保存到文件中
      conn.commit()
      # 准备关闭数据库
      conn.close()
      
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
      • 7
      • 8
      • 9
      • 10
      • 11

      相关技术文章

      点击QQ咨询
      开通会员
      返回顶部
      ×
      微信扫码支付
      微信扫码支付
      确定支付下载
      请使用微信描二维码支付
      ×

      提示信息

      ×

      选择支付方式

      • 微信支付
      • 支付宝付款
      确定支付下载