Page 1 of 1

Multi-databases access

Posted: Sat Feb 25, 2023 5:32 pm
by al2791
In the same program how to access an SQLITE database and another in MYSQL (or ODBC or postgreSql)

Re: Multi-databases access

Posted: Sat Feb 25, 2023 5:49 pm
by infratec
Use OpenDatabase() multiple times :wink:

Re: Multi-databases access

Posted: Sun Feb 26, 2023 5:12 pm
by al2791
No, try the code

Execute Only PART 1 -> OK
Execute Only PART 2 -> OK
Execute Both PART 1 and PART 2 -> PART 1 OK but PART 2 NOT OK
Execute Both PART 2 and PART 1 -> PART 2 OK but PART 1 NOT OK

Code: Select all

; -----------------------
;- PART 1
; -----------------------
UseSQLiteDatabase()
Filename$ = "c:\temp\bd.bdd"
If CreateFile(0, Filename$)
  Debug "Fichier de base de données créé"
  CloseFile(0)

  If OpenDatabase(0, Filename$, "", "")
    Debug "Connecté à PureBasic.sqlite"
    If DatabaseUpdate(0, "CREATE TABLE info (test VARCHAR(255));")
      Debug "Table créée"
      CloseDatabase(0)
    EndIf
  EndIf
EndIf

; -----------------------
;- PART 2
; -----------------------
UseMySQLDatabase()
 If OpenDatabase(9, "host=localhost port=3306 dbname=bdd", "root", "password")
   Debug "Connected to MySQL"
   CloseDatabase(9)
Else
  Debug "Connection failed : "+DatabaseError()
EndIf
End

Re: Multi-databases access

Posted: Sun Feb 26, 2023 5:18 pm
by Kiffi
Use the plugin parameter when you open the database:

Code: Select all

OpenDatabase( ... , #PB_Database_SQLite)
...
OpenDatabase( ... , #PB_Database_MySQL)
See: https://www.purebasic.com/documentation ... abase.html

Re: Multi-databases access

Posted: Sun Feb 26, 2023 5:33 pm
by al2791
Thanks a lot, it's ok