Page 1 of 1
How to save SQLite Database in memory to file?
Posted: Tue Jun 18, 2024 5:35 am
by jacdelad
Hi,
I found several examples on how to use sqlite in memory, using ":memory:" as database. But can I save the database to a file (and reload it into memory)? I mean other than creating an identical database structure and copy the data. I want to prevent to write every single update to disk and want to flush it, when the operations are done.
Re: How to save SQLite Database in memory to file?
Posted: Tue Jun 18, 2024 6:42 am
by Bisonte
Re: How to save SQLite Database in memory to file?
Posted: Tue Jun 18, 2024 11:45 am
by jacdelad
Ah thanks! I obviously used the wrong keywords for searching and didn't think copying it to *memory.
Re: How to save SQLite Database in memory to file?
Posted: Tue Jun 18, 2024 3:30 pm
by justjake
I had problems- in pre 6.11 I got a memory error with 6.11, the code just crashed. (with or without using p-utf8)
here's the version I ended up with.
I originally used utf8() but why do two function calls(utf8() & freememory()) to achieve the same thing with a datasection?
(portion of my sqlite3 library)
Code: Select all
DeclareModule sqlite3
Declare backup( srcNum, dstNum )
EndDeclareModule
Module sqlite3
EnableExplicit
UseSQLiteDatabase()
ImportC ""
sqlite3_backup_init(destID, *zDestName, sourceID, *zSourceName)
sqlite3_backup_step(backupHandle, nPage)
sqlite3_backup_finish(backupHandle)
EndImport
#backupALL = -1
#sqlite3_ok = 0
DataSection
asciiMain:
Data.a 'm', 'a', 'i', 'n', 0 ; "p-utf8" didn't work for me. Caused memory error, on 6.11 just a crash.
EndDataSection
Procedure backup(srcNum, dstNum)
Protected success, backupHandle
If IsDatabase(srcNum) And IsDatabase(dstNum)
backupHandle = sqlite3_backup_init( DatabaseID(dstNum), ?asciiMain, DatabaseID(srcNum), ?asciiMain )
If backupHandle
sqlite3_backup_step( backupHandle, #backupALL )
success = Bool( sqlite3_backup_finish(backupHandle) = #sqlite3_ok )
EndIf
EndIf
ProcedureReturn success
EndProcedure
EndModule
Re: How to save SQLite Database in memory to file?
Posted: Tue Jun 18, 2024 7:59 pm
by Captn. Jinguji
Anything wrong with simply using :
DatabaseUpdate( #DB_YTCS, "Vacuum DB_MEM into '.\gaga.s3db' " )
??
Re: How to save SQLite Database in memory to file?
Posted: Tue Jun 18, 2024 8:11 pm
by infratec