Android Question passing field name in function

Kiran Raotole

Active Member
Licensed User
i make function to get value of any field ::

B4X:
Sub function(sqlite As SQL,book As String,accode As String,return_field) As String
    Return sqlite.ExecQuerySingleResult2("select '" & return_field & "' from GL where FBOOK = ? and FACCODE = ?", _
                                        Array As String(book,accode))
End Sub

this command return field name I want field value
 

Mahares

Expert
Licensed User
Longtime User
this command return field name I want field value
You need to get rid of the single quotes in return_field in the statement. See correct code below:
B4X:
Sub function(sqlite As SQL,book As String,accode As String,return_field) As String
    Return sqlite.ExecQuerySingleResult2("select " & return_field & " from GL where FBOOK = ? and FACCODE = ?", _
    Array As String(book,accode))
End Sub
 
Upvote 0

Kiran Raotole

Active Member
Licensed User
You need to get rid of the single quotes in return_field in the statement. See correct code below:
B4X:
Sub function(sqlite As SQL,book As String,accode As String,return_field) As String
    Return sqlite.ExecQuerySingleResult2("select " & return_field & " from GL where FBOOK = ? and FACCODE = ?", _
    Array As String(book,accode))
End Sub
thanks it works.
 
Upvote 0

RichardN

Well-Known Member
Licensed User
Longtime User
In the interests of keeping code as readable as possible I always name my own variables and sqlite fields with descriptive mixed-case names and construct SQL Queries using capitals for Sqlite reserved words. It is only a small detail but when queries (or their concantation) become complicated it makes it far easier to read.

Like this:

B4X:
Return Sqlite.ExecQuerySingleResult2("SELECT " & ReturnField & " FROM Gl WHERE Fbook = ? AND FacCode = ?", Array As String(Book,AcCode))

For what it's worth.......
 
Upvote 0

Diceman

Active Member
Licensed User
i make function to get value of any field ::

B4X:
Sub function(sqlite As SQL,book As String,accode As String,return_field) As String
    Return sqlite.ExecQuerySingleResult2("select '" & return_field & "' from GL where FBOOK = ? and FACCODE = ?", _
                                        Array As String(book,accode))
End Sub

this command return field name I want field value

Aren't you missing "as string" in the argument list for "return_field"?
Sub function(sqlite As SQL,book As String,accode As String,return_field as String) As String

I assume the value you are returning is a simple value like a string or numeric? And not a bitmap?
 
Upvote 0
Top