Android Tutorial DBUtils - Android databases are now simple!

Erel

B4X founder
Staff member
Licensed User
Longtime User
Erel, what is the reason to lowercase the database field names in ExecuteMap?
This is a way to normalize the keys. Otherwise the case depends on the case returned from the database. I think that it is easier when you know for sure that it is always lower-case. You can of course remove the call to ToLowerCase.

About the second question. You should check if the key exists in the Map before fetching it.
 

junaidahmed

Well-Known Member
Licensed User
Longtime User
ExecuteMemory Table is fine to get cell value from memory table.if i load sql server data via asp script (Webview.Loadurl("http://202.89.55.78/Myweb/Data.asp") ) then how can I get cell value.pls advise ASAP
 

peacemaker

Expert
Licensed User
Longtime User
Seems
Sub ExecuteMemoryTable(SQL As SQL, Query As String, StringArgs() As String, Limit As Int) As List
....
Table1.Add(values)
...

should be edited to:

Sub ExecuteMemoryTable(SQL As SQL, Query As String, StringArgs() As String, Limit As Int) As List
....
Table1.AddAll(values)
...
 

kaputo

Member
Licensed User
Longtime User
Hi, I am trying to populate a ListView (DBUtils.ExecuteListView) with two fields from sqllite, of which one of them is a bitmap. Is this possible (when I try there is no error but the bitmap is not displayed).
Is there an alternate way to populate the ListView from the db to include the bitmap (e.g. lv1.add2linesandbitmap)
Thanks
 

Phantomco

Member
Licensed User
Longtime User
Little Mod to prevent constraint errors on insertmaps

Here's a little mod to DButils that allows insertmaps to update existing records, or add just new ones.

Setting constraint to 1 will replace existing records with the new one, 2 will ignore existing records but not throw an error, and 3 will fail but will keep any records added prior to the error.

These are unique to SQLite working with unique key records.

B4X:
Sub InsertMaps(SQL As SQL, TableName As String, ListOfMaps As List, Constraint As Int)
   Dim sb, columns, values As StringBuilder
   'Small check for a common error where the same map is used in a loop
   If ListOfMaps.Size > 1 AND ListOfMaps.Get(0) = ListOfMaps.Get(1) Then
      Log("Same Map found twice in list. Each item in the list should include a different map object.")
      ToastMessageShow("Same Map found twice in list. Each item in the list should include a different map object.", True)
      Return
   End If
   Dim Sqlqual As String
   Select Case Constraint
      Case 0
         Sqlqual = "INSERT INTO "
      Case 1
         Sqlqual = "INSERT or REPLACE INTO "
      Case 2
         Sqlqual = "INSERT or IGNORE INTO "
      Case 3
         Sqlqual = "INSERT or FAIL INTO "
   End Select
   SQL.BeginTransaction
   Try
'      Msgbox(ListOfMaps.Size,"Inspections")
      For i1 = 0 To ListOfMaps.Size - 1
         
         sb.Initialize
         columns.Initialize
         values.Initialize
         Dim listOfValues As List
         listOfValues.Initialize
         sb.Append( Sqlqual & "[" & TableName & "] (")
         Dim m As Map
         m = ListOfMaps.Get(i1)
         For i2 = 0 To m.Size - 1
            Dim col As String
            Dim value As Object   
            col = m.GetKeyAt(i2)
            value = m.GetValueAt(i2)
            If i2 > 0 Then
               columns.Append(", ")
               values.Append(", ")
            End If
            columns.Append("[").Append(col).Append("]")
            values.Append("?")
            listOfValues.Add(value)
         Next
         sb.Append(columns.ToString).Append(") VALUES (").Append(values.ToString).Append(")")
         If i1 = 0 Then Log("InsertMaps (first query out of " & ListOfMaps.Size & "): " & sb.ToString)
         SQL.ExecNonQuery2(sb.ToString, listOfValues)
      Next
      SQL.TransactionSuccessful
   Catch
      ToastMessageShow(LastException.Message, True)
      Log(LastException)
   End Try
   SQL.EndTransaction
End Sub
 

Ennesima77

Member
Licensed User
Longtime User
I added my personale modify to ExecuteListView

B4X:
Sub ExecuteListViewWithBlob(SQL As SQL, Query As String, StringArgs() As String, Limit As Int,  _
   ListView1 As ListView, BlobCol As String, ValueCol As String)
   
ListView1.Clear
   Dim Table As Cursor, bmpMom As Bitmap 
   Table = SQL.ExecQuery(Query)
   
   For i = 0 To Table.RowCount - 1
      Table.Position=i
      If Table.GetBlob( BlobCol) = Null Then
         bmpMom.InitializeMutable (100,100)
      Else
          Dim Buffer() As Byte 
          Buffer = Table.GetBlob( BlobCol)
         Dim InputStream1 As InputStream
          InputStream1.InitializeFromBytesArray(Buffer, 0, Buffer.Length)
          
          bmpMom.Initialize2(InputStream1)
       End If
       
      If ValueCol = Null Then
         ListView1.AddTwoLinesAndBitmap(Table.GetString2( 0), Table.GetString2(1), bmpMom )
      Else
         ListView1.AddTwoLinesAndBitmap2(Table.GetString2( 0), Table.GetString2(1), bmpMom, Table.GetString(ValueCol) )
      End If
      
   Next
   
   
End Sub

It's possible to use it to fill a ListView with an Image, if DB contain a BLOB column.
There's a control when BLOB is null.

It's also possible set what column containe value to return in click event.

Rimember the first 2 declared column must be the First and Second line in ListView like in follow code:

B4X:
Select FirstName, SecondName, BlobCol, IDCol from persone.........

In ListView first line will be FirstName and the second one will be SecondName.


I hope this can be usefull.
 
Last edited:

gapi

Active Member
Licensed User
Longtime User
I've three spinner .... three subs for populate it ... first ok ... second spinner is blank !!

B4X:
' SUB FOR POPULATE THE SPINNER (x3)
Sub popolaSpinnerDestinazioneMerci()
Dim query As String
   query = "SELECT destinazione_merci.col5 " & _
   "FROM destinazione_merci INNER JOIN clienti " & _
   "ON destinazione_merci.col1= clienti.col2 " & _
   "WHERE clienti.col2=" & QUOTE & showOneCliente.cliente_codice & QUOTE
   Log("query_DestM=" & query)
   DBUtils.ExecuteSpinner(Main.SQL1, query, Null, 0, spinDestMerci)
End Sub
... why, If I ask some query with
B4X:
'
Dim Cursor as Cursor
Cursor1 = Main.SQL1.ExecQuery(query)
Cursor1.position=0
Msgbox(Cursor1.GetString("col25")," query result")

it return me right value ... in spinner not (blank)

thanks :BangHead:
 

AscySoft

Active Member
Licensed User
Longtime User
the log return me correct value of record, what I must debug ?

Use a brake point on the first line of the sub that don't work, then, instead of build application with release/obfuscate, use debug from menu,... the code stops at the (red line) brake point, and you could advance line by line and see the variables values with F8
 

klaus

Expert
Licensed User
Longtime User
You should post your project as a zip file (IDE menu Files / Export As Zip), or at least a smaller one that shows the problem.
Without seeing more code it's impossible to give you a concrete answer.
With the project we could test it to see what happens

Best regards.
 
Cookies are required to use this site. You must accept them to continue using the site. Learn more…