DatabaseColumn

From Xojo Documentation

Class (inherits from Object)


New in 2019r2

Used to access the values of columns in a row in a database table.

Properties
BooleanValue Int64Value StringValue
CurrencyValue IntegerValue Value
DateTimeValue Name fa-lock-32.png
DoubleValue NativeValue fa-lock-32.png
Methods
PictureValue

Notes

Assignments to the DateTimeValue property store the time as well as the date, as with DatabaseRows. When getting a value, the resulting DateTime object may contain a time as well as a date. For fields of type Date, the time will be 00:00:00 (midnight); and for fields of type Time, the date will be January 1, 0001. Fields of type TimeStamp contain valid data for both the date and time.

Boolean fields are strict about what they expect when using the BooleanValue function: "0" and "False" are treated as False and "1" and "True" are treated as True. The behavior of any other values is undefined when retrieved using BooleanValue. The StringValue property, on the other hand, should be able to retrieve the original data if it can't be identified as a Boolean.

The conversion operator, Operator_Convert, has been added to StringValue, BooleanValue, DateTimeValue, IntegerValue, and DoubleValue.

Typically you should use the type-specific "Value" properties to get and set values from the database.

NULL Columns

The properties that return intrinsic value types (Boolean, Currency, Double, Int64, Integer, String) all return their appropriate default value if the database column is NULL.

To check if a column is NULL, use the Value property like this:

If rs.Column("MyColumn").Value Is Nil Then
// Column value is NULL
End If

For situations where you need to set a database column to NULL, you should use the Value property like this:

rs.EditRow
rs.Column("MyColumn").Value = Nil // sets to NULL in the database
rs.SaveRow

Sample Code

The following method populates a ListBox with a RowSet. It uses the Name and StringValue properties to obtain the column names and values:

Sub PopulateListBox(dataList As Listbox, rows As RowSet)
If rows Is Nil Then Return

// set up listbox state for population
dataList.RemoveAllRows

// Add the DB columns as the heades for the ListBox
dataList.ColumnCount = rows.ColumnCount
dataList.Column(-1).WidthExpression = "100"
For i As Integer = 0 To rows.LastColumnIndex
dataList.Heading(i) = rows.ColumnAt(i).Name
Next

// Add the data from the table
for each row as DatabaseRow in rows
dataList.AddRow("")

For i As Integer = 0 To rs.LastColumnIndex
dataList.CellValueAt(dataList.LastAddedRowIndex, i) = rs.ColumnAt(i).StringValue
Next

next
End Sub

The following code uses the Value property to set the values of fields of different data types.

Var myDB As SQLiteDatabase
Var rows As RowSet
rows = myDB.SelectSQL("SELECT * FROM MyTable")
Try
rows.EditRow
rows.Column("MyName").Value = Nil // NULL this field, does not work for all database plugins
rows.Column("MyID").Value = 23
rows.Column("MyText").Value = "Test"
rows.SaveRow
myDB.CommitTransaction
Catch error as DatabaseException
MessageBox("Error: " + error.Message)
End Try

See Also

Database, DatabaseRow, RowSet classes.