Jump to content
Sign in to follow this  
alexlimlexart

one sqlite database to multiple autoit users

Recommended Posts

alexlimlexart

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
footswitch

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
footswitch

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
jchd

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
footswitch

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
ChrisL

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
alexlimlexart

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
jchd

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
  • Like 1

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
skin27

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
jchd

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
skin27

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
StringingLong

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
jchd

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  

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