Author Topic: Two databases  (Read 265 times)

Fim

  • Guru
  • ***
  • Posts: 131
    • View Profile
    • Fims sajt
Two databases
« on: October 04, 2017, 07:29:34 AM »
Can I have two databases simultaneously on a server, such as "Winova.db3" and "Winova-test.db3" without doing anything special on the server? In my progarm, I open one or other database. I think right?

Fim W.

Bern Ertl

  • Master Geek
  • ****
  • Posts: 466
  • Excellent
    • View Profile
    • InterPlan Systems
Re: Two databases
« Reply #1 on: October 04, 2017, 09:28:50 AM »
As long as you have the hard drive space, you can have as many databases as you want.  The app I'm developing right now uses multiple databases to manage different things (licensing, users/logins, global data, individual project data).

Fim

  • Guru
  • ***
  • Posts: 131
    • View Profile
    • Fims sajt
Re: Two databases
« Reply #2 on: October 04, 2017, 09:47:50 AM »
Thanks.
/Fim W.

cj

  • Master Geek
  • ****
  • Posts: 616
    • View Profile
Re: Two databases simultaneously requires ATTACH
« Reply #3 on: October 04, 2017, 12:24:01 PM »
2 databases cannot be open in the same thread without ATTACH so simultaneously is in question.

Each thread gets its own set of handles as demonstrated here by using multiple threads.
The other methods would be to slClose/slOpen each database or use multiple processes.

#INCLUDE "sqlitening.inc"
GLOBAL gs AS STRING
FUNCTION PBMAIN () AS LONG
 LOCAL hThread AS LONG
 THREAD CREATE Other(0) TO hThread
 LOCAL s() AS STRING
 slOpen "sample.db3","C"
 slselAry "select count(*) from parts",s(),"Q9c"
 addIt FUNCNAME$ + " " + JOIN$(s(),$CR)
 DO:SLEEP 50:LOOP UNTIL THREADCOUNT = 1
 ? gs,,"Both"
 'PBMAIN 10000
 'Other 2
END FUNCTION

SUB AddIt(s AS STRING) THREADSAFE
 gs+=s + $CR
END SUB

THREAD FUNCTION Other(BYVAL x AS LONG)
 LOCAL s() AS STRING
 slOpen "junk.db3","C"
 slexe "create table if not exists t1(c1)
 slexe "insert into t1 values('test')"
 slselAry "select count(*) from t1",s(),"Q9c"
 AddIt FUNCNAME$ + " " + JOIN$(s(),$CR)
END FUNCTION
« Last Edit: October 16, 2017, 08:55:42 AM by cj »

cj

  • Master Geek
  • ****
  • Posts: 616
    • View Profile
Re: Two databases using multiple threads ATTACH or processes
« Reply #4 on: October 04, 2017, 12:36:25 PM »
Note: slOpen automatically closes any previous open database.

#INCLUDE "sqlitening.inc"
GLOBAL gs AS STRING
FUNCTION PBMAIN () AS LONG
 LOCAL hThread AS LONG
 THREAD CREATE Other(0) TO hThread
 LOCAL s() AS STRING
 slOpen "sample.db3","C"
 slselAry "select count(*) from parts",s(),"Q9c"
 addIt FUNCNAME$ + " " + JOIN$(s(),$CR)
 DO:SLEEP 50:LOOP UNTIL THREADCOUNT = 1
 ? gs,,"Both"
 'PBMAIN 10000
 'Other 2

'Switch databases
 slOpen "junk.db3"
 slselAry "select count(*) from t1",s(),"Q9c"
 ? JOIN$(s(),$CR)

 slOpen "sample.db3"
 slselAry "select count(*) from parts",s(),"Q9c"
 ? JOIN$(s(),$CR)  '10000
END FUNCTION

SUB AddIt(s AS STRING) THREADSAFE
 gs+=s + $CR
END SUB

THREAD FUNCTION Other(BYVAL x AS LONG)
 LOCAL s() AS STRING
 slOpen "junk.db3","C"
 slexe "create table if not exists t1(c1)
 slexe "insert into t1 values('test')"
 slselAry "select count(*) from t1",s(),"Q9c"
 AddIt FUNCNAME$ + " " + JOIN$(s(),$CR)
END FUNCTION
                           
« Last Edit: October 16, 2017, 08:56:33 AM by cj »

Fim

  • Guru
  • ***
  • Posts: 131
    • View Profile
    • Fims sajt
Re: Two databases
« Reply #5 on: October 05, 2017, 02:26:49 AM »
I did not intend to have two databases open at the same time. Instead, I had thought to be able to switch between them. But thanks for the tips.
/Fim W.