A question: Insert records from XML to SQLite

marbellapc

Member
Licensed User
Longtime User
Hi all
Here I am again with a new question, is that I have an XML which has a number of fields, the thing is that I do the Parser and shows it well all the items, but when I enter the data only adds last item added.

This is the code I use to parse the XML
B4X:
Sub Parser_EndElement (Uri As String, Name As String, Text As StringBuilder)
' ----------------------------------------------------------- Parseamos el XML en busca de los campos para la DB.
   If parser.Parents.IndexOf("Item") > -1 Then
      If Name          = "XML_Titulo" Then
         XML_Titulo       = Text.ToString
      Else If Name     = "XML_Imagen" Then
         XML_Imagen         = Text.ToString
      Else If Name     = "XML_Localidad" Then
         XML_Localidad    = Text.ToString
      Else If Name     = "XML_Producto" Then
         XML_Producto    = Text.ToString
      Else If Name    = "XML_Fecha" Then
         XML_Fecha       = Text.ToString
      Else If Name     = "XML_Ingredientes" Then
         XML_Ingredientes = Text.ToString
      Else If Name     = "XML_Proceso" Then
         XML_Proceso      = Text.ToString
      Else If Name     = "XML_Favorito" Then
         XML_Favorito     = Text.ToString
      End If
   End If
   If Name = "Item" Then
      ListView_XML.AddSingleLine(XML_Titulo)
   End If   
End Sub

This is the button that calls the activity which shows me ListView_XML
B4X:
Sub Button_XML_Click
' ----------------------------------------------------------- '       
       myZip.ABUnzip(ruta & "update.zip", ruta & folder)     ' Si se descarga el archivo, lo descomprimimos.
   File.Delete(ruta, "update.zip")                            '
' ----------------------------------------------------------- '   
   RemoveViews
      Activity.LoadLayout("XML_List")
' ----------------------------------------------------------- 
      Label_Text.TextColor      = Colors.RGB(255,242,223)
      Label_Text.Color          = Colors.RGB(170,40,102)
' ----------------------------------------------------------- 
      Label_copyright.TextColor = Colors.RGB(255,242,223)
      Label_copyright.Color     = Colors.RGB(170,40,102)
' ----------------------------------------------------------- 
      Panel1.Color              = Colors.RGB(255,242,223)
      parser.Initialize
      ListView_XML.Initialize("ListView_XML")
' ----------------------------------------------------------- 
      Activity.AddView(ListView_XML, 10, 30, 300, 360)
      ListView_XML.SingleLineLayout.ItemHeight      = 40dip
      ListView_XML.SingleLineLayout.Label.Left      = 0
      ListView_XML.SingleLineLayout.Label.Width     = 100%x
      ListView_XML.SingleLineLayout.Label.Height    = 40dip
      ListView_XML.SingleLineLayout.Label.Gravity   = Gravity.CENTER_VERTICAL
      ListView_XML.SingleLineLayout.Label.Color     = Colors.RGB(255,242,223)
      ListView_XML.SingleLineLayout.Label.TextSize  = 12
        ListView_XML.SingleLineLayout.Label.TextColor = Colors.RGB(170,40,102)
      ListView_XML.FastScrollEnabled                = True
' ----------------------------------------------------------- Configuramos la opcion para cambiar el color de las lineas del ListView.
   SetDivider(ListView_XML, Colors.RGB(170,40,102), 1dip)
' ----------------------------------------------------------- 
      ListView_XML.SingleLineLayout.ItemHeight = 40dip
   Dim In As InputStream
      In = File.OpenInput(ruta, folder & "update.xml")
      parser.Parse(In, "Parser")
   In.Close
End Sub

And this is the code I added to that when you click on one of the rows I add fields to the Item to SQLite.
B4X:
Sub ListView_XML_ItemClick (Position As Int, Value As Object)
' ----------------------------------------------------------- Insertar registros en DB.
      SQL1.Initialize(DBFileDir & "/" & folder, DBFileName, True)
' ----------------------------------------------------------- 
   Dim Arr(8) As String 
' -----------------------------------------------------------
      Arr = Array As String(XML_Titulo, XML_Imagen, XML_Localidad, XML_Producto, XML_Fecha, XML_Ingredientes, XML_Proceso, XML_Favorito)
   If XML_Titulo = SQL1.ExecQuerySingleResult("SELECT titulo FROM Recetas WHERE Titulo = '" & XML_Titulo & "'") Then
      n = "- Datos Existentes."& CRLF
   Msgbox2(n, "A T E N C I Ó N", "", "Aceptar", "", bmp_icon)
   Else
      SQL1.ExecNonQuery2("INSERT INTO Recetas VALUES (?, ?, ?, ?, ?, ?, ?, ?)", Arr)
      n = "- Actualización realizada." & CRLF
   Msgbox2(n, "A T E N C I Ó N", "", "Aceptar", "", bmp_icon)
   End If
' ----------------------------------------------------------- Eliminamos el archivo XML, despues de haber hecho el INSERT INTO en la DB.
'   File.Delete(File.DirRootExternal & "/" & folder, "update.xml")
      SQL1.Close
End Sub
The problem is that if for example are 3 Items, I add the Item number 3, the other 2 will not let me add.

Sorry to be so heavy, and it's probably silly but I've been mulling over a couple of days without finding any solution.

Greetings and thank you very much in advance.
 

marbellapc

Member
Licensed User
Longtime User
Ouuu!...
Erel thank you very much, and I had not realized that I needed to put "XML_Titulo = Value"

Now everything works perfectly.

Again thank you very much.

Regards
 
Upvote 0

marbellapc

Member
Licensed User
Longtime User
Hello again, sorry to ask again ...

Turns out what I thought it was working properly, the only thing is that it is introducing the title, the other fields I added the last item of the XML.

Add as Erel said the Value, but until now I had not realized that only adds the title selected, the remaining fields of the last record catches of XML.
B4X:
Sub ListView_XML_ItemClick (Position As Int, Value As Object)
' ----------------------------------------------------------- Insertar registros en DB.
      SQL1.Initialize(DBFileDir & "/" & folder, DBFileName, True)
   XML_Titulo = Value
' ----------------------------------------------------------- 
   Dim Arr(8) As String 
' -----------------------------------------------------------
      Arr = Array As String(XML_Titulo, XML_Imagen, XML_Localidad, XML_Producto, XML_Fecha, XML_Ingredientes, XML_Proceso, XML_Favorito)
   If XML_Titulo = SQL1.ExecQuerySingleResult("SELECT titulo FROM Recetas WHERE Titulo = '" & XML_Titulo & "'") Then
      n = "- Datos Existentes."& CRLF
   Msgbox2(n, "A T E N C I Ó N", "", "Aceptar", "", bmp_icon)
   Else
      SQL1.ExecNonQuery2("INSERT INTO Recetas VALUES (?, ?, ?, ?, ?, ?, ?, ?)", Arr)
      n = "- Actualización realizada." & CRLF
   Msgbox2(n, "A T E N C I Ó N", "", "Aceptar", "", bmp_icon)
   End If
' ----------------------------------------------------------- Eliminamos el archivo XML, despues de haber hecho el INSERT INTO en la DB.
'   File.Delete(File.DirRootExternal & "/" & folder, xml_update)
      SQL1.Close
End Sub
My question is, what is the reason that I add a field just right?

Again thank you very much

Regards
 
Upvote 0

marbellapc

Member
Licensed User
Longtime User
Ok, now I understand

Actually I find it difficult to understand some things, but thanks to people like you we are made the way easier.

Erel Greetings and thank you very much for the patience you have with us.
 
Upvote 0
Top