I am just developing an Datanavigator control. The control may be used with the designer and deals with several buttons to move between the records and to activate data insert, update and delete. The values to be stored in the database are comming from input fields (by example EditText) in the layout of an activity or b4x page.
For example:
[CODE lang="b4x" title="Control Code buidling and accessing SQL Insert/Update"]Sub UpdateRecord_Click
Try
'1 ## get values from the input mask by event in the activity/page
' ## store the values in DBNValueMap(Fieldname, Value)
If SubExists(mCallBack, mEventName & "_GetValues") = True Then
' get col name,type and value
CallSub2(mCallBack,mEventName & "_GetValues",DBNDataSet
End If
'2 ## check all column values
Dim tempColumnInfo As ResultSet
Dim Error As Boolean = False
Dim checkUnique As Boolean =True
Dim ColumnType As String
Dim SQLstring As String
Dim ColumnNames As String
Dim ColumnValues As String
ToastMessageShow("Prüfe eingegebene Werte....",True)
For Each ColumnName As String In DBNValueMap.Keys
' get the column info
tempColumnInfo = getColumnInfo(ColumnName)
tempColumnInfo.Position = 0
' check if null values allowed and if input field has a value
If tempColumnInfo.Getstring("col_not_null") = True Then
If DBNValueMap.Get(ColumnName) = Null Then
xui.Msgbox2Async( _
"Keine Eingabe in dem einem Pflichtfeld!" & CRLF & _
"Funktion wird abgebrochen.","Eingabefehler " & ColumnName,"OK","","",IcoAttention)
Error = True
Exit
End If
End If
' check if the value must be unique
If DBNNewRecord = False Then
' for update check if unique check is nessessary
ColumnType = tempColumnInfo.Getstring("col_type")
Select ColumnType
Case "text"
If DBNDataSet.GetString(ColumnName) = DBNValueMap.Get(ColumnName) Then checkUnique = False
Case "Int"
If DBNDataSet.GetInt(ColumnName) = DBNValueMap.Get(ColumnName) Then checkUnique = False
End Select
End If
If checkUnique = True And DBNValueMap.ContainsKey(ColumnName & "_unique") = True Then
If checkIsUnique(ColumnName) = True Then
xui.Msgbox2Async( _
"Eingabe bereits gespeichert!" & CRLF & _
"Der Wert muss eindeutig sein," & CRLF & _
"Funktion wird abgebrochen.","Eingabefehler " & ColumnName,"OK","","",IcoAttention)
Error = True
Exit
End If
End If
Next
'3 ## if new record perform insert
If Error = False And DBNNewRecord = True Then
ToastMessageShow("Füge Daten hinzu....",True)
' bulid colum name part
ColumnNames = "("
For Each ColumnName As String In DBNValueMap.Keys
ColumnNames = ColumnNames & ColumnName & ","
Next
' strip last comma off
ColumnNames = ColumnNames.SubString2(0,ColumnNames.Length-1) & ")"
' build values part
ColumnValues = "("
For Each ColumnName As String In DBNValueMap.Keys
ColumnType = tempColumnInfo.Getstring("col_type")
Select ColumnType
Case "text"
ColumnValues = ColumnValues & "'" & DBNValueMap.Get(ColumnName)& "',"
Case "Int"
ColumnValues = ColumnValues & DBNValueMap.Get(ColumnName)& ","
End Select
Next
' strip last comma off
ColumnValues = ColumnValues.SubString2(0,ColumnNames.Length-1) & ")"
' perform dtatabase insert
DBNSQL.ExecNonQuery("INSERT INTO " & DBNTableName & ColumnNames & " VALUES" & ColumnValues)
ToastMessageShow("Daten erfolgreich hinzugefügt.",True)
' set record pointer to last record
DBNCurrentIndex = DBNDataSet.rowcount-1
' fire event
If SubExists(mCallBack, mEventName & "_CurrentIndexChanged") = True Then
CallSub2(mCallBack,mEventName & "_CurrentIndexChanged",DBNDataSet)
End If
'4 ## perform update
Else if Error = False And DBNNewRecord = False Then
ToastMessageShow("Speichere die Änderungen....",True)
' bulid colum name part
For Each ColumnName As String In DBNValueMap.Keys
ColumnType = tempColumnInfo.Getstring("col_type")
Select ColumnType
Case "text"
ColumnValues = ColumnValues & ColumnName & "= '" & DBNValueMap.Get(ColumnName)& "',"
Case "Int"
ColumnValues = ColumnValues & ColumnName & "=" & DBNValueMap.Get(ColumnName)& ","
End Select
Next
' strip last comma off
ColumnValues = ColumnValues.SubString2(0,ColumnNames.Length-1) & ")"
' get rowid
SQLstring = "UPDATE " & DBNTableName & "SET " & ColumnValues & "WHERE rowid=" & DBNCurrentRowid
DBNSQL.ExecNonQuery(SQLstring)
ToastMessageShow("Änderungen erfolgreich gespeichert.",True)
End If
' reset new record flag
DBNNewRecord = False
' release resultset
tempColumnInfo.close
Catch
If DBNNewRecord = True Then
ToastMessageShow("Neue Daten konnten nicht gespeichert werden!",True)
Else
ToastMessageShow("Änderungen konnten nicht gespeichert werden!",True)
End If
xui1.Msgbox2Async(LastException,"Fehler: DBN002","OK","","",IcoError)
End Try
End Sub[/CODE]
[CODE lang="b4x" title="Event Codeblock in the Activity/b4x page"]' ## DBNav managed database, save button clicked
Sub DBNav1_GetValues(DS As ResultSet)
DBNav1.DBNValueMap.put("Title", EditTextTitel.text)
DBNav1.DBNValueMap.put("Title_unique", EditTextTitel.text) 'report column info -is unique-
DBNav1.DBNValueMap.put("Text_1", EditTextTitel.text)
End Sub[/CODE]
As you see what I need is a solution for a BLOB field as part oft the select /case statement to build the sql string part for the data column.
By the way I know that this code can be optimized but this is a part of the work after having the code basically workinmg for all kind of sql datatypes.
Greetings Guenter