Android Tutorial DBUtils - Android databases are now simple!

LucaMs

Expert
Licensed User
Longtime User


11 months ago I changed "DBUtils" for my purposes.

I have:

for single field:
Sub UpdateRecordField(SQL As SQL, TableName As String, Field As String, NewValue As Object, _
WhereFieldEquals As Map) 'ignore

for more fields:
Sub UpdateRecord(SQL As SQL, TableName As String, Fields As Map, WhereFieldEquals As Map) 'ignore

But I made the mistake of not correctly updating the "header" of the module


Now I can see:
' - Added UpdateRecord2 updates more than one field in a record,
in the module attached here.


I'm sorry
 

Tom1s

Member
Licensed User
Longtime User
I have a table in flexible table and when clicking the row I would like to move it to another table.
How should I do it the easiest way? I can have row number when clicking. Below is not working yet

B4X:
Dim Rowmap As Map
    Rowmap.Initialize
    Rowmap=DBUtils.Executemap(SQL1,"Select Timestamp,Car from DBTableName1 WHERE ID= row",Null)

DBUtils.InsertMaps(SQL1,"Loaded",Rowmap) 'and put in new data

Thanks
 

klaus

Expert
Licensed User
Longtime User
This looks wrong to me:
Rowmap=DBUtils.Executemap(SQL1,"Select Timestamp,Car from DBTableName1 WHERE ID= row",Null)
I suppose that DBTableName1 and row are variables.
Try tis code:
Rowmap=DBUtils.Executemap(SQL1,"Select Timestamp,Car from " & DBTableName1 & "WHERE ID= " & row,Null)
 

Tom1s

Member
Licensed User
Longtime User
Thanks.

Now I have an another problem. Table1 contains: ID INTEGER AUTO_INCREMENT PRIMARY KEY , PID bigint, Timeloaded DATETIME, Name TEXT)
TAble2 contains ID INTEGER AUTO_INCREMENT PRIMARY KEY , PID bigint, Timeloaded DATETIME, Name TEXT, col1, col2)

I try to read the table1 and then put extra info(fill col1 and col2) and then save it to table2. I know how to make it in SQL but now with dbutils I don't know.
Should the map be the same order than columns or is it enough if there is a col1,value ?
I got an error row77 all the time.

Is there a simple way of doing this or should I make a new map case by case with right order? What should i put in that ID because it is auto increment.

Thanks for your help.

B4X:
Sub Table1_celllongClick (Col As Int, Row As Int)
    Activity.Title = Table1.GetValue(Col, Row)
    SelectedRow = Row
    Dim row2 As String = Row+1
    Dim unloadresult As String
    unloadresult = Msgbox2("Unload Row"& row2,"Unloading..","UNLOAD","CANCEL","",Null)
    If unloadresult = DialogResponse.POSITIVE Then
    Dim Rowmap As Map
    Rowmap.Initialize
    Log(Row)
'    Rowmap=DBUtils.Executemap(SQL1,"Select PID,Timeloaded,Name FROM Loaded WHERE ID = "&row2, Null)
    Rowmap=DBUtils.Executemap(SQL1,"Select * FROM table1 WHERE ID = "&row2, Null)
    Log(Rowmap)
    If Rowmap = Null Then
       ToastMessageShow("Error Copying the Row" ,True)
    Else

    Rowmap.Put("col1","32255")
    Rowmap.Put("col2","545")
  '  Rowmap.put("col3","5454")

    Log(Rowmap)
    DBUtils.InsertMaps(SQL1,"table2",Rowmap) 'and put in new data
 

klaus

Expert
Licensed User
Longtime User
DBUtils.InsertMaps(SQL1,"table2",Rowmap)
Expects a List of Maps !
So you need to define a List and add the Map to it and then call DBUtils.InsertMaps with the List as the second parameter.
I know how to make it in SQL but now with dbutils I don't know.
You could also do it with SQL even though you use DBUtils.
 

Tom1s

Member
Licensed User
Longtime User
Ok....i ment sql like mssql and tried sql commands but dbutils are much simpler even if it takes me some time to learn it.
 

anaylor01

Well-Known Member
Licensed User
Longtime User
If I uncomment the delete statement it works. But if I leave it commented then I get this error.
An error has occurred in sub:
main_activity_create(B4A line: 76)
Sql1.initialize(dbfiledir,dbfilename,true)
android.database.sqlite.
sqliteCantOpenDatabaseException: unable to open database file

Line 76 is this:
SQL1.Initialize(dbfiledir, dbfilename, True)

Here is all the code:
Sub Activity_Create(FirstTime As Boolean)
'Do not forget to load the layout file created with the visual designer. For example:
Dim dbfilename As String
Dim dbfiledir As String
dbfilename = "ThinkFast.db"
If FirstTime Then
' File.Delete(File.DirDefaultExternal, dbfilename) ' for testing
If File.Exists(File.DirDefaultExternal, dbfilename) = False Then
dbfiledir = DBUtils.CopyDBFromAssets(dbfilename)
End If

SQL1.Initialize(dbfiledir, dbfilename, True)
End If

Any help would be greatly appreciated. Thanks.
 

LucaMs

Expert
Licensed User
Longtime User
If your db exists, dbfiledir will be a null string, then you try to initialize SQL1 passing "" and "ThinkFast.db".
Put Log("dbfiledir = " & dbfiledir) before SQL1.Initialize(dbfiledir, dbfilename, True)
 

anaylor01

Well-Known Member
Licensed User
Longtime User
This might be the wrong way of doing it but I am thinking the first time I store the value in a database and then if it exists then pull that value out of the database. What do you think?
 

Computersmith64

Well-Known Member
Licensed User
Longtime User
Ok. That won't work. So how would I do it?
In your File.Exists statement you are assuming that if the DB is there is will be in File.DirDefaultExternal, so just initialize your dbfiledir with File.DirDefaultExternal:

B4X:
Private dbfiledir as String = File.DirDefaultExternal

That way, if the File.Exists is true you won't have a Null string in your dbfiledir variable & if it doesn't exist, dbfiledir will be changed by DBUtils.CopyDBFromAssets().

- Colin.
 
Last edited:
Cookies are required to use this site. You must accept them to continue using the site. Learn more…