Android Question Search View From database(sqlite)

apiwatcomscith

Member
Licensed User
Longtime User
hello, guys
I have a problem, I want a use Search View from sqlite database. how to do it and i am calling file.db complete but can't Query SQL for search, I very try but not work !!

help me please .

Thank you and sorry I'm speak English not good. :(

this my code some section.

B4X:
Sub Activity_Create(FirstTime As Boolean)
    sv.Initialize(Me, "sv")
    sv.AddToParent(Activity, 0, 0, 200dip, 300dip)
    Dim cursor1 As Cursor
   
    If FirstTime Then
        'sql1.Initialize(File.DirDefaultExternal,".db",True)
        If File.Exists(File.DirInternal, "lexitron.db") = False Then
            'copy the default DB
            File.Copy(File.DirAssets, "lexitron.db", File.DirInternal, "lexitron.db")
            'if not, initialize it
            sql1.Initialize(File.DirInternal, "lexitron.db", True)
            'and create it
            'CreateDataBase
            'copy the default DB
            File.Copy(File.DirAssets, "lexitron.db", File.DirInternal, "lexitron.db")
        Else
            'if yes, initialize it
            sql1.Initialize(File.DirInternal, "lexitron.db", True)
        End If
   
   
       
        Dim cities As List
       
        cursor1 = sql1.ExecQuery("SELECT esearch FROM eng2thai") 
        'cities = File.ReadList(File.DirAssets, "1.csv")
'        cities = File.ReadList(File.DirAssets, "Cities.txt")
        'As an optimization we store the index as a process global variable
        'and only build it once.
        cities.Add(cursor1.GetString("esearch"))
       
        index = sv.SetItems(cities)
       
        cursor1.Close
       
    Else
        sv.SetIndex(index)
    End If
End Sub
 

DonManfred

Expert
Licensed User
Longtime User
Small code which should help understanding on how to iterate through the cursor. Your code gets only the first entry and then closes the cursor...
B4X:
Dim Row,RowNumber As Int
If Cursor1.RowCount > 0 Then                        'check if entries exist
    RowNumber = Cursor1.RowCount                    'set the row count variable
For Row = 0 To  RowNumber - 1
  Cursor1.Position = Row                        'set the Cursor to each row
  cities.Add(cursor1.GetString("esearch"))
Next
    Else
        ToastMessageShow("No items found", False)
    End If
 
Upvote 0

apiwatcomscith

Member
Licensed User
Longtime User
Small code which should help understanding on how to iterate through the cursor. Your code gets only the first entry and then closes the cursor...
B4X:
Dim Row,RowNumber As Int
If Cursor1.RowCount > 0 Then                        'check if entries exist
    RowNumber = Cursor1.RowCount                    'set the row count variable
For Row = 0 To  RowNumber - 1
  Cursor1.Position = Row                        'set the Cursor to each row
  cities.Add(cursor1.GetString("esearch"))
Next
    Else
        ToastMessageShow("No items found", False)
    End If

Thank you so much
 
Upvote 0

apiwatcomscith

Member
Licensed User
Longtime User
Small code which should help understanding on how to iterate through the cursor. Your code gets only the first entry and then closes the cursor...
B4X:
Dim Row,RowNumber As Int
If Cursor1.RowCount > 0 Then                        'check if entries exist
    RowNumber = Cursor1.RowCount                    'set the row count variable
For Row = 0 To  RowNumber - 1
  Cursor1.Position = Row                        'set the Cursor to each row
  cities.Add(cursor1.GetString("esearch"))
Next
    Else
        ToastMessageShow("No items found", False)
    End If

Thank you Very much It work. !!!
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
Thank you Very much It work. !!!
I should have been honestly...
Im a noob about Sqlite.
I read your thread and just do a search for "sqlite example"
I opened the first thread found and have a look at this example.
I copied part of this code here to explain what is wrong

You could have found the solution by yourself within minutes. BUT you need to understand how a database-cursor works. Reading the Beginners guide about Databases will give you a lot of informations. And this solution here should be found in the beginners guide too ;)
 
Upvote 0
Top