Android Question CloseD: Select id From tableName?

anOparator

Active Member
Licensed User
Longtime User
Am trying to get the “id” column from tableName WHERE name = EditText1.Text, and put “id ” into EditText2
I have tried:
B4X:
 Sub Button1_Click
'Cursor1 = SQL1.ExecQuery("SELECT id FROM tableName WHERE name = 'EditText1.Text'")
'Cursor1 = SQL1.ExecQuery("SELECT id FROM tableName WHERE name Like   EditText1.Text")  'No column = EditText1.Text
Cursor1 = SQL1.ExecQuery("SELECT id FROM tableName WHERE name = name")  ' HOW TO GET "id"
	Log("AAAA " & Cursor1)
	Log("BBBB " & $"${Cursor1}"$)
	Log("CCCC " & EditText1.Text)		' only a shadow of EditText1.Text
	Log("DDDD " & EditText2.Text)
'	Log("EEEE " & SQL1.id)				' Error = unknown member:id
'	Log("FFFF " & SQL1.tableName.id)	' Error = unknown member:tableName
'	Log("GGGG " & id)
'	Log("HHHH " & SQL1.tableName.id)
End Sub
Pls link me to a post that I may have missed or an example.
Thanks in advance
 

Attachments

  • tmp_getId-585785003.zip
    40.7 KB · Views: 270

DonManfred

Expert
Licensed User
Longtime User
B4X:
Sub Button1_Click
    Cursor1 = SQL1.ExecQuery2("SELECT id FROM tableName WHERE name = ?",Array As String(EditText1.Text))  ' HOW TO GET "id"
    ' Cursor will hold  ALL values and row which are fetched with this query
    For i = 0 To Cursor1.RowCount -1
        ' Iterate through all Results...
        Cursor1.Position = i
        ' Now get values from the active row
        Log($"Record #${i}: ${Cursor1.GetInt("id")}"$)
    Next
    Log($"Records found: ${Cursor1.RowCount}"$)
    ' Here only one result is expected....
    If Cursor1.RowCount = 1 Then
        ' set the cursor position
        Cursor1.Position = 0
        EditText2.Text = $"User ID is ${Cursor1.GetInt("id")}"$
    Else
        EditText2.Text = $"User ID is unknown... No results found"$
    End If
End Sub

Please note that the part
B4X:
    For i = 0 To Cursor1.RowCount -1
        ' Iterate through all Results...
        Cursor1.Position = i
        ' Now get values from the active row
        Log($"Record #${i}: ${Cursor1.GetInt("id")}"$)
    Next
is just in there for demonstration/your info.

For your issue only this code is needed
B4X:
Sub Button1_Click
    Cursor1 = SQL1.ExecQuery2("SELECT id FROM tableName WHERE name = ?",Array As String(EditText1.Text))  ' HOW TO GET "id"
    ' Cursor will hold  ALL values and row which are fetched with this query
    ' Here only one result is expected....
    If Cursor1.RowCount = 1 Then
        ' set the cursor position
        Cursor1.Position = 0
        EditText2.Text = $"User ID is ${Cursor1.GetInt("id")}"$
    Else
        EditText2.Text = $"User ID is unknown... No results found"$
    End If
End Sub

And if you are wondering about the String-Structures with the $signs...
https://www.b4x.com/android/forum/threads/b4x-smart-string-literal.50135/
 
Last edited:
Upvote 0

anOparator

Active Member
Licensed User
Longtime User
wow , this B4a is really a powerful code language, and you write poems with it. kudos
while I have tried smartStrings before I like the way they can turn a variable into a variable. Variable "i" becomes a variable ${i}.
Am reading this output 'Log($"Record #${i}: ${Cursor1.GetInt("id")}"$)' as Record #0: 1
will give this a swing this evening, thanks.
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…