赞
踩
SQLite是内嵌在Python中的轻量级、基于磁盘文件袋额数据库管理系统,不需要安装和配置服务,支持使用SQL语句来访问数据库。该数据库使用C语言开发,支持大多数SQL91标准,支持原子的、一致的、独立的和持久的事务,不支持外键限制;通过数据库级的独占性和共享性锁定来实现独立事务,当多个线程同时访问同一个数据库并试图写入数据时,每一时刻只有一个线程可以写入数据。
SQLite支持最大140TB大小的单个数据库,每个数据库完全存储在单个磁盘文件中,以B+树数据结构的形式存储,一个数据库就是一个文件,通过直接复制数据库文件就可以实现数据库的备份。如果需要使用可视化管理工具,可以下载并使用SQLiteManager、SQLite Database Browser 或其他类似工具。
我们还可以来简单了解一下sqlite3模块的API
访问和操作SQLite数据时,首先导入sqlite3模块,然后创建一个与数据库关联的Connection对象,例如:
# -*- coding:utf-8 -*- import sqlite3 #导入模块 ''' 连接数据库 connect()方法,可以判断一个数据库文件是否存在,如果不存在就自动创建一个, 如果存在的话,就打开那个数据库。 ''' database=r'C:\Users\WYXCz\Desktop\crawl_data.db' conn = sqlite3.connect(database) ''' 再创建一个Cusor对象,并且调用Cursor对象的execute()方法来执行SQL语句 创建数据表以及查询、插入、修改或删除数据库中的数据 ''' c = conn.cursor() #创建表 # c.execute('''CREATE TABLE stocks(date text,trans text,symbol text,gty real,price real)''') #向表中插入一条数据 # 提交事务 SELECT语句不需要此操作,默认的execute方法的,commit_at_once设为True会隐式调用此方法 c.execute('''insert into stocks(date,trans,symbol,gty,price) values('2016-02-05','BUY','RHAT',100,35.14)''') # insert 方法2 sql1='''insert into stocks(date,trans,symbol,gty,price) values(?,?,?,?)''' params=('2016-02-05','BUY','RHAT',100,35.14) c.execute(sql1,params) #提交当前事务,保存数据 conn.commit() #关闭数据库连接 conn.close() #-----------------查询刚才插入的数据 方法1------------------------ #由于刚才已经关闭了数据库连接,需要重新创建Connection对象和Cursor对象 conn = sqlite3.connect(database) c = conn.execute('''select * from stocks''') print(c) #<sqlite3.Cursor object at 0x00000000007E25E0> print(list(c)) #[('2016-01-05', 'BUY', 'RHAT', 100.0, 35.14)] #-----------------查询刚才插入的数据 方法2------------------------ conn = sqlite3.connect(database) c = conn.cursor() a = c.execute('select * from stocks') print(c) #<sqlite3.Cursor object at 0x00000000007E25E0> # print('fetchone:',c.fetchone()) # print('fetchall',c.fetchall()) for i in a: print(i) #('2016-01-05', 'BUY', 'RHAT', 100.0, 35.14)
import sqlite3
from DBUtils.PersistentDB import PersistentDB
db_path = r'D:/works/python1/test10/resources/stock0.db'
dbpool = PersistentDB(sqlite3, maxusage=2, database=db_path)
db = dbpool.connection()
print (db is not None)
db.close()
pip install sqlite-utils
import sqlite_utils
db = sqlite_utils.Database("demo_database.db")
# This line creates a "dogs" table if one does not already exist:
db["dogs"].insert_all([
{"id": 1, "age": 4, "name": "Cleo"},
{"id": 2, "age": 2, "name": "Pancakes"}
], pk="id")
现在,您可以使用CLI实用程序执行以下操作:
$ sqlite-utils tables dogs.db --counts [{"table": "dogs", "count": 2}] $ sqlite-utils dogs.db "select * from dogs" [{"id": 1, "age": 4, "name": "Cleo"}, {"id": 2, "age": 2, "name": "Pancakes"}] $ sqlite-utils dogs.db "select * from dogs" --csv id,age,name 1,4,Cleo 2,2,Pancakes $ sqlite-utils dogs.db "select * from dogs" --table id age name ---- ----- -------- 1 4 Cleo 2 2 Pancakes
1、set时,要将table2的num2的值赋给table1的num1字段,要select一下table2,并在括号关联起来
update table1
set num1 = (select num2 from table2 where table2.pid=table1.id)
where...
更新多个字段时:
update table1
set num1 = (select num2 from table2 where table2.pid=table1.id),
num11 = (select num22 from table2 where table2.pid=table1.id)
where...
2、where时,也一样,比如我就将上面的改一下
update table1
set num = 99
where table1.id=(select pid from table2 where table2.pid=table1.id)
别人说太慢了,找了一个折中的办法:
1.把连接写成视图
2.导出结果到csv
3.建立一个空表,结构和视图相同
4.把csv导入到空表
5.修整相关的列
pip3 install datasette
datasette serve path/to/database.db
这将在端口8001上启动Web服务器,访问http://localhost:8001/
以访问Web界面。
datasette serve fivethirtyeight.db -m metadata.json
datasette github:https://github.com/simonw/datasette
sqlite-utils github:https://github.com/simonw/sqlite-utils
sqlite-utils pypi:https://pypi.org/project/sqlite-utils/
参考1:https://www.cnblogs.com/avention/p/8955130.html
https://www.runoob.com/sqlite/sqlite-python.html
https://segmentfault.com/a/1190000019212422
https://www.cnblogs.com/madsnotes/articles/5702238.html
https://blog.csdn.net/weixin_34296641/article/details/92502792
参考2:https://www.cnblogs.com/Donnnnnn/p/6070659.html
https://cloud.tencent.com/developer/article/1026866
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。