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

Thoughts:

  • 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.

/*--------------------------------------------------------------------------------------------------------------------------------------------------------------------------*/

ALWAYS GOOD TO READ:

 

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

Thoughts:

  • 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.

/*--------------------------------------------------------------------------------------------------------------------------------------------------------------------------*/

ALWAYS GOOD TO READ:

 

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

Thoughts:

  • 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.

/*--------------------------------------------------------------------------------------------------------------------------------------------------------------------------*/

ALWAYS GOOD TO READ:

 

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

Thoughts:

  • 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.

/*--------------------------------------------------------------------------------------------------------------------------------------------------------------------------*/

ALWAYS GOOD TO READ:

 

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

Thoughts:

  • 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.

/*--------------------------------------------------------------------------------------------------------------------------------------------------------------------------*/

ALWAYS GOOD TO READ:

 

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

Thoughts:

  • 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.

/*--------------------------------------------------------------------------------------------------------------------------------------------------------------------------*/

ALWAYS GOOD TO READ:

 

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

Thoughts:

  • 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.

/*--------------------------------------------------------------------------------------------------------------------------------------------------------------------------*/

ALWAYS GOOD TO READ:

 

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

Thoughts:

  • 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.

/*--------------------------------------------------------------------------------------------------------------------------------------------------------------------------*/

ALWAYS GOOD TO READ:

 

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

Thoughts:

  • 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.

/*--------------------------------------------------------------------------------------------------------------------------------------------------------------------------*/

ALWAYS GOOD TO READ:

 

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

Thoughts:

  • 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.

/*--------------------------------------------------------------------------------------------------------------------------------------------------------------------------*/

ALWAYS GOOD TO READ:

 

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

    • Eminence
      By Eminence
      Hello,
      Using SQLite, I was trying to select a specific column with having duplicate entries removed by using the DISTINCT function. 
      Local $sQuery = "SELECT DISTINCT supervisor, COUNT(DISTINCT employee_name) FROM data_db What the above snippet does is just list only one supervisor but with the total count of unique employee names in the whole database when it should be listing out all supervisor entries from the database and list the total count of employees per each supervisors. If I remove the COUNT function, it does list out all unique entries of supervisor names from the list. Attached is a screenshot of an example database as well. 
      Any help will be much appreciated. Thank you!

    • Seminko
      By Seminko
      Hey,
      I have a script that uses SQLite. It worked without a hiccup on my W7 system.
      However, last week I bought a new machine, installed W10 and autoit, and now I'm getting an error during _SQLite_Startup.
      Can anyone advise on how to find what's wrong? Tried checking $__g_hPrintCallback_SQLite but that returns nothing.
       
      Tried downloading the latest version of the dll from the link in the function page and I also replaced the default SQLite.dll.au3 that came with AutoIt with the one that came with the latest SQLite version zip.
       
      Relevant parts of my script:
      #include <SQLite.au3> #include <SQLite.dll.au3> _SQLite_Startup(@ScriptDir & "\Config\sqlite3_x64.dll", False, 1) If @error Then MsgBox(16, "SQLite Error", "SQLite3.dll Can't be Loaded! - " & $__g_hPrintCallback_SQLite & @CRLF & @CRLF & "Exiting application / Zavolej Honzovi") Exit -1 EndIf  
      Thanks
      S.
    • FrancescoDiMuro
      By FrancescoDiMuro
      Good evening everyone
      I am building a management for the company I work with, and I just imported a real amount of rows ( about 29000 ), in my SQLite DB.
      The thing I am not understanding, is the time that the script takes to build this amount of rows in the ListView.
      I didn't measure it, but I think it took 2 minutes or so to create each ListView item...
      It is normal that it takes so much time?
      What can I do to improve the creation of the items?

      Here's the code I am using to query and to create ListView items...
      ; Articles ListView: Global $lvwArticles = GUICtrlCreateListView("ID|Fornitore|Codice|Descrizione|EU|Prezzo|Sconto Applicato|Note", 14, 87, 1507, 660, BitOR($GUI_SS_DEFAULT_LISTVIEW,$LVS_SORTASCENDING,$LVS_SORTDESCENDING), BitOR($WS_EX_CLIENTEDGE,$LVS_EX_GRIDLINES,$LVS_EX_FULLROWSELECT)) ; Query $strQuery = "SELECT * FROM ARTICOLI;" ; Query Execution _SQLite_GetTable2d($objDatabase, $strQuery, $arrResult, $intRows, $intColumns) If @error Then ; Error Handling Else ; Cleaning the ListView _GUICtrlListView_DeleteAllItems($lvwArticles) If @error Then ; Error Handling Else ; No records in the Table If UBound($arrResult) < 2 Then ; Error Handling Else _GUICtrlListView_BeginUpdate($lvwArticles) For $intCounter = 1 To UBound($arrResult) - 1 $strListViewItem = $arrResult[$intCounter][0] & "|" & _ $arrResult[$intCounter][1] & "|" & _ $arrResult[$intCounter][2] & "|" & _ $arrResult[$intCounter][3] & "|" & _ $arrResult[$intCounter][4] & "|" & _ $arrResult[$intCounter][5] & "|" & _ $arrResult[$intCounter][6] & "|" & _ $arrResult[$intCounter][7] $objListViewItem = GUICtrlCreateListViewItem($strListViewItem, $lvwArticles) Next _GUICtrlListView_EndUpdate($lvwArticles) EndIf EndIf EndIf Thanks in advance


      Best Regards.
    • 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.
×