备份整个数据库的方法:

# 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.

最新文章

  1. scikit-learn随机森林调参小结
  2. Android面试题--事件处理
  3. poj 2559 Largest Rectangle in a Histogram - 单调栈
  4. Invalid initial heap size: -Xms
  5. 多线程 GET
  6. Unity不同平台生成中预处理的注意点
  7. Android keystore 密码忘记了的找回办法
  8. SQL中添加远程服务器连接
  9. Using 1.7 requires compiling with Android 4.4 (KitKat); currently using API 8
  10. RHCE 基础学习
  11. PROCEDURE_监测系统_原始数据报表数据生成存储过程—求每天平均值插入多表视图
  12. 使用brew安装软件
  13. NOI2001 食物链
  14. Hive动态分区
  15. Vue echarts
  16. Nginx 模块分类
  17. maven ${path.separator}
  18. 让.net core 支持静态文件
  19. file.getPath() getAbsolutePath() getCanonicalPath()区别
  20. C语言——第四次作业(2)

热门文章

  1. 【bzoj1036】[ZJOI2008]树的统计Count
  2. 素数的线性筛 && 欧拉函数
  3. cf306 C. Divisibility by Eight(数学推导)
  4. Ubuntu 12 修改环境变量
  5. Mac Pro 编译安装 Nginx 1.8.1
  6. CMake Error: your CXX compiler: "" was not found
  7. [BZOJ1503][NOI2004]郁闷的出纳员
  8. 汉诺塔(河内塔)算法 ----C语言递归实现
  9. php源码安全加密之PHP混淆算法.
  10. 解决vista和win7在windows服务中交互桌面权限问题:穿透Session 0 隔离