• Welcome, Guest. Please login.
 
July 17, 2019, 03:56:50 pm

News:

Welcome to the SQLitening support forums!


RE: Using multiple databases with slPushDataBase

Started by cj, May 31, 2014, 02:08:06 pm

Previous topic - Next topic

cj

Shows how database handle can be saved so database is not reopened with a new handle using slOpen.


#DIM ALL    'Test slPushDataBase/slPopDataBase
#INCLUDE "sqlitening.inc"
#INCLUDE "win32api.inc"
%Databases=3
%NumberOfTests=3

FUNCTION PBMAIN () AS LONG
  DIM sArray() AS STRING, sDBHandle() AS STRING, s AS STRING
  DIM counter AS LONG, dbNumber AS LONG
  DIM sDBHandle(%DataBases)
  FOR counter = 1 TO %NumberOfTests
    FOR dbNumber = 1 TO %Databases
      IF counter = 1 THEN                   'first time
        slOpen "DB"+ FORMAT$(dbNumber),"C"  'open database
        slExe "Drop Table if exists t"      'drop table in each DB, optional
        slPushDataBase sDBHandle(dbNumber)  'push handle
        slPopDataBase  sDBHandle(dbNumber)  'set current db handle
      ELSE
        slPopDataBase  sDBHandle(dbNumber)  'set current db dhandle
      END IF
      slExe "Create Table if not exists t(Column_One)"
      slexe "Insert into t values('Insert - Hello, world! from table t of database  DB" + FORMAT$(dbNumber) + "')"
    NEXT
  NEXT
  FOR dbNumber = 1 TO %DataBases
    slPopDataBase sDBHandle(dbNumber)        'set current db handle
    slSelAry "Select * from t", sArray(), "Q9c"
    s = s +  JOIN$(sArray(),$CRLF) + STRING$(2,$CRLF)
  NEXT
  slClose  'close last DB used
  ? s,, "Done, closed current database" + STR$(%DataBases)

  IF %DataBases > 1 THEN  'prove DB1 open
    slPopDataBase sDbHandle(1)
    slExe "Insert into t values('Insert - Bye world! in DB1')"
    slSelAry "Select * from t", sArray(), "Q9c"
    ? JOIN$(sArray(),$CRLF),, "Prove DB1 is still open"
  ELSE
    ? "No databases open",,"Bye"
  END IF

END FUNCTION

cj

Is it worth it to slPushPopDatabase?
This just uses slOpen and slClose


#DIM ALL    'Test just using slOpen and slClose
#INCLUDE "sqlitening.inc"
#INCLUDE "win32api.inc"
%Databases=3
%NumberOfTests=3

FUNCTION PBMAIN () AS LONG
  DIM sArray() AS STRING
  LOCAL s AS STRING
  LOCAL counter AS LONG, dbNumber AS LONG

  FOR counter = 1 TO %DataBases:KILL "DB" + FORMAT$(counter):NEXT:ERRCLEAR

  FOR counter = 1 TO %NumberOfTests
    FOR dbNumber = 1 TO %Databases
      slOpen "DB"+ FORMAT$(dbNumber),"C"
      slExe "Create Table if not exists t(Column_One)"
      slexe "Insert into t values('Insert - Hello, world! from table t of database  DB" + FORMAT$(dbNumber) + "')"
      slClose
    NEXT
  NEXT
  'display results
  FOR dbNumber = 1 TO %DataBases
    slOpen "DB" + FORMAT$(dbNumber),"C"
    slSelAry "Select * from t", sArray(), "Q9c"
    s = s +  JOIN$(sArray(),$CRLF) + STRING$(2,$CRLF)
    slClose
  NEXT
  ? s,, "Done, closed current database" + STR$(%DataBases)
  IF %DataBases > 1 THEN  'prove DB1 open
    slOpen "DB1","C"
    slExe "Insert into t values('Insert - Bye world! in DB1')"
    slSelAry "Select * from t", sArray(), "Q9c"
    ? JOIN$(sArray(),$CRLF),, "Prove this works"
    slClose
  ELSE
    ? "No databases open",,"Bye"
  END IF
END FUNCTION