Jump to content
FrancescoDiMuro

Fastest way to import data in SQLite DB

Recommended Posts

FrancescoDiMuro

Good morning community! :)

I am working on a script which read from a text file ( .txt ) and should import all the content in a SQLite3 DB, in order to execute some queries that should be difficult to execute on a text file.
So, I was looking for something very very fast, because the file could be very large ( I don't know exaclty how much can became big, but I know a lot of rows, it's a log file ... )
I found the "Import method", but I don't know If I can implement it in a query ( @jchd, it's your turn! :D )
Do you know some methods that I can implement in my script to have a very very fast import of thousands and thousands rows in a SQLite3 DB? :)

Thanks a lot :)

Francesco

Edited by FrancescoDiMuro

Click here to see my signature:

Spoiler

I will always thank you for the time you spent for me.
I'm here to ask, and from your response, I'd like to learn.
By my knowledge, I can help someone else, and "that someone" could help in turn another, and so on.

Share this post


Link to post
Share on other sites
jchd

Can you post an example of your input file format, its field types, along with the full DB schema?

Also is it a one time process or something which will run often?


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)

Share this post


Link to post
Share on other sites
FrancescoDiMuro

Good morning @jchd! :) I wish you replied to me! :D

The process could run once a day or more, but the file could become very large...
Everyday are stored lines and lines of text... And this could be for years...

The .txt file has this format:
 

"Time_ms"   "MsgProc"   "StateAfter"    "MsgClass"  "MsgNumber" "Var1"  "Var2"  "Var3"  "Var4"  "Var5"  "Var6"  "Var7"  "Var8"  "TimeString"    "MsgText"   "PLC"   "Checksum"
42864651050.3009    1   1   3   70018                                   "2017-05-09 15:37:31"   "Importazione gestione utenti terminata senza errori."      rltVew

With tabs too...

I did also a BEGIN TRANSACTION and COMMIT at the start and at the end of all INSERTs...
So, the script should run this:
BEGIN TRANSACTION;
     INSERT 1...
     INSERT 2...
     INSERT 3...
     INSERT N...
COMMIT;

This should run faster, or not? :)

Thank you dear!

Francesco

EDIT:

I read the .txt file with _FileReadToArray, then, in a For...Next loop, I remove the tabs, insert some ; , and split the text in order to do the INSERT...
 

Edited by FrancescoDiMuro

Click here to see my signature:

Spoiler

I will always thank you for the time you spent for me.
I'm here to ask, and from your response, I'd like to learn.
By my knowledge, I can help someone else, and "that someone" could help in turn another, and so on.

Share this post


Link to post
Share on other sites
AspirinJunkie

The sqlite3.exe knows a .import-statement which works very fast (more explanation).
For this you need the data in csv-Syntax.
If your data does not fit this requirement you can use AutoIt to convert your data into a csv-style.
Then you can import the data by using the _SQLite_SQLiteExe()-function.

Share this post


Link to post
Share on other sites
FrancescoDiMuro
16 minutes ago, AspirinJunkie said:

The sqlite3.exe knows a .import-statement which works very fast (more explanation).
For this you need the data in csv-Syntax.
If your data does not fit this requirement you can use AutoIt to convert your data into a csv-style.
Then you can import the data by using the _SQLite_SQLiteExe()-function.

Thanks for your reply @AspirinJunkie:)

Is this method faster then the normal INSERT with TRANSICTIONS?

And, can I use the .import in a query executed with _SQLite_Exec? Or I have to do it through CMD? 

Thank you :) 


Click here to see my signature:

Spoiler

I will always thank you for the time you spent for me.
I'm here to ask, and from your response, I'd like to learn.
By my knowledge, I can help someone else, and "that someone" could help in turn another, and so on.

Share this post


Link to post
Share on other sites
AspirinJunkie
47 minutes ago, FrancescoDiMuro said:

Is this method faster then the normal INSERT with TRANSICTIONS?

My last tests with this are long ago. But in my mind the .import-function was much faster than insert-statement - even with transaction.
Especially at big number of inserts.

47 minutes ago, FrancescoDiMuro said:

And, can I use the .import in a query executed with _SQLite_Exec? Or I have to do it through CMD? 

_SQLite_SQLiteExe() is an wrapper for the sqlite3.exe. It doesn't use the sqlite3.dll like the other _SQLITE*-functions do.
So take care that the function can find a proper sqlite3.exe and then this function can do everything what the sqlite3.exe can do.

Edited by AspirinJunkie

Share this post


Link to post
Share on other sites
FrancescoDiMuro

@AspirinJunkie

So I should run sqlite3.exe and then, send commands to that process ( .import... ).
Am I correct?

Thank you :)

Francesco


Click here to see my signature:

Spoiler

I will always thank you for the time you spent for me.
I'm here to ask, and from your response, I'd like to learn.
By my knowledge, I can help someone else, and "that someone" could help in turn another, and so on.

Share this post


Link to post
Share on other sites
AspirinJunkie

No - you should use the _SQLite_SQLiteExe()-function as it's described in the helpfile.
In the example for the function the use of the .import-statement is also described.

You don't need to manually run the sqlite3.exe by yourself.

Edited by AspirinJunkie

Share this post


Link to post
Share on other sites
FrancescoDiMuro

@AspirinJunkie, thanks for the reply :)

But now, a question arises...

How can I, always in the fast way possible, replace @TAB with ";", and put some double quotes in the fields of the text file, in order to let recognize the .import of SQLite3? :)

Thank you! :) 


Click here to see my signature:

Spoiler

I will always thank you for the time you spent for me.
I'm here to ask, and from your response, I'd like to learn.
By my knowledge, I can help someone else, and "that someone" could help in turn another, and so on.

Share this post


Link to post
Share on other sites
AspirinJunkie

Why you wan't to replace the @TABs? As in the example to _SQLITE_SQLiteExe there is a .separater-statement where you can define that the separator for the data fields is a @TAB instead of a ;

Then the double quotes should only be necessary if a field contains a @TAB as data.

Share this post


Link to post
Share on other sites
FrancescoDiMuro

@AspirinJunkie,

The double quotes are needed for let recognize the .csv from the sqlite3.exe, or not?

Example:

; This should be the format of a CSV:

"Data1";"Data2";"Data3";"DataN";     ; Keyboard and Regional Settings are Italian

If I have data without those double quotes, how can I let recognize the .csv format from sqlite3.exe?

Thank you :) 


Click here to see my signature:

Spoiler

I will always thank you for the time you spent for me.
I'm here to ask, and from your response, I'd like to learn.
By my knowledge, I can help someone else, and "that someone" could help in turn another, and so on.

Share this post


Link to post
Share on other sites
AspirinJunkie

If you set the separater to @TAB then you only need double quotes around a data field if the data field contains Tabs as data.
If the file structure is really like you said (@Tab as separator, column-names in first row) then the import for this file is quite simple:

#include <SQLite.au3>

Global $s_InputFile_Path = @ScriptDir & "\mydata.txt"
Global $s_DataBase_Path = @ScriptDir & "\mydatabase.db"
Global $s_Out

_SQLite_SQLiteExe($s_DataBase_Path, _
    ".separator \t" & @CRLF & _
    ".import '" & $s_InputFile_Path & "' testtable" & @CRLF _
    , $s_Out)

just put a valid sqlite3.exe (maybe from here) in the directory of your script.
The script should then create a new database with a new table named "testtable" filled with your data from "mydata.txt".
You can erase all double quotes because it seems that your data fields doesn't contain the separator as data.

Share this post


Link to post
Share on other sites
FrancescoDiMuro

I just tried this directly in the sqlite3.exe, and it returned this message:

Quote

Error: multi-character column separators not allowed for import

Does the command .import should create the table, or have I to create it before the import?

Thanks :) 


Click here to see my signature:

Spoiler

I will always thank you for the time you spent for me.
I'm here to ask, and from your response, I'd like to learn.
By my knowledge, I can help someone else, and "that someone" could help in turn another, and so on.

Share this post


Link to post
Share on other sites
AspirinJunkie

I don't know what you exactly done.
I gave you an example - did you tried it?

35 minutes ago, FrancescoDiMuro said:

Does the command .import should create the table, or have I to create it before the import?

it's your choice.
If the table already exist when you use the .import statement then you have to delete the first row of the file because it would treatened as a data row and not as the header names.

Share this post


Link to post
Share on other sites
FrancescoDiMuro

Yes, I tested your code, and it does nothing 'cause it can't find the "sqlite3.exe", even If I set it in the function:

_SQLite_SQLiteExe($sUser_DB, _
                                ".separator \t" & @CRLF & _
                                ".import '" & $sUser_TXT_File & "' ALLARMI" & @CRLF , _
                                $sOutput, _                                     
                                @ScriptDir & "\SQLite\sqlite3.exe")

I tested too this pattern directly from sqlite3.exe, and it returns the error I've posted in the post #13...

What am I doing wrong?

Thanks :) 


Click here to see my signature:

Spoiler

I will always thank you for the time you spent for me.
I'm here to ask, and from your response, I'd like to learn.
By my knowledge, I can help someone else, and "that someone" could help in turn another, and so on.

Share this post


Link to post
Share on other sites
AspirinJunkie
19 minutes ago, FrancescoDiMuro said:

it does nothing 'cause it can't find the "sqlite3.exe"

Why do you know that's the reason?
Because of the @error-value?

Did you tried to download the sqlite3.exe from the source i gave you and copy it directly to the script-directory?

Share this post


Link to post
Share on other sites
jchd

You can significantly speed up the process by using bulk inserts, e.g. grouping dozens of inserts into one statement:

insert into T values (<values for fields of row1>), (<values for fields of row2>), (<values for fields of row3>), ..., (<values for fields of rowN>);

Finally you can use the SQLite3 CLI (command line interpreter) like so:

C:\Users\jc\Documents\AutoMAT\tmp>sqlite3 testimport.sq3
SQLite version 3.18.0 2017-03-28 18:48:43
Enter ".help" for usage hints.
sqlite> .separator \t
sqlite> .import tstin.dsv ImportedData
Error: cannot open "tstin.dsv"
sqlite> .import tstin.csv ImportedData
sqlite> select count(*) from ImportedData;
11183
sqlite> select * from ImportedData limit 5;
42864651050.3009        1       1       3       70018
                2017-05-09 15:37:31     Importazione gestione utenti terminata senza errori.
rltVew
42864651050.3009        1       1       3       70018
                2017-05-09 15:37:31     Importazione gestione utenti terminata senza errori.
rltVew
42864651050.3009        1       1       3       70018
                2017-05-09 15:37:31     Importazione gestione utenti terminata senza errori.
rltVew
42864651050.3009        1       1       3       70018
                2017-05-09 15:37:31     Importazione gestione utenti terminata senza errori.
rltVew
42864651050.3009        1       1       3       70018
                2017-05-09 15:37:31     Importazione gestione utenti terminata senza errori.
rltVew
sqlite> .quit

C:\Users\jc\Documents\AutoMAT\tmp>

Sorry for being late to the party: I started composing the answer much earlier but had to move urgently.


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)

Share this post


Link to post
Share on other sites
FrancescoDiMuro
20 minutes ago, AspirinJunkie said:

Why do you know that's the reason?
Because of the @error-value?

Did you tried to download the sqlite3.exe from the source i gave you and copy it directly to the script-directory?

I downloaded you sqlite3.exe even If I already had one, and put in the @ScriptDir...
I error checked the function, and it returns @error = 2, which means that it can't find the sqlite3.exe ( Help file ).

I don't know why...

Thanks for your help :) 


Click here to see my signature:

Spoiler

I will always thank you for the time you spent for me.
I'm here to ask, and from your response, I'd like to learn.
By my knowledge, I can help someone else, and "that someone" could help in turn another, and so on.

Share this post


Link to post
Share on other sites
FrancescoDiMuro
17 minutes ago, jchd said:

You can significantly speed up the process by using bulk inserts, e.g. grouping dozens of inserts into one statement:

insert into T values (<values for fields of row1>), (<values for fields of row2>), (<values for fields of row3>), ..., (<values for fields of rowN>);

Finally you can use the SQLite3 CLI (command line interpreter) like so:

C:\Users\jc\Documents\AutoMAT\tmp>sqlite3 testimport.sq3
SQLite version 3.18.0 2017-03-28 18:48:43
Enter ".help" for usage hints.
sqlite> .separator \t
sqlite> .import tstin.dsv ImportedData
Error: cannot open "tstin.dsv"
sqlite> .import tstin.csv ImportedData
sqlite> select count(*) from ImportedData;
11183
sqlite> select * from ImportedData limit 5;
42864651050.3009        1       1       3       70018
                2017-05-09 15:37:31     Importazione gestione utenti terminata senza errori.
rltVew
42864651050.3009        1       1       3       70018
                2017-05-09 15:37:31     Importazione gestione utenti terminata senza errori.
rltVew
42864651050.3009        1       1       3       70018
                2017-05-09 15:37:31     Importazione gestione utenti terminata senza errori.
rltVew
42864651050.3009        1       1       3       70018
                2017-05-09 15:37:31     Importazione gestione utenti terminata senza errori.
rltVew
42864651050.3009        1       1       3       70018
                2017-05-09 15:37:31     Importazione gestione utenti terminata senza errori.
rltVew
sqlite> .quit

C:\Users\jc\Documents\AutoMAT\tmp>

Sorry for being late to the party: I started composing the answer much earlier but had to move urgently.

I'm not following you, sorry...

EDIT:

I'll try to understand you...
It's better do something like:

"INSERT INTO Sample ( columns ) VALUES ( values of columns, a dozen )"

than a For...Next loop with N inserts? N = Number of rows in the text file - 1.

Thanks @jchd :) 

Edited by FrancescoDiMuro

Click here to see my signature:

Spoiler

I will always thank you for the time you spent for me.
I'm here to ask, and from your response, I'd like to learn.
By my knowledge, I can help someone else, and "that someone" could help in turn another, and so on.

Share this post


Link to post
Share on other sites
AspirinJunkie
1 hour ago, FrancescoDiMuro said:

I downloaded you sqlite3.exe even If I already had one, and put in the @ScriptDir...
I error checked the function, and it returns @error = 2, which means that it can't find the sqlite3.exe ( Help file ).

That's need to be analysed - to ensure it's not a bug.
Put the sqlite3.exe in the same directory as the script (no subfolder or anything else).
Then only change the value of $s_InputFile_Path and leave everything else in the script as is.
Then run the script, post the output of the msgbox and tell if the mydatabase.db is correctly created and filled:
 

#include <SQLite.au3>

Global $s_InputFile_Path = @ScriptDir & "\mydata.txt"
Global $s_DataBase_Path = @ScriptDir & "\mydatabase.db"
Global $s_SQLITE3EXE_Path = @ScriptDir & "\sqlite3.exe"
Global $s_Out

MsgBox(0,"", StringFormat("$s_SQLITE3EXE_Path = %s\nFileExists = %s\nWorkingDir = %s" , $s_SQLITE3EXE_Path, FileExists($s_SQLITE3EXE_Path), @WorkingDir))

_SQLite_SQLiteExe($s_DataBase_Path, _
        ".mode tabs" & @CRLF & _
        ".import '" & $s_InputFile_Path & "' testtable" & @CRLF _
        , $s_Out, _
        $s_SQLITE3EXE_Path)

 

Share this post


Link to post
Share on other sites

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

  • Similar Content

    • dangr82
      By dangr82
       
      I have created this function for a database, but I can not make it work. I always have two error messages: "not an error" ... and the file created, in the script directory, does not contain anything.
      Global $sDBName = "Hen.db" Func DatabaseTable() Local $sConnDB _SQLite_Startup() If @error Then MsgBox($MB_SYSTEMMODAL, "SQLite Error", "SQLite.dll Can't be Loaded!") Exit -1 EndIf $sConnDB = _SQLite_Open($sDBName) If @error Then MsgBox($MB_SYSTEMMODAL, "SQLite Error", "Can't Load Database!") Exit -1 EndIf If Not _SQLite_Exec($sDBName, 'CREATE TABLE Animal ("Name", "Age");') = $SQLITE_OK Then MsgBox($MB_SYSTEMMODAL, "SQLite Error", _SQLite_ErrMsg()) If Not _SQLite_Exec($sDBName, 'INSERT INTO Animale VALUES ("Charlie","5");') = $SQLITE_OK Then MsgBox($MB_SYSTEMMODAL, "SQLite Error", _SQLite_ErrMsg()) _SQLite_Close($sConnDB) _SQLite_Shutdown() EndFunc  
    • Eminence
      By Eminence
      Hello,
      I've been searching on how to select the first row in my database which includes a time column based on the latest start_time date for today's date.
      This is my current code:
      Local $iCurrentDate = _Now() Local $sqlHandle = _dbOpen($sDbPath) Local $aResult, $iRows, $iCols _SQLite_GetTable2d($sqlHandle, "SELECT start_time FROM " & $srawDb & " ORDER BY datetime(start_time) DESC LIMIT 1", $aResult, $iRows, $iCols) _ArrayDisplay($aResult) _dbClose($sDbpath) What it does is it gets the first row entry in the database however it does not select the latest start_time entry in the database, it always select the first row. I've tried changing DESC to ASC to see if that will do it but the issue still remains. Any thoughts on this? Thanks in advance.
       
      *EDIT
      The date format in the database is by MM/DD/YYYY HH:MM:SS.
    • Jefrey
      By Jefrey
      Needed a way to store global temporary & permanent information and came up with this.
      This is inspired by NodeJS's store and store2 packages, as well as W3 specs' localStorage and sessionStorage, offering multiple ways of usage.
      This is not related to any browser's storage, nor will allow you to access or modify browsers storage - although this is possible and not a hard task, this is not what this UDF is intended to do.
      This UDF offers functions for temporary storage (that gets cleaned up once the application is shutdown) that is kept on memory using ScriptingDictionary, as well as for permanent storage, that is saved on the harddisk as an encrypted file.
      sessionStorage (temporary storage)
      It's useful to keep application state and temporary settings accessible by any part of your script (although it could also be done with a global variable, I still prefer this method).
      You have multiple ways, at your choice, to:
      ; add or modify a key sessionStorage("foo", "bar") store("foo", "bar") sessionStorage_set("foo", "bar") sessionStorage_setItem("foo", "bar") ; read a key (returns false if key does not exist) $read = sessionStorage("foo") $read = store("foo") $read = sessionStorage_get("foo") $read = sessionStorage_getItem("foo") ; delete a key sessionStorage_remove("foo") ; delete all keys sessionStorage_clear() sessionStorage_clearAll() localStorage (permanent storage)
      It's useful to store user-defined settings.
      ; initialize ; this is optional, but allows you to control ; how things are going to be saved localStorage_startup([file where you want the settings to be saved], [crypt password]) ; by default, if not supplied, if supplied the "Default" keyword (or if you dont initialize), ; the file will be a random-named file (based on @ScriptFullPath) at user's %APPDATA% ; and the password will also be based on @ScriptFullPath ; you can set only the crypt password if you want: ; localStorage_startup(Default, "mypassword") ; the usage is the same as sessionStorage ; add or modify a key localStorage("foo", "bar") store2("foo", "bar") ; notice the '2' localStorage_set("foo", "bar") localStorage_setItem("foo", "bar") ; read a key (returns false if key does not exist) $read = localStorage("foo") $read = store2("foo") $read = localStorage_get("foo") $read = localStorage_getItem("foo") ; delete a key localStorage_remove("foo") ; delete all keys localStorage_clear() localStorage_clearAll() Download
    • Seminko
      By Seminko
      In my recent project I'm downloading a bunch of data, so I decided to store it in a SQLite database. NOTE: I'm using sqlite3_x64.dll
      Everything is working just fine but I'm struggling with getting the Median value. SQLite has an Average function but not a Median one.
      I googled but all of the provided solutions are way above my pay-grade. After some more searching I found 'extension-functions.c' on the SQLite site where Median is included. After almost an hour of struggling I was able to successfully compile it into a DLL.
      So I downloaded @jchd's SQLiteExtLoad.au3 as seen here:
      But I'm getting these errors:
      "Path\SQLiteExtLoad.au3"(21,40) : warning: $g_hDll_SQLite: possibly used before declaration. Local $RetVal = DllCall($g_hDll_SQLite, ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^ "Path\SQLiteExtLoad.au3"(21,40) : error: $g_hDll_SQLite: undeclared global variable. Local $RetVal = DllCall($g_hDll_SQLite, ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^ So I tried, copying the function to my file and changing the DLL variable ($g_hDll_SQLite) directly to the DLL location (C:\...\.. .dll), but now this error fires
      If __SQLite_hChk($hConn, 1) Then Return SetError(@error, 0, $SQLITE_MISUSE) To be honest, I don't know what to use as the $hConn - handle of connection.
      I would appreciate any help, be it getting the median using SQLite queries or getting the DLL extension loaded using AutoIt.
      Thanks, S.
       
      EDIT: well, I suspect the $hConn variable refers to the return value of the _SQLite_Open function. Well, at least now _SQLite_EnableExtensions doesn't give errors. Now I run into problems with _SQLite_LoadExtension, which gives error -1, and extended 1. Apparently the 1 constant is a generic error where other error do not apply.
      BTW, anybody knows whether I need to compile the extension DLL "into" x64 when I use a x64 SQLite? That might be the problem...
       
      EDIT2: I recompiled the dll and tried it using the SQLite3.exe and it works, so I'm confident the extension DLL has been created correctly

    • AdamUL
      By AdamUL
      I have been testing AutoIt 3.3.14.3 with SQLite, and the Help File examples.  The examples that I have tested are throwing errors or not doing anything.  I have sqlite3.dll, sqlite3_x64.dll, and sqlite3.exe in the directories with the testing script.  I am copying the examples directly from the help file into a test script for testing with no edits.  The _SQLite_GetTable2d example is returning an error ("Library used incorrectly") with each _SQLite_Exec command.  The _SQLite_FastEncode example returns an empty dialog box.  The _SQLite_Exec example only prints out the SQLite version, and nothing else in the SciTE console.  Currently, I'm still searching for what is causing this issue.  I'm on Windows 7 Enterprise 64-bit.  Is anyone else having this issue?  
       
      Adam
       
×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.