Android Question Save and show inf from SQLiteDB

f0raster0

Well-Known Member
Licensed User
Longtime User
Hey guys,
I have saved manually a password "1234560" in a SQLite DB then after run the first time the app should show the picture1 for default but it isn't happening..

When I save other password (for example 1234561) by writing it in EditText2 yes, after reset the app it show me picture2.

1)Then I have added a ListView to check if the password 01234560 is saved and yes it is in then I don't understand why picture1 isn't visible.
2) Also the ListView show me "1234560" correct but many rows below finally show me "1234561" How I can show the list without rows empty
Thanks for any help

The main code:
B4X:
Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'These variables can be accessed from all modules.
  
End Sub

Sub Globals
    'These global variables will be redeclared each time the activity is created.
    'These variables can only be accessed from this module.
    Dim Consulta As SQL
    Dim Res As Cursor
    Dim c As String

    Private Panel1 As Panel  
    Private WebView1 As WebView
    Private Button2 As Button
    Private EditText1 As EditText
  

    Private Panel2 As Panel
    Private Button1 As Button
    Private ImageView1 As ImageView
    Private ImageView2 As ImageView
    Private ImageView3 As ImageView  
    Private ImageView4 As ImageView
    Private ImageView5 As ImageView
    Private ImageView6 As ImageView
  
  
  
    Private Panel3 As Panel
    Private EditText2 As EditText
    Private ListView1 As ListView
    Private Button4 As Button
    Private Button5 As Button
End Sub

Sub Activity_Create(FirstTime As Boolean)
    'Do not forget to load the layout file created with the visual designer. For example:
      Activity.LoadLayout("Layout1")
    Panel2.Visible=True
    Panel3.Visible=False
    ImageView1.Visible=False
    ImageView2.Visible=False
    ImageView3.Visible=False
    ImageView4.Visible=False
    ImageView5.Visible=False
    ImageView6.Visible=False
  
  
  
        'copy bd in:
        Dim ruta As String
        If File.ExternalWritable Then
            ruta = File.DirDefaultExternal 'SD
        Else
            ruta = File.DirInternal 'internal
        End If
      
        'check if the bd exist
        If File.Exists(ruta, "cafes.db") = False Then
                File.Copy(File.DirAssets, "cafes.db", ruta, "cafes.db")
        End If
        'start variable sql
        Consulta.Initialize(ruta, "cafes.db", True)
      
        'look in db if password is saved if yes then show pics
      
        Res=Consulta.ExecQuery("SELECT clave FROM CLAVES")
        For i=0 To Res.RowCount-1

            Res.Position=i
            c=Res.GetString("clave")
            'I have saved the password 01234560 in db before by writing it manually
            If c="12345610" Then
                ImageView1.Visible=True
            End If
            If c="1234561" Then
                ImageView2.Visible=True
            End If          
            If c="1234562" Then
                ImageView3.Visible=True
            End If
            If c="1234563" Then
                ImageView4.Visible=True
            End If
            If c="1234564" Then
                ImageView5.Visible=True
            End If
            If c="1234565" Then
                ImageView6.Visible=True
            End If
        Next
End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub

Sub Button1_Click
    Panel3.Visible=True      
End Sub
Sub Button2_Click
'Panel1
'web
    WebView1.LoadUrl("http://www.google.com/")
    WebView1.Visible=True  
End Sub

Sub Button4_Click
    'Panel3
    'check if password is correct then saved it in db
        If EditText2.Text<>"1234560" AND EditText2.Text<>"1234561" AND EditText2.Text<>"1234562" AND EditText2.Text<>"1234563" AND EditText2.Text<>"1234564" AND EditText2.Text<>"1234565" Then
            Msgbox("Try again","Wrong Password")
            Panel3.Visible=False
        Else
              
            Consulta.ExecNonQuery2("INSERT INTO CLAVES (rowid, clave) VALUES(NULL, ?)", Array As String( EditText2.Text))
            Msgbox("Password","Saved")
            Panel3.Visible=False
        End If
End Sub

Sub Button5_Click
'to check values saved

    Res=Consulta.ExecQuery("SELECT clave FROM CLAVES")  
    If Res.RowCount>0 Then
            For i=0 To Res.RowCount-1
            Res.Position=i          
            ListView1.AddSingleLine(Res.GetString("clave"))
            Next
    End If
End Sub
 

Mahares

Expert
Licensed User
Longtime User
Your database should only be initialized once like this:
B4X:
if firsttime then
   Consulta.Initialize(ruta, "cafes.db", True)
end if

Also,
B4X:
Dim Consulta As SQL  'should be in Process_Globals
 
Upvote 0

f0raster0

Well-Known Member
Licensed User
Longtime User
Thanks I have moved "Consulte" to Globals, but I think
B4X:
Consulta.Initialize(ruta, "cafes.db", True)
must start when run the app because it must decide if show or not show the pictures, the condition is if the password 1234560 was saved then show picture1 (password 1234561-picture2, password 1234563-picture3, etc) after each re-start must look the DB..

I don't know why I can't see picture1. Picture2 to 6 work fine.
 
Upvote 0
Top