Jump to content

Recommended Posts

Posted (edited)

Hello -

For a SQLite insert statement I'm passing a 2D array through two For/Next loops -  code snippet:

Local $sInsertInto2 = ""


For $i = 0 To $iCount - 1
    
    $sInsertInto2 &= "  ("
    
    For $j = 0 To UBound($aTmp, 2) - 2
        $sInsertInto2 &= "'" & StringReplace($aTmp[$i][$j], "'", "''") & "', "
    Next
    
    $sInsertInto2 &= "'" & StringReplace($aTmp[$i][$j], "'", "''") & "')" & ((($iCount - 1) = $i) ? (";") : ("," & @CRLF))
Next

This works great but due to StringReplace() it is very slow. StringReplace() is used to "escape" single quotes (') and it is only required if a single quote actually is present...

Somewhere on the forum there is a faster example by UEZ but I can't find it...

Anyone any idea?

Edited by supersonic
Posted (edited)

Input: "'AutoIt'Is'Great!'"

Output: "'AutoIt''Is''Great!'"

This is not the whole SQLite statement but it is the way a string is transformed IF there is a single quote (red text)...

Edited by supersonic
Posted
  On 1/30/2017 at 9:40 AM, supersonic said:

.....  due to StringReplace() it is very slow. StringReplace() is used to "escape" single quotes (') and it is only required if a single quote actually is present ....

Expand  

have a look to the _SQLite_FastEscape() function instead

 

P.S.

  On 1/30/2017 at 9:40 AM, supersonic said:

Somewhere on the forum there is a faster example by UEZ but I can't find it...

Anyone any idea?

Expand  

maybe you mean this link?

 

image.jpeg.9f1a974c98e9f77d824b358729b089b0.jpeg Chimp

small minds discuss people average minds discuss events great minds discuss ideas.... and use AutoIt....

Posted (edited)

deleted a duplicated post

Edited by Chimp
deleted a duplicated post

 

image.jpeg.9f1a974c98e9f77d824b358729b089b0.jpeg Chimp

small minds discuss people average minds discuss events great minds discuss ideas.... and use AutoIt....

Posted (edited)

Chimp & Juvigy, thank you. The UDF _SQLite_FastEscape() uses StringReplace() w/ case-sense. Using case-sense speeds up things a little bit (22s : 16s). Using SRER seems to as fast as SR w/ case-sense (17s). Using ASM from UEZ is very slow due to DllCall()... With other words: Still any chance to speed it up?

Edited by supersonic
Posted

Lets see the ASM.

All tests, everywhere in the forum, except for the one you just ran, have ASM winning by a large margin.

  Reveal hidden contents

Posted
  On 1/30/2017 at 10:16 AM, supersonic said:

A 2D array is about [40000+][100+]...

Expand  

A really large number of columns!

Are you using a transaction to group your inserts?

If that isn't good enough, you can try binding values (but then again 100s of DllCalls)

Then if the source is a text file, try escaping the whole file first!

If the source is a [valid] .csv, use the CLI (sqlite3.exe) to import it.

  Reveal hidden contents

This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Posted

jchd,

yes, many columns... It is a script to import tables from a Progress DB. I fetch the data via ODBC in steps (e. g. 1000). This works really fine. And I use transaction which greatly improves performance.

Parsing through the source data array index by index seems to be always slow. So the next thing I try is to put the SQLite statement together and afterwards double the single quotes for char. escaping.

Posted

jchd,

first putting together the SQLite insert/replace statement and afterwards escaping the single quotes (StringReplace w/ case-sense) helped me out. Thank you.

Posted

You can also put the whole content in a variable and process the replacement in one shot (with a few steps) :

Local $sInsertInto2 = ""

For $i = 0 To $iCount - 1
    $sInsertInto2 &= "  (" & @CRLF
    For $j = 0 To UBound($aTmp, 2) - 1
        $sInsertInto2 &= $aTmp[$i][$j] & @CRLF
    Next
    $sInsertInto2 &= ")" & @CRLF
Next

$sInsertInto2 = StringReplace($sInsertInto2, "'", "''")
$sInsertInto2 = StringRegExpReplace($sInsertInto2, "\R\K(?!\h*[()])(\V+)", "'$1'")
$sInsertInto2 = StringRegExpReplace($sInsertInto2, "'\K\R(?=')", ", ")
$sInsertInto2 = StringReplace($sInsertInto2, @CRLF, "")
$sInsertInto2 = StringRegExpReplace($sInsertInto2, "\)\K(?=\h+\()", ", " & @CRLF)
$sInsertInto2 &= ";"
ConsoleWrite($sInsertInto2 & @CRLF)

 

Posted (edited)

jguinch,

that's nearly how I did it:

For $i = 0 To $iCount - 1
    $sInsertInto2 &= "  ("
    For $j = 0 To UBound($aTmp, 2) - 2
        $sInsertInto2 &= "`" & $aTmp[$i][$j] & "`, "
    Next
    $sInsertInto2 &= "`" & $aTmp[$i][$j] & "`)" & ((($iCount - 1) = $i) ? (";") : ("," & @CRLF))
Next


$sInsertInto2 = StringReplace($sInsertInto2, "'", "''", 0, 1)
$sInsertInto2 = StringReplace($sInsertInto2, "`", "'", 0, 1)

That's about 4-5 times faster. Setting StringReplace() to case-sensitive also speeds things up. Good enough :lol:

Thank you.

 

Edited by supersonic
Posted

jguinch,

indeed, ternary ops consume some time... Like running afterwards some SRs / SRERs :) At the end there is a difference of about +/- 0,1-0,2 seconds...

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...