• Welcome to SQLitening Support Forum.
 

News:

Welcome to the SQLitening support forums!

Main Menu

Saving Images Remotely

Started by Fredrick Ughimi, August 12, 2017, 12:07:14 PM

Previous topic - Next topic

cj

> I am running the application on a remote mode.

slSetProcessMods "L0" is local mode

Ln = Load either the local or remote processing lib and ruts. This along with push/pop database allows you to access both a local and remote database in the same running program. WARNING: The misuse of this may cause a GPF.

Fredrick Ughimi

Without the local mode placed before the SQL Select statement, I get error -8 Access Denied on the slPutFile Statement:


slPutFile sTempImageFile, slFN("Picture"), "C"
Fredrick O. Ughimi<br /><br />fughimi@gmail.com<br />- Freedom lies in being bold -- Robert Frost, Poet

cj

#17
Example getting local files without using slSetProcessMods "L0" or slGetFile to stay in remote mode and sent files from local machine.
http://sqlitening.com/support/index.php?topic=9675.0

You are always in local mode so server is never accessed.

I will add a local put to code in general forum so slSetProcessMods "L0"  and slSetProcessmods "L1" is never needed.

Fredrick Ughimi

Quote
You are always in local mode so server is never accessed.

Switching to remote mode or commenting all local mode statements, I get error -8 Access Denied on the slPutFile Statement:


slPutFile sTempImageFile, slFN("Picture"), "C"


Fredrick O. Ughimi<br /><br />fughimi@gmail.com<br />- Freedom lies in being bold -- Robert Frost, Poet

cj

slPutFile doesn't work on server unless in FACT, but you are using ddoc locally so should be in local mode when doing slPutfile.

Fredrick Ughimi

Quote
slPutFile doesn't work on server unless in FACT, but you are using ddoc locally so should be in local mode when doing slPutfile.

You are right. But I am getting I getting error -8 Access Denied on the slPutFile Statement in local mode.

Let me check my SQLitening files.

Fredrick O. Ughimi<br /><br />fughimi@gmail.com<br />- Freedom lies in being bold -- Robert Frost, Poet

cj

Fredrick,

Please try running this code because it should help.
It allows staying in remote mode (no more slSetProcessMods L0 or L1)
Appreciate knowing you tested it.

http://sqlitening.com/support/index.php?topic=9676.0

4 Helper functions:
FUNCTION PutBlob(sBlobKey AS STRING,sBlob AS STRING) THREADSAFE AS LONG
FUNCTION GetBlob(sBlobKey AS STRING,sBlobData AS STRING) AS LONG
FUNCTION GetLocalFile(sFileName AS STRING, sData AS STRING) THREADSAFE AS LONG
FUNCTION PutLocalFile(sFileName AS STRING, sData AS STRING) THREADSAFE AS LONG



Fredrick Ughimi

Quote
Fredrick,

Please try running this code because it should help.
It allows staying in remote mode (no more slSetProcessMods L0 or L1)
Appreciate knowing you tested it.

http://sqlitening.com/support/index.php?topic=9676.0

4 Helper functions:
FUNCTION PutBlob(sBlobKey AS STRING,sBlob AS STRING) THREADSAFE AS LONG
FUNCTION GetBlob(sBlobKey AS STRING,sBlobData AS STRING) AS LONG
FUNCTION GetLocalFile(sFileName AS STRING, sData AS STRING) THREADSAFE AS LONG
FUNCTION PutLocalFile(sFileName AS STRING, sData AS STRING) THREADSAFE AS LONG

Sure. I would get right on it.

Thank you, CJ.
Fredrick O. Ughimi<br /><br />fughimi@gmail.com<br />- Freedom lies in being bold -- Robert Frost, Poet

cj

#INCLUDE "sqlitening.inc"
$Server    = "192.168.0.2"
$DataBase  = "dummy.db3"
%ShowSteps = 0
%DropTable = 0
%Images = 9

FUNCTION PBMAIN() AS LONG
DIM sBlobArray(1 TO %Images) AS STRING
CreateTestFiles %Images       'dummy jpg files
ConnectRemote                 'connect remote database
UpdatePictureTable %Images    'update  remote images

GetImages %Images,sBlobArray()'get remote images
slDisconnect                  'disconnect
? JOIN$(sBlobArray(),$CR)     'show images
END FUNCTION

SUB CreateTestFiles(NumberOfImages AS LONG)
LOCAL x AS LONG, hFile AS LONG
LOCAL sJpgFileName AS STRING
FOR x = 1 TO NumberOfImages  'create dummy JPG files
  hFile = FREEFILE
  sJPGFileName= FORMAT$(x)+".tmp"
  OPEN sJPGFileName FOR OUTPUT AS #hFile
  PRINT #hFile, "Data in JPG file"+STR$(x);
  CLOSE hFile
NEXT
END SUB

SUB ConnectRemote
IF %ShowSteps THEN ? "1 connect",%MB_SYSTEMMODAL
slConnect $Server
slOpen $Database,"C"
IF %DropTable THEN slexe "drop table if exists PictureTable"
slexe  "create table if not exists PictureTable(blobkey unique,blobdata)"
END SUB

SUB UpdatePictureTable(NumberOfImages AS LONG)
LOCAL x AS LONG
LOCAL sJpgFileName,sBlob AS STRING
IF %ShowSteps THEN ? "2 write all JPG's",%MB_SYSTEMMODAL
FOR x = 1 TO NumberOfImages
  sJPGFileName= FORMAT$(x)+".tmp"
  GetLocalFile sJPGFileName,sBlob
  PutBlob "key"+FORMAT$(x),sBlob
NEXT
END SUB

SUB GetImages(NumberOfImages AS LONG,sBlobArray() AS STRING)
LOCAL x AS LONG
IF %ShowSteps THEN ? "3 get all JPG's",%MB_SYSTEMMODAL
REDIM sBlobArray(1 TO NumberOfImages) AS STRING
FOR x = 1 TO NumberOfImages
  GetBlob "key"+FORMAT$(x),sBlobArray(x)
NEXT
END SUB
'-------------------------------------------------------------------------------------------------

FUNCTION PutBlob(sBlobKey AS STRING,sBlob AS STRING) THREADSAFE AS LONG

LOCAL NumberOfChanges AS LONG 'function returns 0 on success

NumberOfChanges = slGetChangeCount("T")
slexeBind "replace into PictureTable values('" + sBlobKey + "',?)",slBuildBindDat(sBlob)
IF slGetErrorNumber THEN
   FUNCTION = slGetErrorNumber
   EXIT FUNCTION
END IF
NumberOfChanges = slGetChangeCount("T") - NumberOfChanges
IF NumberOfChanges <> 1 THEN FUNCTION = -9999

END FUNCTION
'-------------------------------------------------------------------------------------------------
FUNCTION GetBlob(sBlobKey AS STRING,sBlobData AS STRING) THREADSAFE AS LONG

sBlobData = ""   'function returns 0 on success
slSel "select blobdata from PictureTable where blobkey ='"+sBlobKey + "'"
IF slGetErrorNumber THEN
  FUNCTION = slGetErrorNumber
  EXIT FUNCTION
END IF
IF slGetRow THEN sBlobData = slf(1)

END FUNCTION
'-------------------------------------------------------------------------------------------------
FUNCTION GetLocalFile(sFileName AS STRING, sData AS STRING) THREADSAFE AS LONG

sData = "" 'function returns 0 on success
LOCAL MaxRetry AS LONG
LOCAL hFile AS LONG

IF ISFALSE(ISFILE(sFileName)) THEN 'local file not found
  FUNCTION = 53                'set error 53, file not found
  EXIT FUNCTION                'exit function
END IF

hFile = FREEFILE              'file handle
FOR MaxRetry = 1 TO 10        'maximum 10 attempts to open
  ERRCLEAR                     'clear error
  OPEN sFileName FOR BINARY ACCESS READ LOCK WRITE AS #hFile 'block writers
  IF ERR THEN                  'open error
   SLEEP 500                   'wait
   ITERATE                     'retry open
  END IF
  GET$ #hFile,LOF(hFile),sData 'read data
  FUNCTION = ERR               '0 if success
  CLOSE #hFile                 'close file
  EXIT FOR
NEXT
FUNCTION = ERR                '10 attempts to open reached

END FUNCTION

FUNCTION PutLocalFile(sFileName AS STRING, sData AS STRING) THREADSAFE AS LONG

LOCAL MaxRetry AS LONG
LOCAL hFile AS LONG

hFile = FREEFILE              'file handle
FOR MaxRetry = 1 TO 10        'maximum 10 attempts to open
  ERRCLEAR                     'clear error
  OPEN sFileName FOR OUTPUT AS #hFile 'exclusive
  IF ERR THEN                  'open error
   SLEEP 500                   'wait
   ITERATE                     'retry open
  END IF
  PRINT #hFile, sData;         'write data
  FUNCTION = ERR               '0 if success
  CLOSE #hFile                 'close file
  EXIT FOR
NEXT
FUNCTION = ERR                '10 attempts to open reached

END FUNCTION


Fredrick Ughimi

CJ,

Both versions ran well. Its interesting.

Adopting it to my application is the tricky thing.

Still working on it.
Fredrick O. Ughimi<br /><br />fughimi@gmail.com<br />- Freedom lies in being bold -- Robert Frost, Poet

Fredrick Ughimi

#25
Not easy adapting your latest codes because I have not separated the image table from the rest of the data. I want all fields in one table together for the time being.

Edit: The REPLACE SQLite Statement is an interesting find.
Fredrick O. Ughimi<br /><br />fughimi@gmail.com<br />- Freedom lies in being bold -- Robert Frost, Poet

Fredrick Ughimi

Hello CJ,

A couple of questions:

What is the difference

between:
FUNCTION PutBlob(sBlobKey AS STRING,sBlob AS STRING) THREADSAFE AS LONG
and:
FUNCTION PutLocalFile(sFileName AS STRING, sData AS STRING) THREADSAFE AS LONG

between:
FUNCTION GetBlob(sBlobKey AS STRING,sBlobData AS STRING) THREADSAFE AS LONG
and:
FUNCTION GetLocalFile(sFileName AS STRING, sData AS STRING) THREADSAFE AS LONG
and:
SUB GetImages(NumberOfImages AS LONG,sBlobArray() AS STRING)

Which of the functions should I use to replace:


slPutFile sTempImageFile, slFN("Picture"), "C"


Best regards,
Fredrick O. Ughimi<br /><br />fughimi@gmail.com<br />- Freedom lies in being bold -- Robert Frost, Poet

Fredrick Ughimi

Cj,

My report works now. Using the PutLocalFile() function replacing slPutFile.


FUNCTION PutLocalFile(sFileName AS STRING, sData AS STRING) THREADSAFE AS LONG

LOCAL MaxRetry AS LONG
LOCAL hFile AS LONG

hFile = FREEFILE              'file handle
FOR MaxRetry = 1 TO 10        'maximum 10 attempts to open
  ERRCLEAR                     'clear error
  OPEN sFileName FOR OUTPUT AS #hFile 'exclusive
  IF ERR THEN                  'open error
   SLEEP 500                   'wait
   ITERATE                     'retry open
  END IF
  PRINT #hFile, sData;         'write data
  FUNCTION = ERR               '0 if success
  CLOSE #hFile                 'close file
  EXIT FOR
NEXT
FUNCTION = ERR                '10 attempts to open reached

END FUNCTION             

PutLocalFile($TempImageFile, slFN("Picture"))
PutLocalFile($TempSignatureFile, slFN("Signature"))


Thanks a million.


Fredrick O. Ughimi<br /><br />fughimi@gmail.com<br />- Freedom lies in being bold -- Robert Frost, Poet

cj

#28
You are very welcome

Not usng slPutFile means application can stay in remote mode and write locally, great.
If slGetfile is ever used a switch back to local mode then remote mode will be required, bad.

GetLocalFile and PutLocalFile access local files on network so no SQL or SQLitening routines are loaded/reloaded.
This eliminates reloading local and remote routines using slSetProcessMods "L0" and slSetProcessMods "L1"

If in local mode the remote database was not available
If in remote mode the local files were not available using slGetFile or slPutFile
The  slSetProcessMods "L1" needed to be called before using remote routines
The  slSetProcessMods "L0" needed to be called if returning to access local .JPG files and temp file.

Note:
This application uses a single remote database.
If a local database was also needed, sllPushDatabase (save handle) and slPopDatabase (get handle)
would be needed to switch back and forth to use the handle of the current database.

Fredrick Ughimi

CJ,

I tried using the GetLocalFile() and I noticed that it only saves one image out of two images presented to it.
For instance I want to save a Picture and Signature images:


m_sPixName = Ughimi.jpg
GetLocalFile(m_sPixName, m_sPicture)
'--------------------------------------------'
m_sSignatureName = Signature1.jpg
GetLocalFile(m_sSignatureName, m_sSignature)


Only the Signature1.jpg would saved in its field. But if I comment out:


m_sPixName = "Ughimi.jpg"
GetLocalFile(m_sPixName, m_sPicture)
'--------------------------------------------'
'm_sSignatureName = "Signature1.jpg"
'GetLocalFile(m_sSignatureName, m_sSignature)


Ughimi.jpg would be saved in it's field.

How do I fix that?


     
Fredrick O. Ughimi<br /><br />fughimi@gmail.com<br />- Freedom lies in being bold -- Robert Frost, Poet