• Welcome, Guest. Please login.
 
March 30, 2020, 01:07:13 am

News:

Welcome to the SQLitening support forums!


slSelBind

Started by Fim, December 28, 2019, 09:17:41 am

Previous topic - Next topic

Fim

What I'm getting is slSelBind is a method of using "Prepared Statement Object" in SQLite
In an ERP system there is something called "requirements explosion" and I have programmed it, works ok, but it takes time.
This slSel-sts is executed 220 times for a particular item:
"SELECT MTRLNR, POS, QUANT, FROM_VECKA, TOM_VECKA, SORT_1 FROM STR WHERE ARTNR =" + $ SQ + TRIM $ (ARTNR) + $ SQ + "ORDER BY MTRLNR"
Instead, I've tried this:
slFIX = slBuildBindDat ("$ SQ + TRIM $ (ARTNR) + $ SQ")
..
..
slSelBind ("SELECT MTRLNR, POST, QUANTITY, FROM_VECKA, TOM_VECKA, SORT_1 FROM STR WHERE ARTNR =? ORDER BY MTRLNR", slFIX, SET_NUMMER_TABELL (NIVA))

But it does not work, I think wrong, but I do not know what.
Fim W

Fim

Thanks,
I'll dig into this and see if I understand it.
/Fim W.
Fim W

cj

Quote from: undefinedThanks,
I'll dig into this and see if I understand it.
/Fim W.
Welcome.
Hope this is easier to follow.
Happy New Year!

MACRO bt(parm) = slBuildBindDat(TRIM$(parm),"T")

'select bind text
searchfor="HEIDI"
slSelBind "Select c1,c2 From t1 Where c1 = ? Order by c2", slBuildBindDat(TRIM$(searchfor),"T")
slSelBind "Select c1,c2 From t1 Where c1 = ? Order by c2", bt(searchfor)

'insert bind text
sColumn1 = "Heidi"
sColumn2 = "Klum"
slexeBind "Insert into t1 values(?,?)", bt(sColumn1) + bt(sColumn2)

'==========================================================================================
'Example with optimized GetRecordSet function

#INCLUDE "sqlitening.inc"
MACRO bt(parm) = slBuildBindDat(TRIM$(parm),"T")

FUNCTION PBMAIN () AS LONG
LOCAL sColumn1,sColumn2,searchfor,sEverything,rs() AS STRING, x AS LONG

slOpen    "junk.db3","C"
slexe    "Drop Table If Exists T1"
slexe    "Create Table If Not Exists T1(C1 text collate nocase,C2 text collate nocase)"

'insert bind text
sColumn1 = "Heidi"
sColumn2 = "Klum"
slexe "begin immediate"
FOR x = 1 TO 10
  slexeBind "Insert into t1 values(?,?)",bt(sColumn1) + bt(sColumn2)
NEXT
slexe "end"

'select bind text
searchfor = "hEidi"
slSelBind "Select c1,c2 From t1 Where c1 = ? Order by c2", bt(searchfor)

Display data using returned function and by traversing the rs() array
sEverything = "Returned string value" + $CR +  GetRecordSet(rs(), $TAB) + $CR + $CR
sEverything+="Loop through rs() array" + $CR

FOR x = LBOUND(rs) TO UBOUND(rs)
  sEverything+= USING$("#  &&",x,rs(x),$CR)
NEXT
? sEverything,,"Optimized GetRecordSet"
END FUNCTION

'==========================================================================================
FUNCTION GetRecordSet (sRecordSet() AS STRING,sColumnDelimiter AS STRING) AS STRING
LOCAL colnum,rownum,columns,highelement AS LONG
columns = slGetColumnCount                          'number of columns to return
DIM sCol(1 TO columns) AS STRING                    'avoid concatenation of current row
highelement = 10000                                'arbitrary top limit of recordset array

REDIM sRecordset(0 TO highelement) AS STRING        'column names will be in element 0
FOR colnum=1 TO columns                            'for
  sCol(colnum) = slGetColumnName(colnum)            ' get column name for the column
NEXT                                                ' next
sRecordset(0) = JOIN$(sCol(),sColumnDelimiter)      'concatenate into sRecordset(0)

DO WHILE slGetRow                                  'read recordset loop
  INCR rownum                                        'actual number of reads rows
  IF rownum > highelement THEN                      'redim recordset array if needed
   highelement = highelement + 10000
   REDIM PRESERVE sRecordset(0 TO highelement)
  END IF
  FOR colnum = 1 TO columns                          'read columns of current row
   sCol(colnum) = slf(colnum)                        'place into current column array
  NEXT
  sRecordset(rownum) = JOIN$(sCol(),sColumnDelimiter)'concatenate columns into current recordset(row)
LOOP
IF rownum = 0 THEN
  ERASE sRecordSet
  EXIT FUNCTION                                      'exit if no rows returned
END IF
REDIM PRESERVE sRecordset(0 TO rownum)              'shrink array to actual size
FUNCTION = JOIN$(sRecordSet(),$CR)                  'optionally return as string
END FUNCTION

Fim

Thank you, that looks good.
Right now I have other problems.
Will return.
Fim W

cj

January 15, 2020, 12:49:51 pm #4 Last Edit: January 17, 2020, 07:46:27 am by cj
Fim,

Note: TRIM$ causes problems with lines requiring leading or trailing spaces.
I put it into a macro only for convenience in an above example.
MACRO btt(parm) = slBuildBindDat(TRIM$(parm),"T") 'bind trim text (not good loading files)
MACRO bt(parm)=slBuildBindDat(parm,"T")'bind text

Binding makes loading or saving files or strings into columns work "as is" without the $SQ issues.
Binding strings without "T" should only be used with binary/blob data so it compares correctly.

#INCLUDE "sqlitening.inc" 'fim00.bas
FUNCTION PBMAIN () AS LONG
 LOCAL COL AS LONG, sresult, search AS STRING
 slOpen
 slexe "create table t1(C1 text collate nocase,C2 text collate nocase)"
 slexeBind "insert into t1 values(?,?)",slBuildBindDat("Heidi","T") + slBuildBindDat("Klum","T")
 slexeBind "insert into t1 values(?,?)",slBuildBindDat("Heidi","T") + slBuildBindDat("Mouse","T")
 slexeBind "Insert into t1 values(?,?)",bt("Heidi") + bt("Macro used") 'for convenience
 search = "HEIDi"
 slSelBind "Select c1,c2 From t1 Where c1 = ? Order by c2", slBuildBindDat(search,"T")
 DO WHILE slGetRow:FOR COL=1 TO slGetColumnCount:sresult+=USING$("&&",slf(COL),$TAB):NEXT:sresult+=$CR:LOOP:?sresult
END FUNCTION