database 如何备份sqlite数据库?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25675314/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 07:47:51  来源:igfitidea点击:

How to backup sqlite database?

databasesqlitebackup

提问by thelolcat

What's the proper way to do it? Do I just copy the .sq3 file?

正确的做法是什么?我只是复制 .sq3 文件吗?

What if there are users on the site and file is being written while it's being copied?

如果站点上有用户并且文件在复制时正在写入怎么办?

回答by Googie

The sqlite3 command line tool features the .backupdot command.

sqlite3 命令行工具具有.backupdot 命令

You can connect to your database with:

您可以通过以下方式连接到您的数据库:

sqlite3 my_database.sq3

and run the backup dot command with:

并使用以下命令运行备份点命令:

.backup backup_file.sq3

Instead of the interactive connection to the database, you can also do the backup and close the connection afterwards with

除了与数据库的交互式连接之外,您还可以进行备份并在之后关闭连接

sqlite3 my_database.sq3 ".backup 'backup_file.sq3'"

Either way the result is a copy named backup_file.sq3of the database my_database.sq3.

无论哪种方式,结果都是一个名为backup_file.sq3database的副本my_database.sq3

It's different from regularly file copying, because it takes care of any users currently working on the database. There are proper locks set on the database, so the backup is done exclusively.

它与定期文件复制不同,因为它负责处理当前在数据库上工作的所有用户。在数据库上设置了适当的锁,因此备份是独占的。

回答by Lava Sangeetham

.backup is the best way.

.backup 是最好的方法。

sqlite3 my_database .backup my_database.back

you can also try .dump command , it gives you the ability to dump the entire database or tables into a text file. If TABLE specified, only dump tables matching LIKE pattern TABLE.

您也可以尝试 .dump 命令,它使您能够将整个数据库或表转储到文本文件中。如果指定了 TABLE,则仅转储与 LIKE 模式 TABLE 匹配的表。

sqlite3 my_database .dump > my_database.back

A good way to make an archival copy using dump and store, Reconstruct the database at a later time.

使用转储和存储制作存档副本的好方法,稍后重建数据库。

sqlite3 my_database .dump | gzip -c > my_database.dump.gz
zcat my_database.dump.gz | sqlite3 my_database

Also check this question Do the SQLite3 .backup and .dump commands lock the database?

还要检查这个问题SQLite3 .backup 和 .dump 命令会锁定数据库吗?

回答by Lava Sangeetham

try {
    final String inFileName = "/data/data/your app package/databases/db";
    File dbFile = new File(inFileName);
    FileInputStream fis = new FileInputStream(dbFile);
    String path = Environment.getExternalStorageDirectory().getAbsolutePath() + "/CALC/Backup";
    File dir = new File(path);
    if (!dir.exists()) dir.mkdirs();
    String outFileName = path + "/filename"; // output file name
    // Open the empty db as the output stream
    OutputStream output = new FileOutputStream(outFileName);

    // Transfer bytes from the inputfile to the outputfile
    byte[] buffer = new byte[1024];
    int length;
    while ((length = fis.read(buffer)) > 0) {
        output.write(buffer, 0, length);
    }
    Toast.makeText(getActivity(), "Backup Successfully", 2).show();
    // Close the streams
    output.flush();
    output.close();
    fis.close();
} 
catch (Exception e) {
    e.printStackTrace();
}