Android Question SQ Lite Database Multi-Access

Devendra

Member
Licensed User
Longtime User
I have a requirement in my application that one SQ-Lite Database to be shared with 2 Android application running @ the same time, Both the applications will be accessing the SQ-Lite file same time. Is it possible with Android OS and also whether SQ-Lite support this scenario. It is like in Windows Access Database can be shared with 2 Applications @ the same time. Is it the Same Scenario Here. Pls Advice.
 

KMatle

Expert
Licensed User
Longtime User
Where is the db located? On one device and another needs data from it? How do you connect the devices?

You could have one device as a server and other devices connect to it (via TCP/IP socket) and get the data from *one* app running on it (like a db server).
 
Upvote 0

Devendra

Member
Licensed User
Longtime User
The database in in the same device difference is 2 different applications are accessing the same Database there is no TCP/IP involvement.
 
Upvote 0

Devendra

Member
Licensed User
Longtime User
Even the Both Applications in same device, All i want is when one application is using the database, another service app in the same device is sharing the same database there is no Bluetooth or any external device involvement, The Database is in the External Folder
 
Upvote 0

keirS

Well-Known Member
Licensed User
Longtime User
B4A doesn't support it. The main issue is that SQLite does not support multiple writes to the DB at the same time. You can read at the same time but not write. B4A only supports transactions in exclusive mode which means you cannot read from the DB at the same time as it is being written to. SQLite has another transaction mode which is non exclusive and allows reads and writes at the same time but the SQL library doesn't implement this.

One way round this is to do all the DB code in a service in one of the apps and use intents in the other app to communicate with the service. That way you only have one app doing all the reads and updates to the database.
 
Upvote 0

udg

Expert
Licensed User
Longtime User
One way round this is to do all the DB code in a service in one of the apps and use intents in the other app to communicate with the service. That way you only have one app doing all the reads and updates to the database.

I would have suggested that same solution. A more general one could be that of set all the DB-related functions in a sticky service belonging to a no-UI app so to have any number of apps using intents to send/receive data from that first "service-only" app (that in a way plays the role of a dedicated DBMS).

udg
 
Upvote 0

sorex

Expert
Licensed User
Longtime User
I guess it might work when you open and close the database after every query.

As it are apps and not service the chance that a write happends at the same time is none existing I guess since you need to switch apps extremely fast to make that possible.

You only need to store it at a location both apps can read.
 
Upvote 0

sorex

Expert
Licensed User
Longtime User
ok, just read it's an app and service. I guess the open/send query/close method would work here aswell.

there's a minimal chance that both might write at the same time but then can be solved by writing a file to the same location that has the app/service name in it.

so it becomes

B4X:
if file.exists(lockfile)=false
createLockfile
opendb
send query
closedb
removeLockfile
else
queueQueryForLaterRetry
end if
 
Last edited:
Upvote 0
Top