【sqlite】python备份数据库
2024-08-24 15:10:13
备份整个数据库的方法:
# coding=utf-8
import sqlite3 def testBakSqlite():
conn = sqlite3.connect("sqlite_db_mine/testDB.db")
with open('testDB.sql.bak','w') as f:
for line in conn.iterdump():
data = line + '\n'
data = data.encode("utf-8")
f.write(data) testBakSqlite()
如果想要备份其中的一个表,没有很好的办法。下面是一些网上的讨论。
http://stackoverflow.com/questions/6677540/how-do-i-dump-a-single-sqlite3-table-in-python
You can copy only the single table in an in memory db:
import sqlite3 def getTableDump(db_file, table_to_dump):
conn = sqlite3.connect(':memory:')
cu = conn.cursor()
cu.execute("attach database '" + db_file + "' as attached_db")
cu.execute("select sql from attached_db.sqlite_master "
"where type='table' and name='" + table_to_dump + "'")
sql_create_table = cu.fetchone()[0]
cu.execute(sql_create_table);
cu.execute("insert into " + table_to_dump +
" select * from attached_db." + table_to_dump)
conn.commit()
cu.execute("detach database attached_db")
return "\n".join(conn.iterdump()) TABLE_TO_DUMP = 'table_to_dump'
DB_FILE = 'db_file' print getTableDump(DB_FILE, TABLE_TO_DUMP)
Pro: Simplicity and reliability: you don't have to re-write any library method, and you are more assured that the code is compatible with future versions of the sqlite3 module.
Con: You need to load the whole table in memory, which may or may not be a big deal depending on how big the table is, and how much memory is available.
最新文章
- scikit-learn随机森林调参小结
- Android面试题--事件处理
- poj 2559 Largest Rectangle in a Histogram - 单调栈
- Invalid initial heap size: -Xms
- 多线程 GET
- Unity不同平台生成中预处理的注意点
- Android keystore 密码忘记了的找回办法
- SQL中添加远程服务器连接
- Using 1.7 requires compiling with Android 4.4 (KitKat); currently using API 8
- RHCE 基础学习
- PROCEDURE_监测系统_原始数据报表数据生成存储过程—求每天平均值插入多表视图
- 使用brew安装软件
- NOI2001 食物链
- Hive动态分区
- Vue echarts
- Nginx 模块分类
- maven ${path.separator}
- 让.net core 支持静态文件
- file.getPath() getAbsolutePath() getCanonicalPath()区别
- C语言——第四次作业(2)
热门文章
- 【bzoj1036】[ZJOI2008]树的统计Count
- 素数的线性筛 &;&; 欧拉函数
- cf306 C. Divisibility by Eight(数学推导)
- Ubuntu 12 修改环境变量
- Mac Pro 编译安装 Nginx 1.8.1
- CMake Error: your CXX compiler: "" was not found
- [BZOJ1503][NOI2004]郁闷的出纳员
- 汉诺塔(河内塔)算法 ----C语言递归实现
- php源码安全加密之PHP混淆算法.
- 解决vista和win7在windows服务中交互桌面权限问题:穿透Session 0 隔离