Reading SQLITE records into a RTF control
Posted: Sat Jul 29, 2023 11:03 am
I am trying to read a record from a sqlite database into a rich text editor control.
Here is the relevant part of the code I am using:
The Memory Viewer shows that the contents of the record has been read into the buffer pointed to by pbBuff correctly but the contents of the record is not showing in the rich text editor.
How should I change the code to make this work?
Here is the relevant part of the code I am using:
Code: Select all
Procedure DBStreamOpenFileCallback(hFile, pbBuff, cb, *pcb)
Protected *pbRecordData = #Null, dwRecordSize.i=0, DataBaseContents$=""
Protected dwRead = 0; //dwRecordSize - size (bytes) of the record to be read
; avoid loading the resource more than once
If (dwRead = 0) ; If bytes read = 0 load allocate memory for the text and then read the text
DatabaseQuery(#Database, "SELECT Title FROM tblDVD WHERE Disk_Number = 1") ; Get the first record in the 'tblDVD' table
; If DatabaseQuery(#Database, "SELECT Disk_Number, Title FROM tblDVD ORDER BY Disk_Number")
NextDatabaseRow(#Database)
*pbRecordData = AllocateMemory(DatabaseColumnSize(#Database, 0))
dwRecordSize = DatabaseColumnSize(#Database, 0) ;Get size of Data in column 0 ie Title
DataBaseContents$ = GetDatabaseString(#Database, 0)
PokeS(*pbRecordData, DataBaseContents$)
;CopyMemoryString(@DataBaseContents$, @*pbRecordData) ;Does Not work!!!
FinishDatabaseQuery(#Database)
EndIf
If *pbRecordData <>#Null ;//If the pointer to the record data is set
Debug "[" + #PB_Compiler_Filename + " "+ #PB_Compiler_Line +"] Pointer to resource data is valid"
ZeroMemory_(pbBuff, cb) ;//Fill pbBuff with cb bytes of zero
If ((dwRecordSize - dwRead) > cb)
Debug "[" + #PB_Compiler_Filename + " "+ #PB_Compiler_Line +"] Reading partial data"
CopyMemory_(pbBuff, *pbRecordData + dwRead, cb) ;Copy cb bytes of data from the memory (pbResData + dwRead) To pbBuff
*pcb = cb ;Get number of bytes of Data Read
dwRead = dwRead + *pcb ;Add number of bytes read from *pcb to dwRead
Else ;Read last block of data
CopyMemory_(pbBuff, *pbRecordData + dwRead, dwRecordSize - dwRead) ;Copy (dwRecordSize - dwRead) bytes of data from (pbResData + dwRead) to pbBuff
ShowMemoryViewer(pbBuff, dwRecordSize - dwRead)
*pcb = dwRecordSize - dwRead ;*pcb points to number of bytes transferred
FreeMemory(*pbRecordData)
*pbRecordData = #Null ;Reset pointer To the resouce to NULL
dwRecordSize = 0 ;Reset size (bytes) of the resource to zero
dwRead = 0 ;Reset data to read to 0 (no more bytes to read?)
EndIf
ProcedureReturn 1
Else
MessageRequester("Info", "Pointer is NULL")
EndIf
ProcedureReturn 0
EndProcedure
Procedure DBEditor_LoadRecord(hRichEd)
; hRichEd - Windows ID for the RTF Editor
Protected StreamData.EDITSTREAM, ext$, FileType
Protected Success=#False
StreamData\dwCookie = 0 ;Not used so set to 0
StreamData\dwError = #Null ;No errors (yet!)
StreamData\pfnCallback = @DBStreamOpenFileCallback() ;Pointer to callback procedure
SendMessage_(hRichEd, #EM_STREAMIN, #SF_RTF, @StreamData) ;Call CALLBACK PROCEDURE - use #SF_RTF, #SF_TEXT Or #SF_UNICODE if required
EndProcedure
How should I change the code to make this work?