Jump to content
topten

Working with two and more sqlite.db at the same time

Recommended Posts

topten

I have several sqlite databases and I am using this function to load a databse

Func _LoadDatabase($Database_File = -1, $Database_Structure = -1, $Table_Name = -1, $_Automatic_Unload = 1)
    _SQLite_Startup()

    _DefaultAnalyser($Database_File, 'DataBase.db3')
    _DefaultAnalyser($Database_Structure, 'id INTEGER PRIMARY KEY,data TEXT,parameters TEXT')
    _DefaultAnalyser($Table_Name, 'Database')
    _DefaultAnalyser($_Automatic_Unload, 1)

    If $_Automatic_Unload Then OnAutoItExitRegister('_UnloadDatabase')
    If Not FileExists($Database_File) Then
        _SQLite_Open($Database_File)
        _SQLite_Exec(-1, "CREATE TABLE " & $Table_Name & " (" & $Database_Structure & ")")
        $_Just_Created = True ;I made This Variable to know that I should Insert The Values or Update Them........
        ;Since we cannot insert the same id twice(will cause error because it is Declared as PRIMARY KEY)
    Else
        _SQLite_Open($Database_File)
    EndIf
    If @error Then Return SetError(1, @error, -1)
    Return 1
EndFunc   ;==>_LoadDatabase

When I load one databse  it works ok, but when I am using the same function to load the second database it becomes unresponsive - is it possible to use several databases simultaneously, something like:

_LoadDatabase (1.db)

_LoadDatabase (2.db)

Edited by topten

Share this post


Link to post
Share on other sites
jchd

You can use as many databases as needed, simultaneously, by using the handle returned by _SQLite_Open and passing it as the first argument in _SQLite_* functions.

You can also attach a database to an already open one: see SQL attach. Which way is best suited to your use case depends on the context and which operations you have to perform.


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
Luigi

More or less...

_SQLite_Startup()


Global $g_hDataBase1 = _SQLite_Open($Database_File_1)
Global $g_hDataBase2 = _SQLite_Open($Database_File_2)

Func _LoadDatabase($Database_File = -1, $Database_Structure = -1, $Table_Name = -1, $_Automatic_Unload = 1)

    _DefaultAnalyser($Database_File, 'DataBase.db3')
    _DefaultAnalyser($Database_Structure, 'id INTEGER PRIMARY KEY,data TEXT,parameters TEXT')
    _DefaultAnalyser($Table_Name, 'Database')
    _DefaultAnalyser($_Automatic_Unload, 1)

    If $_Automatic_Unload Then OnAutoItExitRegister('_UnloadDatabase')
    If Not FileExists($Database_File) Then
        ; do not use -1... use variable's database
        _SQLite_Exec($g_hDataBase1, "CREATE TABLE " & $Table_Name & " (" & $Database_Structure & ")")
        _SQLite_Exec($g_hDataBase2, "CREATE TABLE " & $Table_Name & " (" & $Database_Structure & ")")
        $_Just_Created = True ;I made This Variable to know that I should Insert The Values or Update Them........
        ;Since we cannot insert the same id twice(will cause error because it is Declared as PRIMARY KEY)
    Else
        _SQLite_Open($Database_File)
    EndIf
    If @error Then Return SetError(1, @error, -1)
    Return 1
EndFunc   ;==>_LoadDatabase


Func on_exit()
    _SQLite_Close($g_hDataBase1)
    _SQLite_Close($g_hDataBase2)
EndFunc

 

  • Like 1

Visit my repository

Share this post


Link to post
Share on other sites
topten

Thank you everyone for your great advise, Sending special thanx to Luigi (it is great that you've made an example in the context of the function I use)

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

×