How to save SQLite Database in memory to file?

Just starting out? Need help? Post your questions and find answers here.
User avatar
jacdelad
Addict
Addict
Posts: 2003
Joined: Wed Feb 03, 2021 12:46 pm
Location: Riesa

How to save SQLite Database in memory to file?

Post 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.
Good morning, that's a nice tnetennba!

PureBasic 6.21/Windows 11 x64/Ryzen 7900X/32GB RAM/3TB SSD
Synology DS1821+/DX517, 130.9TB+50.8TB+2TB SSD
User avatar
Bisonte
Addict
Addict
Posts: 1313
Joined: Tue Oct 09, 2007 2:15 am

Re: How to save SQLite Database in memory to file?

Post by Bisonte »

PureBasic 6.21 (Windows x64) | Windows 11 Pro | AsRock B850 Steel Legend Wifi | R7 9800x3D | 64GB RAM | RTX 5080 | ThermaltakeView 270 TG ARGB | build by vannicom​​
English is not my native language... (I often use DeepL.)
User avatar
jacdelad
Addict
Addict
Posts: 2003
Joined: Wed Feb 03, 2021 12:46 pm
Location: Riesa

Re: How to save SQLite Database in memory to file?

Post by jacdelad »

Ah thanks! I obviously used the wrong keywords for searching and didn't think copying it to *memory.
Good morning, that's a nice tnetennba!

PureBasic 6.21/Windows 11 x64/Ryzen 7900X/32GB RAM/3TB SSD
Synology DS1821+/DX517, 130.9TB+50.8TB+2TB SSD
justjake
New User
New User
Posts: 1
Joined: Tue Jun 18, 2024 3:24 pm

Re: How to save SQLite Database in memory to file?

Post 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
Captn. Jinguji
User
User
Posts: 94
Joined: Sun Oct 24, 2004 9:25 am

Re: How to save SQLite Database in memory to file?

Post by Captn. Jinguji »

Anything wrong with simply using :

DatabaseUpdate( #DB_YTCS, "Vacuum DB_MEM into '.\gaga.s3db' " )

??
Is this an artifact or should it be disposed of ?
infratec
Always Here
Always Here
Posts: 7604
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: How to save SQLite Database in memory to file?

Post by infratec »

Post Reply