Python 基础知识(十五)SQLite

Python 基础知识(十五)SQLite

SQLite简单介绍

适用于:小型数据库
动态类型:①NULL 空字段 ②INTEGER 整形
③REAL 实数
④TEXT 字符型
⑤BLOB 二进制

语句:(1)select * from 列表
(2)插入 INSERT INTO 列表()Values 值
Python 基础知识(十五)SQLite
(3)更新 UPDATE
Python 基础知识(十五)SQLite
(4)删除 DELET
Python 基础知识(十五)SQLite

Python连接SQLite

Python 基础知识(十五)SQLite
Python 基础知识(十五)SQLite
连接成功后可以在Pycharm里使用SQL语句
Python 基础知识(十五)SQLite

SQLite3

导入sqlite3模块

方法:

conn = sqlit3.connect('数据库’) 连接数据库

conn.cursor() 创建游标

conn.commit() 提交操作

conn.close() 关闭数据库连接

游标.execute(sql语句[,参数]) 执行操作

游标.fetchall() 获取所有结果到列表

游标.fetchone() 返回一条结果

游标.fetchmany(数量) 返回指定数量的条目

参数化查询可以避免SQL注入,参数化查询的两种方法

查询操作
Python 基础知识(十五)SQLite
增加操作
Python 基础知识(十五)SQLite
Python 基础知识(十五)SQLite
更新操作
Python 基础知识(十五)SQLite
删除操作
Python 基础知识(十五)SQLite
Python 基础知识(十五)SQLite
游标.fetchall() 提取所有
Python 基础知识(十五)SQLite
游标.fetchone() 只返回第一条
Python 基础知识(十五)SQLite
游标.fetchmany()
Python 基础知识(十五)SQLite