Android Question sqlite up to date

fernando gibert

Member
Licensed User
How must an sqlitedb be replaced on the device with the actual one used in the pc ?

Is there a way to drop database in dirinternal and then

copy from assets to dirinternal ?

Thanks alot !
 

Myr0n

Active Member
Licensed User
Hi,
Are you asking how to copy a sqlite db from your assets to a writable location?
If so, you can use dbutils that you can check here
Or you can use
B4X:
File.Copy(File.DirAssets, SQLiteDbNameInAssets, TargetDir, SQLiteDbNameToYourDevice)
I recommend you to take a look inside of the Dbutils code class.
 
Upvote 0

Myr0n

Active Member
Licensed User
Remember to take in consideration the runtime permissions.
 
Upvote 0

fernando gibert

Member
Licensed User
My question refers to following schema:

* Creating/testing a new Project(apk), when the execution does a wrong action , the database in the device becomes changed.

* I want to replace this corrupted db with the original one in the pc , correct the pgm failures and retry testing until finallu works.

* Sometimes wish to make changes in the pc database and want this changes reflect into the device db.

* Once the pgm is finished and works properly the device db shall be the used one.

Thanks
 
Upvote 0

klaus

Expert
Licensed User
I use this code:
B4X:
'    File.Delete(SQLDataBasePath, SQLDateBaseName) ' only for testing, removes the database
       
    'check if the database already exists
    If File.Exists(SQLDataBasePath, SQLDateBaseName) = False Then
        'copy the default DB
        File.Copy(File.DirAssets, SQLDateBaseName, SQLDataBasePath, SQLDateBaseName)
    End If
    SQL1.Initialize(SQLDataBasePath, SQLDateBaseName, True)

When I want to update the database, I uncomment this line:
File.Delete(SQLDataBasePath, SQLDateBaseName) ' only for testing, removes the database
 
Upvote 0

Myr0n

Active Member
Licensed User
This is what I use :
B4X:
'Copies a database file that was added in the Files tab. The database must be copied to a writable location.
'This method copies the database to the storage card. If the storage card is not available the file is copied to the internal folder.
'The target folder is returned.
'Filename = DB File to copy from the Assets
'OverwriteDB =  False = If the database file already exists then no copying is done.
'                True  = If the database file already exists then will be overwrite
Sub CopyDBFromAssets2 (FileName As String, OverwriteDB As Boolean) As String
    Dim TargetDir As String = GetDBFolder
    If (File.Exists(TargetDir, FileName) = False) Or (OverwriteDB = True) Then
        File.Copy(File.DirAssets, FileName, TargetDir, FileName)
    End If
    Return TargetDir
End Sub
 
Upvote 0

fernando gibert

Member
Licensed User
* ************************+
File.Delete(File.DirInternal, "mysio.db") ' only for testing, removes the database
If File.Exists(File.dirinternal, "mysio.db") = True Then
Msgbox("exists","Ok")
End If
'check if the database already exists
If File.Exists(File.dirinternal, "mysio.db") = False Then
'copy the default DB
Msgbox("No exists","Ok")
File.Copy(File.DirAssets, "mysio.db" , File.dirinternal, "mysio.db")
End If
* ********************************

This Works and shows msgs as wished.

----------------------------------------------

But the very solution is :
----- delete files in the IDE
----- synchronize files ::: if you debug here the msg No exists is shown
----- add new files
----- synchronize them. ::: here msg exists shows

debug

This means the above system depends on " dir assets " , so , what's the way asset files be updated ?

I mean assets are updated & refreshed only with deleting and adding files.

Thanks again
 
Upvote 0

klaus

Expert
Licensed User
If you change the database file in the Files folder of the project, which is the File.DirAssets folder, with the same name it works without changing anything in the IDE.
 
Upvote 0

fernando gibert

Member
Licensed User
Klaus ,

If you add registers ( for instance ) in your pc database, when testing again , this new records are not reflected in the IDE nor on device….

spinner or list do not "read" them.

unless you delete and add the db in files .
 
Upvote 0

klaus

Expert
Licensed User
Well,
Before posting my previous answer I tested it.
I have a database with three columns called persons.db.
Then, I replaced it by another file, with the same file name, with four columns and all four columns are displayed when I run the program.
So, I don't know what you have done?
 
Upvote 0

fernando gibert

Member
Licensed User
I've tested several times

Added in the pc SQLite db some récords.

Open the Project .

Debug to device using databridge.

Executing: the new récords are not reflected in the device spinner/list.

b4a 8.0

Samsung tablet

just working when i do what's explained before.
 
Upvote 0

fernando gibert

Member
Licensed User
The pc one in c:\mythings\
using sqlanzeiger ( it does the same with SQLite expert)

once i send thru abd push "c:\mythings\mysio.db" sdcard ….. ( some tests before , now i don't use this ) ….
even with Windows f. explorer ...
 
Upvote 0

klaus

Expert
Licensed User
Now, I understand.
I was speaking about the Files folder of the project, which is the File.DirAssets folder!
As your file is not in the programs Files folder, you need of course to either copy it there or update it in the IDE.
 
Upvote 0

fernando gibert

Member
Licensed User
I arrived to this solution !

My wish depends of following:

When you made a working apk and want to develop another one over the same db …

when testing/developing this new apk , the db is changed , so adding this apk to a device it becomes a bit annoying….

Is there something that can permit use of db in "the state of arts" i.e. use the db whichever its updated content is ?

This means that if the db structure is the same data quantity is irrevelant.
 
Upvote 0
Top