Sign in to follow this  
Followers 0
alexlimlexart

one sqlite database to multiple autoit users

13 posts in this topic

I was reading this "How to corrupt your database" in the sqlite help file and my condern right now is a single sqlite database being used by multiple autoit at the same time. This means that different users in the network are opening/writing/saving to a single shared sqlite database. Is this possible? If not then what are the best approach for this to prevent corruption? And the file lock thing mentioned in the help file.. will this prevent multiple users from accessing the file at the same time?

Thanks in advance.

Share this post


Link to post
Share on other sites



Hey there.

Yes, network shares are way dangerous because network file locks don't work the same way as local file system locks.

You can use a "semaphore" system, I know I've seen it in the forum in the past.

Ah, here it is: www.autoitscript.com/forum/topic/117033-file-locking-with-cooperative-semaphores/

Basically it creates a folder via command line, in the same directory structure as the database file.

Whenever you want to write to the database, you need to check for the existance of that semaphore folder. If it's there, some other user is writing to the database, thus locking it.

When you stop writing to the databse, remove the folder, thus removing the lock.

You also need to implement a timeout feature that will bypass a lock when it's there for too long (these are called dead locks, I guess).

footswitch


Share this post


Link to post
Share on other sites

I know this isn't what you asked, but let me say that SQLite just wasn't made for concurrent access over a network share.

My suggestion is, if this isn't a portable software, neither is it stored in one of those standalone network drives, and if you have administrative privileges over the server, you may as well install MySQL or MS SQL Express, which handle "locks" appropriately.

There are UDFs for those as well; though not officially supported, the SQL one for instance works really well for me.

Good luck either way :)


Share this post


Link to post
Share on other sites

Hi alex,

Please read again what I wrote about this in your previous thread.

Now you have basically two ways to handle this: either drop SQLite and use a client/server engine instead or workaround the unreliable file locking issue.

The latter solution is much easier to manage: for a way to do it.

@footswitch,

I disagree: SQL indeed _was_ made for shared access. It just happens that no commonly available remote file locking protocol works correctly. This isn't SQLite fault, since its code is correct.


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

#5 ·  Posted (edited)

jchd,

I'm not fond of disagreeing over semantics :)

In SQLite, "shared access" is supported within the same "machine".

From what I've read some time ago (in Mike Owens' book if I'm not mistaken), when SQLite was first developed, shared access across the network was an intention but not an actual goal, not one that was "written in stone" anyways. If you ask me, it still remains very much so.

EDIT: but yes you're absolutely right that it's not SQLite's "fault"!

Edited by footswitch

Share this post


Link to post
Share on other sites

I do this thing where I create a text file in the same network folder as the database and I write the local computer name in the text file.

If the text file exists and it is not "My" @ComputerName on line 1 then I know someone else is using the database, so I wait until the text file is removed before "My Computer" then creates a new one and writes its @ComputerName to the file.

So far this has worked OK but I don't have a deadlock get around

Share this post


Link to post
Share on other sites

All replies are good... I am so limited with privileges and so looks like a "turn based" system would work plus a timeout. Jchd, glad you replied. I know what you meant from the other thread but i just wanted to put this into a new one. I guess i am going on a fair direction. Great help from here thanks a lot guys.

Share this post


Link to post
Share on other sites

#8 ·  Posted (edited)

As I said, I've something much better in the works, but not yet ready for consumption.

Anyway, keep in mind that reads (SELECTs) also need to read-lock the DB file: searching an index or doing a full table scan while another process is changing the DB under your feet would certainly be a very bad thing!

Hence the locking semaphore file has to be used not only for writes (UPDATE, INSERT, CREATE, ALTER, REINDEX, ..., various pragmas).

Note that the semaphore file is subject itself to the same locking issues than the DB, albeit at a slower pace and without the difference in locking types SQLite uses. So it isn't a perfectly failproof method either.

What I and other say about all this is nonetheless overly pessimistic in many use cases. In practice, unless you're doing a heavy use of the DB from 20+ machines, you're unlikely to encounter corruption easily (modulo decent network reliability). But as the saying goes, better sad than sorry.

Have a look and use this wrapper UDF to backup your DB at times. It will happily hot backup on the fly reliably (but backup restarts when a write is made, if using the default journaling mode).

In any event, make good use of transactions to group statements that need to operate fast (bulk inserts) or as an atomic block (typically read-modify-write operations).

Right after openning a DB connection, use _SQLite_SetTimeout with a (ms) value greater than the duration of the worst possible sequence of worst duration statements you might ever encounter. To make a long story short, don't hesitate to make it 5 or 10 minutes or much more if you need much more, meaning "wait forever, but not that much".

Use BEGIN IMMEDIATE .. COMMIT transactions. With that, you are sure to never get SQLITE_BUSY nor risk a DB deadlock. This way, you don't even have to test for that.

I'll do my best to come up with a better transparent and reliable LAN support but my current "half homeless" situation makes it difficult for me to work normally.

Edited by jchd
1 person likes this

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

As I said, I've something much better in the works, but not yet ready for consumption.

JCHD, is this anything near consumption :)

Share this post


Link to post
Share on other sites

I'm really sorry to say NO.

I've not written a single line of AutoIt code for months and that situation will last for some time.

In the meantime, I've heard of a promising solution which I've had no time to try myself.

Have a look at this. From what I read it should be fairly easy to adapt the standard SQLite UDF to invoke this library instead.


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

Ok, that's clear. Thanks for the link, this seems an interesting approach to try out.

Share this post


Link to post
Share on other sites

Why struggle with the inadequacies of SqlLite and just use MySql instead?

It's like if you know it has issues, irregardless of who's fault it is, then why bother using it at all if it can't be fixed?

In fact, if you took the time to read the FAQ, its all layed out here:

http://www.sqlite.org/faq.html#q5

If it isn't the right tool, trying to fit a round peg into a square hole is a complete waste of time. Bite the bullet and use something else.

Share this post


Link to post
Share on other sites

Fast, flexible, reliable, unumcombered from legaleese, portable, blind easy to setup, predictable, stable, ...

These are only some of the advantages of SQLite over most (if not all) RDBMS, especially over MySQL.

I fail to see why the fore-mentionned DLL wouldn't fit the sqare hole.


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

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
Sign in to follow this  
Followers 0

  • Similar Content

    • Burgs
      By Burgs
      Greetings,
        I have SQLite setup within my AutoIT program...I'm trying to accomplish what should be a relatively simple task.  I want to be able to return an array of 'table' names for an established database...I believe this might be possible using the '_SQLite_SQLiteExe' command...since it seems to be able to access SQLite schemas...?  The ".tables" command is one of them...if I am not mistaken that command returns a list of all table names in the active database.
        I am attempting the following:
      #include <SQLite.au3> #include <SQLite.dll.au3> Global $hDb, $sIn, $sOut ... $sIn = ".tables" & @CRLF _SQLite_SQLiteExe($hDb, $sIn, $sOut) if @error == 0 Then ;Show Table (using SQLite3.dll) Else if @error == 2 Then ConsoleWrite("ERROR: Sqlite3.exe file not found" & @CRLF) Else ConsoleWrite("ERROR: @error=" & @error & " when calling _SQLite_SQLiteExe" & @CRLF) EndIf ;@error is "2"...OR NOT... EndIf ;@error is "0"...OR NOT... ...   The error being thrown is "ERROR: Sqlite3.exe file not found" ...
        Am I required to have the Sqlite3.exe installed in my directory (i.e. @ScriptsDir)...???  I do not have it in there at present because I did not believe it was necessary with the 'include' calls to "SQLite.au3" and "SQLite.dll.au3"...any advice appreciated.  Thanks in advance. 
      Regards
       
       
       
       
       
       
       
    • FrancescoDiMuro
      By FrancescoDiMuro
      Good evening guys
      I am working on a little project, in which I have to retrieve 8000+ rows of data from a table, from a SQLite database, to populate a combobox 
      This is what I tried 'til now, but it still takes about 12 seconds to populate the combobox.
      _SQLite_Exec($objDatabase, "BEGIN TRANSACTION;") If _SQLite_GetTable($objDatabase, "SELECT DISTINCT Comune FROM LISTA_COMUNI;", $arrRisultatoQuery, $intRighe, $intColonne) = $SQLITE_OK Then For $i = 2 To UBound($arrRisultatoQuery) - 1 If $i < UBound($arrRisultatoQuery) - 1 Then GUICtrlSetData($cbo_ComuneNascita, $arrRisultatoQuery[$i] & "|") Else GUICtrlSetData($cbo_ComuneNascita, $arrRisultatoQuery[$i]) EndIf Next _SQLite_Exec($objDatabase, "COMMIT;") Are there any other solution to retrieve 8000+ records from a SQLite database?
      Thank you very much  
    • FrancescoDiMuro
      By 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!  )
      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
    • FrancescoDiMuro
      By FrancescoDiMuro
      Good morning guys
      I was trying to use again SQLite in AutoIt, and I've again an issue: I can't startup SQLite...
      Can you please show me the way to set the enviornment for a SQLIte usage in AutoIt? Step by step, I'll follow your suggestion(s).

      Thanks for everyone will reply!

      Francesco
    • FrancescoDiMuro
      By FrancescoDiMuro
      Good evening everyone
      I'm working on this little project for a week, and, what I'm trying to do could be useful for many users as well...
      I'm trying to do a "Report Generator", which reads the data that have to report from a text file (.txt) formatted with this pattern;
      Data1;Data2;Data3;Data4;Data5;;

      YES, there are 2 semi-colon at the end of the line.

      In detail, Data1 is a date/time stamp with this format: YYYY/MM/DD HH:MM:SS ;

      When the script starts, the user is prompted to choose 2 dates which I'll call as:
      Report_Date_Start; Report_Date_Start. So, the report, should cover all dates between Report_Date_Start AND Report_Date_End.

      And, already at this point, I don't know how to do the query... How can I say to the script:
      SELECT * FROM (.txt) WHERE Data1 BETWEEN Report_Date_Start AND Report_Date_End; ?

      I thought that I could do a _DateDiff, but if the difference between the two dates is months and not days, how can I do the trick?
      Should I make a Switch...Case with the _DateDiff() and see then calculate all the dates between Report_Date_Start AND Report_Date_End... But then, how can I compare the dates in the file with all the dates between Report_Date_Start AND Report_Date_End? I'm going crazy, I know...

      I've already made a "Export Tool", which exports the content of the .txt file in a .db, managed with SQLite... I mean, there I could easily do a query like I did above the thread, but, this "export", for 1080 rows, takes 28 seconds to be done. And, 1080 rows are daily rows that are added every day in the .txt file, so, in a week, the file could be easily 7000+ rows, which means that the "export" would take 3 minutes to be done... And we can go over and over...
      I'll post just for be "complete" what I've done about the export, so, maybe, someone could say how to improve it in terms of efficency...
       
      Local $aContenutoFileAuditReport = "" _FileReadToArray($sFileAudit_Report, $aContenutoFileAuditReport) If(IsArray($aContenutoFileAuditReport) And Not @error) Then Local $aContenutoFileAuditReport_Splitted = "" Local $sQuery = "" Local $hInizioConteggio = TimerInit() For $i = 1 To UBound($aContenutoFileAuditReport) - 1 $aContenutoFileAuditReport_Splitted = StringSplit($aContenutoFileAuditReport[$i], ";") $sQuery = "INSERT INTO FileDB_Report(DATESTAMP, TIMESTAMP, USER_ID, OBJECT_ID, DESCRIPTION, COMMENT) " & _ "VALUES(" & _ _SQLite_FastEscape($aContenutoFileAuditReport_Splitted[0]) & "," & _ _SQLite_FastEscape($aContenutoFileAuditReport_Splitted[1]) & "," & _ _SQLite_FastEscape($aContenutoFileAuditReport_Splitted[3]) & "," & _ _SQLite_FastEscape($aContenutoFileAuditReport_Splitted[4]) & "," & _ _SQLite_FastEscape($aContenutoFileAuditReport_Splitted[5]) & "," & _ _SQLite_FastEscape($aContenutoFileAuditReport_Splitted[6]) & ");" If(_SQLite_Exec($hFileDB_Report, $sQuery) <> $SQLITE_OK) Then ConsoleWrite("Errore durante l'esecuzione della query #" & $i & @CRLF) Else ConsoleWrite("Query eseguita correttamente #" & $i & @CRLF) EndIf Next ConsoleWrite("Esportazione completata in: " & Round(TimerDiff($hInizioConteggio)/1000, 0) & " secondi") Else MsgBox($MB_ICONERROR, "Errore!", "Errore durante la lettura del file nell'array." & @CRLF & "Errore: " & @error) EndIf I know that I can't do queries from a .txt file...
      [19:18] 
      I've been writing this post from 18:40 maybe...
      By the way, if @jchd or someone else could tell me if I can import a formatted .txt file in SQLite and then, do queries on the DB, I'd be very happy for that...
      About the report in PDF, I'm talking with @taietel in order to know how to create a PDF.
      I hope someone will help me
      Sorry for the "long" list of questions...
      Thank you for everything you've done for me
      I have to say that this is the community of programming language that I've loved most!
      By the way, I'll be back tomorrow in the morning ( ~ 9:15 a.m. Italian time ), so, excuse me if I can't answer before that time.
      Hope you guys have a wonderful day/night.

      Thanks again

      Francesco