Android Question Local Dir SQLite DB

demonio_jack

Member
Licensed User
Hi everyone:
I am using DB Browser (SQLite) to design a local database on my android device. I am testing the application with a Huawei device, during the tests, I connect to the local database, but I cannot return or do any operation with the tables (insert, select, etc). It doesn't throw any errors at me and it's as if the tables are empty.
Where can I find the database on the mobile device?
How can I validate that the database I designed on the computer is the same as the mobile? Must I copy or create the DB every time I test the application?

Thank you

Demonio
 

Bladimir Silva Toro

Active Member
Licensed User
Longtime User
Hi @demonio_jack

You can copy the SQLite database from internal memory to external memory

B4X:
File.Copy(File.DirAssets,"Datos.s3db",File.DirRootExternal,"Datos.s3db")

Remember that to use external memory you must give permissions on the manifest file

I attach a basic example is very easy to use SQLite
 

Attachments

  • Proyecto SQLite.zip
    37.2 KB · Views: 257
Upvote 0

KMatle

Expert
Licensed User
Longtime User
Upvote 0

MicroDrie

Well-Known Member
Licensed User
@demonio_jack it is important how you open the database file. Simple opening of the database is single user mode. For multi user access, your program should open the database in the so-called WAL mode. Mutations are temporary saved to a journal file. When you open in WAL mode the database browser will detect the wall mode and share the database with your program after your have saved the additional mutations to the database file. Be aware that saving the database not always means that the database is physically is saved to the file. The OS can decide to wait till a certain amount of data is saved to the journal file before physical saving the data to the database file. And also don't forget to close your database after a mutation. Not very common, but you can also let your program create the database bottom up, test with the database browser and use the tested queries in your program. Then you don have to copy your database with your program. Then there is only one database so you can use the wrong one!
 
Upvote 0

demonio_jack

Member
Licensed User
Hi @demonio_jack

You can copy the SQLite database from internal memory to external memory

B4X:
File.Copy(File.DirAssets,"Datos.s3db",File.DirRootExternal,"Datos.s3db")

Remember that to use external memory you must give permissions on the manifest file

I attach a basic example is very easy to use SQLite
Thank you so much Bladimir, this is was exactly what I was looking for. :)
 
Upvote 0

demonio_jack

Member
Licensed User
FileDirAssets is write protected. You should see an error message. I always use FileDirInternal (so it's not accessable from the outside, except of rooted devices and you don't need permissions).
Thank you very much KMatle. I did not know how these processes work. Now I have much clearer this type of incident
 
Upvote 0

demonio_jack

Member
Licensed User
@demonio_jack it is important how you open the database file. Simple opening of the database is single user mode. For multi user access, your program should open the database in the so-called WAL mode. Mutations are temporary saved to a journal file. When you open in WAL mode the database browser will detect the wall mode and share the database with your program after your have saved the additional mutations to the database file. Be aware that saving the database not always means that the database is physically is saved to the file. The OS can decide to wait till a certain amount of data is saved to the journal file before physical saving the data to the database file. And also don't forget to close your database after a mutation. Not very common, but you can also let your program create the database bottom up, test with the database browser and use the tested queries in your program. Then you don have to copy your database with your program. Then there is only one database so you can use the wrong one!

I will have to study this subject more calmly since I did not know the way in which local databases are opened. Anyway, as it is a first example that I am doing, I will investigate what you mention to me since it will surely avoid many problems in the future. Thank you very much for the contribution.

Demonio
 
Upvote 0
Top