Reading SQLITE records into a RTF control

Just starting out? Need help? Post your questions and find answers here.
XCoder
User
User
Posts: 72
Joined: Tue Dec 31, 2013 9:18 pm

Reading SQLITE records into a RTF control

Post by XCoder »

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:

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
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?
Axolotl
Addict
Addict
Posts: 802
Joined: Wed Dec 31, 2008 3:36 pm

Re: Reading SQLITE records into a RTF control

Post by Axolotl »

Hey XCoder,
a first quick attempt to help:
Check your callback implementation, because MSDN wrote: The callback function returns zero to indicate success.
Just because it worked doesn't mean it works.
PureBasic 6.04 (x86) and <latest stable version and current alpha/beta> (x64) on Windows 11 Home. Now started with Linux (VM: Ubuntu 22.04).
XCoder
User
User
Posts: 72
Joined: Tue Dec 31, 2013 9:18 pm

Re: Reading SQLITE records into a RTF control

Post by XCoder »

Thanks for your suggestion Axolotl.

Following your suggestion I have tried the four possible combinations of 1's and 0's for my return values but these have had no effect.

I looked at "How to use streams" at https://learn.microsoft.com/en-us/windo ... se-streams and realised that one of the return values should have been -1.

However, my program will still not work when I insert -1.
Post Reply