cursor not initialized

engvidal

Member
Licensed User
Longtime User
Hi

I am studing sql access and I receive this message when executing this line:

cursor1=SQL1.ExecQuery("SELECT nome, email, telefone FROM sql_test")
android.database.sqlite.SQLiteException: no such table: sql_test: , while compiling: SELECT nome, email, telefone FROM sql_test


sql_test exists and at debug (local vars) I read:Cursor not initialized.

If I check the cursor the code above runs ok (see *)
B4X:
Dim cursor1 As Cursor
If cursor1.IsInitialized Then   '(*) this if is allways false
   cursor1=SQL1.ExecQuery("SELECT nome, email, telefone FROM sql_test")      '(*) never run
   cursor1.Position=indice
   label3.Text=cursor1.GetString("nome")
   label4.Text=cursor1.GetString("email")
   label5.Text=cursor1.GetString("telefone")
End If
 

Attachments

  • sql_teste.zip
    11.3 KB · Views: 151

Mahares

Expert
Licensed User
Longtime User
I changed the below btLer_Click sub for you. See corrected sub below:
B4X:
Dim cursor1 as cursor    'Dim this in Globals

B4X:
Sub btLer_Click
  Dim MyCount As Int
   If IsNumber(EditText1.Text) Then
     MyCount=SQL1.ExecQuerySingleResult("SELECT count(*) FROM sql_test")
      indice = EditText1.Text
      If (indice < MyCount) Then     
            cursor1=SQL1.ExecQuery("SELECT nome, email, telefone FROM sql_test")      
            cursor1.Position=indice
            label3.Text=cursor1.GetString("nome")
            label4.Text=cursor1.GetString("email")
            label5.Text=cursor1.GetString("telefone")
      Else
         Msgbox("Table has  " & MyCount & " records. Indice must be less than record count","")
          EditText1.Text=""
      End If
   Else   
      Msgbox("indice incorreto","Erro !")
   End If   
End Sub
It will work for you now.
 
Upvote 0

mc73

Well-Known Member
Licensed User
Longtime User
Try removing the cursor1.isInitialized check.

Also, in the btLer_click sub you'll get an error when indice=2. This is because you added two elements upon db's creation, thus you should have
B4X:
if indice<2 ...
The
B4X:
SQL1.ExecNonQuery("DROP TABLE IF EXISTS sql_test")
isn't really needed, since you only insert records upon db's creation, thus the table doesn't exist.

Also, the
B4X:
If SQL1.IsInitialized = False Then
        SQL1.Initialize(File.DirRootExternal,"sql_test.sql", False)
    End If
can be avoided, since you are initializing db at your previous steps.

Inside btLer_click, instead of loading all your records to a cursor, you can simply load the one matching the indice, IF upon db's creation, you insert an ID fields, which is strongly recommended. This way you can write for e.g.

B4X:
cursor1=SQL1.ExecQuery2("SELECT nome, email, telefone FROM sql_test WHERE indice=?",Array As String (indice))        
                cursor1.Position=0
Finally, after getting data from the cursor, it is always a good idea to
B4X:
cursor1.close
 
Upvote 0

engvidal

Member
Licensed User
Longtime User
Dear Mahares

Thank You very very much. It worked fine.

On the original code, even using index of 0 the error ocurred.
But now is OK.

Vidal
 
Upvote 0

engvidal

Member
Licensed User
Longtime User
Hi mc73

Thank you for your attention.

Just one question. why to do
B4X:
cursor1.position=0
?

Vidal
 
Upvote 0
Top