Need help on SQL Initialize and filestorage

LA3QMA

Member
Licensed User
Longtime User
I'm new to b4a and need a kick in my .....

I have a MySQL database that i have converted to SQLite.

From the IDE i have included the file "db2011"

B4X:
Msgbox(File.ExternalWritable, "")
Msgbox( File.Exists(File.DirAssets,"db2011"),"")

I get TRUE on both and so far so good...

When trying:

B4X:
SQL1.Initialize(File.DirDefaultExternal, "db2011", False)

This is not working.

But if i copy the file from "Assets" to DirDefaultExternal:

B4X:
File.Copy(File.DirAssets,"db2011",File.DirDefaultExternal,"db2011")

then i can use the SQLite and search etc.

I understand that Assets is readonly? this is ok as i'm not going to change anything in the database and this is probably why it's not possible to open the database from the Assets?

So is the procedure to copy the file from the Assets to either External or Internal? If so then i have use twice the space for my database?

Anyone that can guide me into the right track?
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
The assets folder is a virtual folder. It is actually a reference to a folder inside the APK file. As the APK file is compressed, only sequential reading is possible. Therefore it is not possible to open a database there.

I recommend you to check DBUtils: Basic4android Search: DBUtils
It has a method that checks if the DB exists and copy it otherwise. It has many other useful methods.
 
Upvote 0

margret

Well-Known Member
Licensed User
Longtime User
DataBase SQL

I'm new to b4a and need a kick in my .....



I understand that Assets is readonly? this is ok as i'm not going to change anything in the database and this is probably why it's not possible to open the database from the Assets?

So is the procedure to copy the file from the Assets to either External or Internal? If so then i have use twice the space for my database?

Anyone that can guide me into the right track?

There is a known issue in Android and a limit I think of 1meg file size with the SQL database in the assets directory. Yes, you do need to copy the file to Internal or External and that is the correct procedure. I think most would suggest to have just the empty structure of the database in the assets directory and then copy it over. I understand that if it's a database with lookup reference data, not user data that will not work. In that case I have seen it suggested to not include the database but to download it to the device instead. Hope this helps.

Thanks,

Margret
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
The database is not limited to 1mb. The issue is that Android is unable to unpack compressed files larger than 1mb (stored inside the APK file). The workaround for this issue is to change the database extension to jpg. Then the Android packager doesn't compress the file and it can be larger than 1mb.
 
Upvote 0

LA3QMA

Member
Licensed User
Longtime User
Internal/External/Assets

Ok tnx for fast reply.

I don't want to use so many libraries if i don't have too.

But if the Assets is "virtual" then i have to make some downloadfunction.

Do i really need to include the tables in the Assets? Can't i just download the SQLite file and open it from the Internal/External storage using Initialize?
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
Do i really need to include the tables in the Assets?
No.
Can't i just download the SQLite file and open it from the Internal/External storage using Initialize?
Yes, you can.

I don't want to use so many libraries if i don't have too.
DBUtils is a code module. It is not a library (though I wouldn't worry too much to add a library if it is useful).
For downloading files I recommend you to check HttpUtils which is a code module + service.
 
Upvote 0

LA3QMA

Member
Licensed User
Longtime User
Assets

It's probably in the manual but here we go...

The "Assets" are virtual storage and are included in the apk-file.
So when i download an application what are happening then?

Are the apk file extracted and installed to the device and then "deleted"?

Just want to know how the system works.
Regarding my previous questions on copying the file from Assets to Internal/External are the file now using twice the amount of storage?

And one "off topic" question:
When i'm including a library is all the commands included in the "exe" file or is the "compiler" using only whats needed from my program?
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
Are the apk file extracted and installed to the device and then "deleted"?
If I remember correctly the apk file is not extracted. The OS maps the file as a virtual memory file.

When i'm including a library is all the commands included in the "exe" file or is the "compiler" using only whats needed from my program?
The complete library is included in your project. However it will not have any effect on the program speed.
 
Upvote 0
Top