Android Question Best way to do this concatenation?

Robert Valentino

Well-Known Member
Licensed User
Longtime User
I have some code that I am converting that use to be all & for concatenation of strings, moving to smart strings and was wondering what was best

In the second part of this building the SQL statement I use a & to and on the 2nd part of the string
B4X:
            mSQLStatement =  $"DELETE FROM ${DEFINE_DB_TiesTableName} where ${DEFINE_DB_TagTiesFWID} = ${FWID}"$
            
            If  FWTypeID > 0 Then
                mSQLStatement = mSQLStatement &$"  and  ${DEFINE_DB_TagTiesFWTypeID} = ${FWTypeID}"$
            End If

OR

Maybe me wonder should I just put the first part of the command in the smart string as well?

B4X:
            mSQLStatement =  $"DELETE FROM ${DEFINE_DB_TiesTableName} where ${DEFINE_DB_TagTiesFWID} = ${FWID}"$
            
            If  FWTypeID > 0 Then
                mSQLStatement = $"${mSQLStatement}  and  ${DEFINE_DB_TagTiesFWTypeID} = ${FWTypeID}"$
            end if

Just wondering
 

Mahares

Expert
Licensed User
Longtime User
Just wondering
B4X:
mSQLStatement = $"DELETE FROM ${DEFINE_DB_TiesTableName} 
    WHERE ${DEFINE_DB_TagTiesFWID} = ${FWID}
    AND  ${DEFINE_DB_TagTiesFWTypeID} = ${FWTypeID}"$
Or if you can use a parameterized query, it is better:
B4X:
 mSQLStatement = $"DELETE FROM ${DEFINE_DB_TiesTableName}
    WHERE ${DEFINE_DB_TagTiesFWID} = ?
    AND ${DEFINE_DB_TagTiesFWTypeID} = ?"$
 
Upvote 0

Mahares

Expert
Licensed User
Longtime User
Think you missed what I was asking.
Why don't you explain yourself better then, if I missed what you are asking. Maybe some one will not miss what you are asking. I gave you 2 options, not one. You choose what works for you. Tell us what I missed from your question.
 
Last edited:
Upvote 0

aeric

Expert
Licensed User
Longtime User
I think OP wants an SQL command to handle a scenario where FWTypeID is optional (only applies when it is a positive value).

One line code:
B4X:
mSQLStatement = $"DELETE FROM ${DEFINE_DB_TiesTableName} WHERE ${DEFINE_DB_TagTiesFWID} = ${FWID}
${IIf(FWTypeID > 0, $" AND ${DEFINE_DB_TagTiesFWTypeID} = ${FWTypeID}"$, "")}"$
 
Upvote 0

Mahares

Expert
Licensed User
Longtime User
OP wants an SQL command to handle a scenario where FWTypeID is optional (only applies when it is a positive value).
Glad you read the OP mind. However,DEFINE_DB_TagTiesFWID and DEFINE_DB_TagTiesFWTypeID represent columns. I don't think you can use variables for the column name. If they are actual column names and not variables, then you need to remove the string literal tags from both columns. I wish the OP explained his statement more clearly so there is less guessing.
 
Upvote 0

Robert Valentino

Well-Known Member
Licensed User
Longtime User
Aeric, like your idea

the DEFINE are const
B4X:
    Private Const DEFINE_DB_TagTiesFWID                                 As String = "FWID"
    Private Const DEFINE_DB_TagTiesFWTypeID                         As String = "FWTypeID"
 
Upvote 0
Top