Shooting in the dark here. SQL queries are right . Database is setup as an id field, description field and longblob field. I imported a 50k jpeg intot he database with a utility and veified it was there.
Cannot retreive the pic because I am not usre of the code below. Anyway, have a play. Standard queries work with this code. And I have verified that I am getting data back. If I use the "length" command, I get a "Commands out of synch error, you can not use this command now".
Code: Select all
;============================================================================================================================
; Enable the image decoders
;============================================================================================================================
UseJPEGImageEncoder()
;============================================================================================================================
; Declare the library and SQL compression communication values
;============================================================================================================================
#libmysql = 1
#MySQL_CLIENT_COMPRESS = 32
;============================================================================================================================
; Declare data types used in sql return queries
;============================================================================================================================
dbHnd.l
SQL.s
row.s
i.l
j.l
affRows.l
fieldNum.l
rowsNum.l
;============================================================================================================================
; new list for SQL data return
;============================================================================================================================
NewList SqlData.s()
;============================================================================================================================
; All of my global variables
;============================================================================================================================
Global CFF_MySQL_Init.l, CFF_MySQL_ERRNO.l,CFF_MySQL_ERROR.l, CFF_MySQL_Real_Connect.l, CFF_MySQL_Real_Query.l
Global CFF_MySQL_Store_Result.l, CFF_MySQL_Field_Count.l, CFF_MySQL_Use_Result.l, CFF_MySQL_Fetch_Row.l
Global CFF_MySQL_Fetch_Lengths.l, CFF_MySQL_Free_Result.l, CFF_MySQL_Close.l, CFF_MySQL_Ping.l
;============================================================================================================================
; Declare the current directory path
;============================================================================================================================
CurrentDir.s = Space(512)
Result = GetCurrentDirectory_(Len(CurrentDir), @CurrentDir) ; Get the current directory
If Right(CurrentDir, 1) <> "\"
CurrentDir + "\"
EndIf
;============================================================================================================================
; Set the string maniplation buffer size (Must be called)
;============================================================================================================================
Procedure SetStringManipulationBufferSize(Bytes)
PBStringBase.l = 0
PBMemoryBase.l = 0
!MOV eax, dword [PB_StringBase]
!MOV [esp + 4],eax
!MOV eax, dword [PB_MemoryBase]
!MOV [esp + 8],eax
HeapReAlloc_(PBMemoryBase, #GMEM_ZEROINIT, PBStringBase, Bytes)
!MOV dword [_PB_StringBase], eax
EndProcedure
;a$ = "s" + Space(1048574) + "e" ; Now assign a string to be exactly 1048576 bytes (1 MB).
;Debug Str(Len(a$)) + " " + Left(a$, 1) + " " + Right(a$, 1) ; Prove it by showing Len and start/end chars of string.
;============================================================================================================================
; Initialise the MySQL library and functions
;============================================================================================================================
Procedure MySQL_Init()
Shared CurrentDir.s
If OpenLibrary(#libmysql, CurrentDir.s + "libmySQL.dll")
CFF_MySQL_Init = IsFunction(#libmysql, "mysql_init")
CFF_MySQL_Ping = IsFunction(#libmysql, "mysql_ping")
CFF_MySQL_ErrNo = IsFunction(#libmysql, "mysql_errno")
CFF_MYSQL_Error = IsFunction(#libmysql, "mysql_error")
CFF_MySQL_Real_Connect = IsFunction(#libmysql, "mysql_real_connect")
CFF_MySQL_Real_Query = IsFunction(#libmysql, "mysql_real_query")
CFF_MySQL_Store_Result = IsFunction(#libmysql, "mysql_store_result")
CFF_MySQL_Field_Count = IsFunction(#libmysql, "mysql_field_count")
CFF_MySQL_Use_Result = IsFunction(#libmysql, "mysql_use_result")
CFF_MySQL_Fetch_Row = IsFunction(#libmysql, "mysql_fetch_row")
CFF_MySQL_Fetch_Lengths = IsFunction(#libmysql, "mysql_fetch_lengths")
CFF_MySQL_Free_Result = IsFunction(#libmysql, "mysql_free_result")
CFF_MySQL_Close = IsFunction(#libmysql, "mysql_close")
ProcedureReturn CallFunctionFast (CFF_MySQL_Init, dbHnd)
EndIf
EndProcedure
;============================================================================================================================
; Ping the remote server
;============================================================================================================================
Procedure MySQL_Ping_Server(dbHnd)
ProcedureReturn CallFunctionFast(CFF_MySQL_Ping, dbHnd)
EndProcedure
;============================================================================================================================
; Get the query error message
;============================================================================================================================
Procedure.s MySQL_GetError(db_ID, Requester)
Protected Errormsg.s, i.l, Error.l
If CallFunctionFast(CFF_MySQL_ErrNo, db_ID) > 0
*Error = CallFunctionFast(CFF_MySQL_Error, db_ID)
Errormsg = PeekS(*Error)
If Requester ; A value of 1 means show the error
MessageRequester("SQL Error", Errormsg, #PB_MessageRequester_Ok)
EndIf
EndIf
ProcedureReturn Errormsg
EndProcedure
;============================================================================================================================
; Connect to the remote database
;============================================================================================================================
Procedure MySQL_Real_Connect(dbHnd, host.s, user.s, password.s, db.s, port.l, options.l)
ProcedureReturn CallFunctionFast(CFF_MySQL_Real_Connect, dbHnd, host, user, password.s, db, port, 0, options)
EndProcedure
;============================================================================================================================
; Generate the sql query
;============================================================================================================================
Procedure MySQL_Real_Query(dbHnd, Query.s)
ProcedureReturn CallFunctionFast(CFF_MySQL_Real_Query, dbHnd, Query, Len(Query))
EndProcedure
;============================================================================================================================
; Store the returned result for use
;============================================================================================================================
Procedure MySQL_Store_Result(dbHnd)
ProcedureReturn CallFunctionFast(CFF_MySQL_Store_Result, dbHnd)
EndProcedure
;============================================================================================================================
; Count the number of returned fields
;============================================================================================================================
Procedure MySQL_Field_Count(dbHnd)
ProcedureReturn CallFunctionFast(CFF_MySQL_Field_Count, dbHnd)
EndProcedure
;============================================================================================================================
; Use the returned results
;============================================================================================================================
Procedure MySQL_Use_Result(dbHnd)
ProcedureReturn CallFunctionFast(CFF_MySQL_Use_Result, dbHnd)
EndProcedure
;============================================================================================================================
; Fetch the affected row
;============================================================================================================================
Procedure MySQL_Fetch_Row(*mysqlResult)
ProcedureReturn CallFunctionFast(CFF_MySQL_Fetch_Row, *mysqlResult)
EndProcedure
;============================================================================================================================
; Fetch the lengths of the affected rows
;============================================================================================================================
Procedure MySQL_Fetch_Lengths(*mysqlResult)
ProcedureReturn CallFunctionFast (CFF_MySQL_Fetch_Lengths, *mysqlResult)
EndProcedure
;============================================================================================================================
; Free the results
;============================================================================================================================
Procedure MySQL_Free_Result(*mysqlResult)
ProcedureReturn CallFunctionFast(CFF_MySQL_Free_Result, *mysqlResult)
EndProcedure
;============================================================================================================================
; Close the database connection
;============================================================================================================================
Procedure MySQL_Close(dbHnd)
CallFunctionFast(CFF_MySQL_Close, dbHnd)
EndProcedure
;============================================================================================================================
; Re-use this section to get the sql data into a structure
;============================================================================================================================
Procedure GetSqlData(Result) ; If the query worked, get the data
Shared dbHnd
ClearList(SqlData.s()) ; Always remember to clear the list at program start
If Result ; If the query didn't work, give an error
MySQL_GetError(dbHnd, 1)
Else
*mysqlResult = MySQL_Use_Result(dbHnd) ; If data returned, use the result
If *mysqlResult = 0
If MySQL_Field_Count(dbHnd) ; No fields are returned so that's an error
MySQL_GetError(dbHnd, 1)
Else
MySQL_GetError(dbHnd, 1) ; Fields are returned, so no error but query didn't return data
EndIf
Else
affRows = CallFunction(#libmysql,"mysql_affected_rows",dbHnd) ; How many rows affected
fieldNum = CallFunction(#libmysql,"mysql_num_fields", *mysqlResult) ; How many fields
rowsNum = CallFunction(#libmysql,"mysql_num_rows", *mysqlResult) ; How many rows
Repeat ; Sequentially process all returned data
*mysqlRow = MySQL_Fetch_Row(*mysqlResult)
If *mysqlRow <> 0
*mysqlLen = MySQL_Fetch_Lengths(*mysqlResult)
row.s = ""
For j = 1 To fieldNum ; Length of given field
length = PeekL(*mysqlLen + 4 * (j - 1))
fieldptr = PeekL(*mysqlRow + 4 * (j - 1))
If fieldptr > 0
content.s = PeekS(fieldptr, length)
Else
content = "NULL" ; Zero pointer returned means empty field
EndIf
row.s = row.s + content + ";" ; Content (individual collumns)
Next j
AddElement(SqlData.s()) ; Add the returned data to our linked list
SqlData.s() = row.s
EndIf
Until *mysqlRow = 0
Result.l = MySQL_Free_Result(*mysqlResult)
EndIf
EndIf
EndProcedure
;============================================================================================================================
; Buffer helper
;============================================================================================================================
Procedure.l hex2dec(h$)
h$ = UCase(h$)
For r = 1 To Len(h$)
d <<4 : a$ = Mid(h$, r, 1)
If Asc(a$) > 60
d + Asc(a$) - 55
Else
d + Asc(a$) - 48
EndIf
Next
ProcedureReturn d
EndProcedure
;============================================================================================================================
; Try to initialise the database environment (Procedure MySQL_Init())
;============================================================================================================================
dbHnd = MySQL_Init()
If dbHnd
LibOpened = 1
Else
LibOpened = 0
Message = MessageRequester("Critical Error", "libmysql.dll not found in programs startup directory")
End
EndIf
;============================================================================================================================
; Start of the program
;============================================================================================================================
SetStringManipulationBufferSize(1048576) ; Set the buffer size for all strings to 1 MB.
If MySQL_Real_Connect(dbHnd, "localhost", "root", "", "test", 3306, #MySQL_CLIENT_COMPRESS) <> 0
; Result = MySQL_Real_Query(dbHnd, "SELECT LENGTH(image) FROM forkle WHERE id=0;")
; If Result ; Test the return code for the query
; MySQL_GetError(dbHnd, 1) ; Get the error code
; EndIf
Result = MySQL_Real_Query(dbHnd, "SELECT image FROM forkle WHERE id=0;")
If Result ; Test the return code for the query
MySQL_GetError(dbHnd, 1) ; Get the error code
Else ; Otherwise do the following
GetSqlData(Result) ; Get the data from the seerver
FirstElement(SqlData.s()) ; There should be only one return but...
;----------------------------------------------------------------------------------------------
BufferData.s = StringField(SqlData.s(), 1, ";")
BufferSize = Len(BufferData.s) / 2:Debug Str(BufferSize)
*Buffer = AllocateMemory(BufferSize)
j = -1
For i = 1 To Len(BufferData.s) + 2 Step 2
j = j + 1
Number = Hex2Dec(Mid(BufferData.s, i, 2))
PokeB(*Buffer + j, Number)
Next i
Result = CatchImage(0, *Buffer)
Result = SaveImage(0, "D:\Foom.jpg", #PB_ImagePlugin_JPEG)
;----------------------------------------------------------------------------------------------
EndIf
EndIf
End