0

What I want is the ruby way to perform the following actions:

# dump database in SQL text format
echo .dump | sqlite3 mydb.sqlite3 > mydump.sql

# execute sql queries found on file mydump.sql to rebuild database
sqlite> .read mydump.sql

I want to perform the above actions through the SQLite3 API assuming that only the sqlite3-ruby gem and lib is available and not the command line util.

I read through the documentation here: http://www.rubydoc.info/github/luislavena/sqlite3-ruby/SQLite3/Database but I can't find the proper way to do it. Has anyone a clue on how to do it?

Thanasis Petsas
  • 4,378
  • 5
  • 31
  • 57
  • Let me see if I have understood: What you want to do is to get a backup from your current sqlite from a rails application, and then latter be able to restore, reuse or rebuild the database from that file? is that it? – Luan Gonçalves Barbosa May 23 '17 at 20:37
  • Actually, I want to replicate those exact steps described here: https://techblog.dorogin.com/sqliteexception-database-disk-image-is-malformed-77e59d547c50 to overcome the "database disk image is malformed" error. I am able to do it using the command line util (sqlite3) using the `.dump` and `.read`, but I wan to be able to do those steps entirely in ruby without relying on the sqlite3 command line tool, i.e., without calling sqlite3 tool through system, etc. – Thanasis Petsas May 23 '17 at 21:01
  • 2
    The "dot" commands are part of the sqlite3 shell utility, not of SQLite3 (or the sqlite3 gem) itself, so if you really want to do that using ruby, then you have to implement what they do using ruby. (But why not just use the shell?) – varro May 23 '17 at 21:15
  • Thanks @varro for the details. I didn't know about that. I want this functionality to be part of the program itself and not to rely on any other software that happens to be installed on the host machine. I will have a look to the source code of these commands if available and try to embed in my program. – Thanasis Petsas May 24 '17 at 14:46

0 Answers0