﻿
#include 'SQLite_Backup.au3'

; min. required: version 3.7.4
_SQLite_Startup(@ScriptDir & "\sqlite3.dll", False, 1)

; Modify the pathes for your SQLite database files!
; "germandict.db" - Erstellung s. hier: https://autoit.de/thread/85897-sqlite-m%C3%B6glichkeit-abfrage-beschleunigen/?postID=704374#post704374
Global $g_sDBFile = @ScriptDir & "\germandict.db"
Global $g_sDBFileCopy = @ScriptDir & "\germandict_copy_running.db" ; Target file: _Example_Backup_RunningDB()

; Function: _SQLite_Backup_LoadOrSaveDB
; ByRef parameter, must be declared (no preassignment required for: File -> Memory)
Global $g_hDBMem

ConsoleWrite('LOAD FILE TO MEMORY' & @CRLF)
_Example_LoadFileToMemory()

ConsoleWrite('SAVE MEMORY TO FILE' & @CRLF)
_Example_SaveMemoryToFile()
_SQLite_Close($g_hDBMem)

ConsoleWrite('BACKUP RUNNING DB' & @CRLF)
_Example_Backup_RunningDB()


_SQLite_Shutdown()




Func _Example_LoadFileToMemory()

	Local $Timer = TimerInit()

	; Since $g_hDBMem is not a database pointer, a memory database is automatically created.
	; This also results in the copy direction: file to memory.
	Local $iResult = _SQLite_Backup_LoadOrSaveDB($g_sDBFile, $g_hDBMem)
#cs
	If you do not use the standard scheme names (To: 'main', From: 'main'), you can pass them as optional parameters.
	Local $iResult = _SQLite_Backup_LoadOrSaveDB($g_sDBFile, $g_hDBMem, 'temp MYCOPY')
#ce
	If $iResult = $SQLITE_OK Then
		ConsoleWrite('COPY-TIME File -> Memory: ' & StringFormat('%.1f s', TimerDiff($Timer)/1000) & ' ' & _SQLite_ErrCodeLiteral($iResult) & @CRLF & @CRLF)
	Else
		ConsoleWrite('ERROR: ' & _SQLite_ErrCodeLiteral($iResult) & @CRLF & @CRLF)
	EndIf

EndFunc  ;==>_Example_LoadFileToMemory


Func _Example_SaveMemoryToFile()

	Local $Timer = TimerInit()

	; $g_hDBMem is now a database pointer (created and filled by _Example_LoadFileToMemory() )
	; This also results in the copy direction: memory to file.
	Local $iResult = _SQLite_Backup_LoadOrSaveDB($g_sDBFile, $g_hDBMem)
#cs
	If you do not use the standard scheme names (To: 'main', From: 'main'), you can pass them as optional parameters.
	Local $iResult = _SQLite_Backup_LoadOrSaveDB($g_sDBFile, $g_hDBMem, 'temp MYCOPY')
#ce
	If $iResult = $SQLITE_OK Then
		ConsoleWrite('COPY-TIME Memory -> File: ' & StringFormat('%.1f s', TimerDiff($Timer)/1000) & ' ' & _SQLite_ErrCodeLiteral($iResult) & @CRLF & @CRLF)
	Else
		ConsoleWrite('ERROR: ' & _SQLite_ErrCodeLiteral($iResult) & @CRLF & @CRLF)
	EndIf

EndFunc  ;==>_Example_SaveMemoryToFile


Func _Example_Backup_RunningDB()

	Local $hDB = _SQLite_Open($g_sDBFile)

	; Parameter: Handle running DB, Path backup file, [optional] progress function, [optional] string scheme names, [optional] max. number progress steps
	_SQLite_Backup_RunningDB($hDB, $g_sDBFileCopy, _progress, '', 100)

	Local $Timer = TimerInit(), $iStatus
	While True
		Sleep(20)
		$iStatus = _SQLite_Backup_RunningDB_Status()
		ConsoleWrite('TIMER: ' & StringFormat('%.2f', TimerDiff($Timer)/1000)  & ' BACKUP-STATUS: ' & ($iStatus ? '$SQLITE_BACKUP_RUNNING' : '$SQLITE_BACKUP_NOTRUNNING') & @CRLF)
		If Not $iStatus Then
			ConsoleWrite('FINISHED, LAST_ERROR: ' & _SQLite_ErrCodeLiteral(_SQLite_Backup_RunningDB_LastError()) & @CRLF)
			ExitLoop
		EndIf
	WEnd

	_SQLite_Close($hDB)

EndFunc  ;==>_Example_Backup_RunningDB


Func _progress($r, $p) ; $r=remaining-pages, $p=page-count
	ConsoleWrite('Completion: ' & StringFormat('%.1f %%', (100 * ($p - $r) / $p))  & @CRLF)
EndFunc  ;==>_progress
