Titanium Community Questions & Answer Archive

We felt that 6+ years of knowledge should not die so this is the Titanium Community Questions & Answer Archive

mobile database backup/recovery on device

Anyone have working code to backup the local database on a device (specifically iPhone, but portability would be nice). I've tried several methods, but I can't get any of the .dump or .backup commands from sqlite to work.

The end result is an application that gets updates OTA and can revert to a previous version of the data if something goes wrong.

Thanks!

Eric

— asked July 6th 2010 by Eric Lofland
  • backup
  • database
0 Comments

2 Answers

  • Accepted Answer

    Eric,

    Try closing the db & then use the Ti.Filesystem to create the backup, by reading the existing file and writing it out to the backup.

    You should then be able to open the DB again.

    — answered July 6th 2010 by David Ashwood
    permalink
    4 Comments
    • A couple of questions here, where does the file get copied, resourcesDirectory?

      I see methods for Ti.Filesystem.File read and write, but I have no idea how to implement these. Do you have or can you point me to any examples?

      Thanks!

      — commented July 6th 2010 by Eric Lofland
    • The KitchenSink is always a good place to start http://github.com/appcelerator/titanium_mobile/blob/master/demos/KitchenSink/Resources/examples/filesystem.js - look around line 41 for how to read/write a file.

      — commented July 6th 2010 by David Ashwood
    • var f = Titanium.Filesystem.getFile(Titanium.Filesystem.resourcesDirectory, 'app.db');
      var b = Titanium.FileSystem.createFile(Titanium.Filesystem.applicationDataDirectory, 'backup.db');
      var resources =f.read().text;
      b.write(resources);
      

      This fails with
      [ERROR] Script Error = Result of expression 'Titanium.FileSystem' [undefined] is not an object. at misc.js (line 63).

      — commented July 6th 2010 by Eric Lofland
    • sheesh, how many times will I mistype Filesystem (I put FileSystem)… This is the code I just tested that works:

      var f = Titanium.Filesystem.getFile(Titanium.Filesystem.resourcesDirectory, 'app.db');
      var backupDir = Titanium.Filesystem.getFile(Titanium.Filesystem.applicationDataDirectory);
      var b = Titanium.Filesystem.getFile(backupDir.nativePath, 'backup.db');
      b.write(f.read());
      

      I've been looking at a very old Kitchen Sink version and didn't have all this code. Time to update!

      Thank you so much for your help David!

      Eric

      — commented July 6th 2010 by Eric Lofland
  • db.execute('.backup dbBackup.db');
    
    Error Domain=com.plausiblelabs.pldatabase Code=3 UserInfo=0x560d6a0 
    "An error occured parsing the provided SQL statement." 
    (SQLite #1: near ".": syntax error) (query: '.backup dbBackup.db')
    
    — answered July 6th 2010 by Eric Lofland
    permalink
    2 Comments
    • Any success with this? I am looking to create backups and be able to restore them as well. Would love to see if anyone has gotten some working code before I fully dive in creating my own.

      Thanks

      — commented September 18th 2011 by Leonardo Amigoni
    • Hi Leonardo, did you figure this out?
      I would like to save a generated user db from Android to my webserver and restore if necessary.
      Thanks,

      — commented December 14th 2012 by Jeff Lukin
The ownership of individual contributions to this community generated content is retained by the authors of their contributions.
All trademarks remain the property of the respective owner.