Android Programming Press on the image to return to the main documentation page.

SQL

The SQL library allows you to create and manage SQL databases.
See the
SQL tutorial for more information.

List of types:

Cursor
ResultSet
SQL

Cursor


Events:

None

Members:


  Close

  ColumnCount As Int [read only]

  GetBlob (ColumnName As String) As Byte()

  GetBlob2 (Index As Int) As Byte()

  GetColumnName (Index As Int) As String

  GetDouble (ColumnName As String) As Double

  GetDouble2 (Index As Int) As Double

  GetInt (ColumnName As String) As Int

  GetInt2 (Index As Int) As Int

  GetLong (ColumnName As String) As Long

  GetLong2 (Index As Int) As Long

  GetString (ColumnName As String) As String

  GetString2 (Index As Int) As String

  IsInitialized As Boolean

  Position As Int

  RowCount As Int [read only]

Members description:

Close
Closes the cursor and frees resources.
ColumnCount As Int [read only]
Gets the number of columns available in the result set.
GetBlob (ColumnName As String) As Byte()
Returns the blob stored in the given column.
Example:
Dim Buffer() As Byte
Buffer = Cursor.GetBlob("col1")
GetBlob2 (Index As Int) As Byte()
Returns the blob stored in the column at the given ordinal.
Example:
Dim Buffer() As Byte
Buffer = Cursor.GetBlob2(0)
GetColumnName (Index As Int) As String
Returns the name of the column at the specified index.
The first column index is 0.
GetDouble (ColumnName As String) As Double
Returns the Double value stored in the given column.
The value will be converted to Double if it is of different type.
Example:
Log(Cursor.GetDouble("col2"))
GetDouble2 (Index As Int) As Double
Returns the Double value stored in the column at the given ordinal.
The value will be converted to Double if it is of different type.
Example:
Log(Cursor.GetDouble2(0))
GetInt (ColumnName As String) As Int
Returns the Int value stored in the given column.
The value will be converted to Int if it is of different type.
Example:
Log(Cursor.GetInt("col2"))
GetInt2 (Index As Int) As Int
Returns the Int value stored in the column at the given ordinal.
The value will be converted to Int if it is of different type.
Example:
Log(Cursor.GetInt2(0))
GetLong (ColumnName As String) As Long
Returns the Long value stored in the given column.
The value will be converted to Long if it is of different type.
Example:
Log(Cursor.GetLong("col2"))
GetLong2 (Index As Int) As Long
Returns the Long value stored in the column at the given ordinal.
The value will be converted to Long if it is of different type.
Example:
Log(Cursor.GetLong2(0))
GetString (ColumnName As String) As String
Returns the String value stored in the given column.
The value will be converted to String if it is of different type.
Example:
Log(Cursor.GetString("col2"))
GetString2 (Index As Int) As String
Returns the String value stored in the column at the given ordinal.
The value will be converted to String if it is of different type.
Example:
Log(Cursor.GetString2(0))
IsInitialized As Boolean
Position As Int
Gets or sets the current position (row).
Note that the starting position of a cursor returned from a query is -1.
The first valid position is 0.
Example:
Dim Cursor As Cursor
Cursor = SQL1.ExecQuery("SELECT col1, col2 FROM table1")
For i = 0 To Cursor.RowCount - 1
  Cursor.Position = i
  Log(Cursor.GetString("col1"))
  Log(Cursor.GetInt("col2"))
Next
Cursor.Close
RowCount As Int [read only]
Gets the numbers or rows available in the result set.

ResultSet

This type is an extension to the Cursor type.
It adds a single method (NextRow).
Its interface is the same as B4J and B4i ResultSet type.

Events:

None

Members:


  Close

  ColumnCount As Int [read only]

  GetBlob (ColumnName As String) As Byte()

  GetBlob2 (Index As Int) As Byte()

  GetColumnName (Index As Int) As String

  GetDouble (ColumnName As String) As Double

  GetDouble2 (Index As Int) As Double

  GetInt (ColumnName As String) As Int

  GetInt2 (Index As Int) As Int

  GetLong (ColumnName As String) As Long

  GetLong2 (Index As Int) As Long

  GetString (ColumnName As String) As String

  GetString2 (Index As Int) As String

  IsInitialized As Boolean

  NextRow As Boolean

  Position As Int

  RowCount As Int [read only]

Members description:

Close
Closes the cursor and frees resources.
ColumnCount As Int [read only]
Gets the number of columns available in the result set.
GetBlob (ColumnName As String) As Byte()
Returns the blob stored in the given column.
Example:
Dim Buffer() As Byte
Buffer = Cursor.GetBlob("col1")
GetBlob2 (Index As Int) As Byte()
Returns the blob stored in the column at the given ordinal.
Example:
Dim Buffer() As Byte
Buffer = Cursor.GetBlob2(0)
GetColumnName (Index As Int) As String
Returns the name of the column at the specified index.
The first column index is 0.
GetDouble (ColumnName As String) As Double
Returns the Double value stored in the given column.
The value will be converted to Double if it is of different type.
Example:
Log(Cursor.GetDouble("col2"))
GetDouble2 (Index As Int) As Double
Returns the Double value stored in the column at the given ordinal.
The value will be converted to Double if it is of different type.
Example:
Log(Cursor.GetDouble2(0))
GetInt (ColumnName As String) As Int
Returns the Int value stored in the given column.
The value will be converted to Int if it is of different type.
Example:
Log(Cursor.GetInt("col2"))
GetInt2 (Index As Int) As Int
Returns the Int value stored in the column at the given ordinal.
The value will be converted to Int if it is of different type.
Example:
Log(Cursor.GetInt2(0))
GetLong (ColumnName As String) As Long
Returns the Long value stored in the given column.
The value will be converted to Long if it is of different type.
Example:
Log(Cursor.GetLong("col2"))
GetLong2 (Index As Int) As Long
Returns the Long value stored in the column at the given ordinal.
The value will be converted to Long if it is of different type.
Example:
Log(Cursor.GetLong2(0))
GetString (ColumnName As String) As String
Returns the String value stored in the given column.
The value will be converted to String if it is of different type.
Example:
Log(Cursor.GetString("col2"))
GetString2 (Index As Int) As String
Returns the String value stored in the column at the given ordinal.
The value will be converted to String if it is of different type.
Example:
Log(Cursor.GetString2(0))
IsInitialized As Boolean
NextRow As Boolean
Moves the cursor to the next result. Returns false when the cursor reaches the end.
Example:
Do While ResultSet1.Next
'Work with Row
Loop
Position As Int
Gets or sets the current position (row).
Note that the starting position of a cursor returned from a query is -1.
The first valid position is 0.
Example:
Dim Cursor As Cursor
Cursor = SQL1.ExecQuery("SELECT col1, col2 FROM table1")
For i = 0 To Cursor.RowCount - 1
  Cursor.Position = i
  Log(Cursor.GetString("col1"))
  Log(Cursor.GetInt("col2"))
Next
Cursor.Close
RowCount As Int [read only]
Gets the numbers or rows available in the result set.

SQL

The main object that accesses the database.

Events:

QueryComplete (Success As Boolean, Result As ResultSet)
NonQueryComplete (Success As Boolean)

Members:


  AddNonQueryToBatch (Statement As String, Args As List)

  BeginTransaction

  Close

  EndTransaction

  ExecNonQuery (Statement As String)

  ExecNonQuery2 (Statement As String, Args As List)

  ExecNonQueryBatch (EventName As String) As Object

  ExecQuery (Query As String) As android.database.Cursor

  ExecQuery2 (Query As String, StringArgs() As String) As android.database.Cursor

  ExecQueryAsync (EventName As String, Query As String, Args As List) As Object

  ExecQuerySingleResult (Query As String) As String

  ExecQuerySingleResult2 (Query As String, StringArgs() As String) As String

  Initialize (Dir As String, FileName As String, CreateIfNecessary As Boolean)

  IsInitialized As Boolean

  TransactionSuccessful

Members description:

AddNonQueryToBatch (Statement As String, Args As List)
Adds a non-query statement to the batch of statements.
The statements are (asynchronously) executed when you call ExecNonQueryBatch.
Args parameter can be Null if it is not needed.
Example:
For i = 1 To 1000
  sql.AddNonQueryToBatch("INSERT INTO table1 VALUES (?)", Array(Rnd(0, 100000)))
Next
Dim SenderFilter As Object = sql.ExecNonQueryBatch("SQL")
Wait For (SenderFilter) SQL_NonQueryComplete (Success As Boolean)
Log("NonQuery: " & Success)
BeginTransaction
Begins a transaction. A transaction is a set of multiple "writing" statements that are atomically committed,
hence all changes will be made or no changes will be made.
As a side effect those statements will be executed significantly faster (in the default case a transaction is implicitly created for
each statement).
It is very important to handle transaction carefully and close them.
The transaction is considered successful only if TransactionSuccessful is called. Otherwise no changes will be made.
Typical usage:
SQL1.BeginTransaction
Try
  'block of statements like:
  For i = 1 to 1000
    SQL1.ExecNonQuery("INSERT INTO table1 VALUES(...)
  Next
  SQL1.TransactionSuccessful
Catch
  Log(LastException.Message) 'no changes will be made
End Try
SQL1.EndTransaction
Close
Closes the database.
Does not do anything if the database is not opened or was closed before.
EndTransaction
Ends the transaction.
ExecNonQuery (Statement As String)
Executes a single non query SQL statement.
Example:
SQL1.ExecNonQuery("CREATE TABLE table1 (col1 TEXT , col2 INTEGER, col3 INTEGER)")

If you plan to do many "writing" queries one after another, then you should consider using BeginTransaction / EndTransaction.
It will execute significantly faster.
ExecNonQuery2 (Statement As String, Args As List)
Executes a single non query SQL statement.
The statement can include question marks which will be replaced by the items in the given list.
Note that Basic4android converts arrays to lists implicitly.
The values in the list should be strings, numbers or bytes arrays.
Example:
SQL1.ExecNonQuery2("INSERT INTO table1 VALUES (?, ?, 0)", Array As Object("some text", 2))
ExecNonQueryBatch (EventName As String) As Object
Asynchronously executes a batch of non-query statements (such as INSERT).
The NonQueryComplete event is raised after the statements are completed.
You should call AddNonQueryToBatch one or more times before calling this method to add statements to the batch.
Note that this method internally begins and ends a transaction.
Returns an object that can be used as the sender filter for Wait For calls.
Example:
For i = 1 To 1000
  sql.AddNonQueryToBatch("INSERT INTO table1 VALUES (?)", Array(Rnd(0, 100000)))
Next
Dim SenderFilter As Object = sql.ExecNonQueryBatch("SQL")
Wait For (SenderFilter) SQL_NonQueryComplete (Success As Boolean)
Log("NonQuery: " & Success)
ExecQuery (Query As String) As android.database.Cursor
Executes the query and returns a cursor which is used to go over the results.
Example:
Dim Cursor As Cursor
Cursor = SQL1.ExecQuery("SELECT col1, col2 FROM table1")
For i = 0 To Cursor.RowCount - 1
  Cursor.Position = i
  Log(Cursor.GetString("col1"))
  Log(Cursor.GetInt("col2"))
Next
ExecQuery2 (Query As String, StringArgs() As String) As android.database.Cursor
Executes the query and returns a cursor which is used to go over the results.
The query can include question marks which will be replaced with the values in the array.
Example:
Dim Cursor As Cursor
Cursor = sql1.ExecQuery2("SELECT col1 FROM table1 WHERE col3 = ?", Array As String(22))

SQLite will try to convert the string values based on the columns types.
ExecQueryAsync (EventName As String, Query As String, Args As List) As Object
Asynchronously executes the given query. The QueryComplete event will be raised when the results are ready.
Note that ResultSet extends Cursor. You can use Cursor if preferred.
Returns an object that can be used as the sender filter for Wait For calls.
Example:
Dim SenderFilter As Object = sql.ExecQueryAsync("SQL", "SELECT * FROM table1", Null)
Wait For (SenderFilter) SQL_QueryComplete (Success As Boolean, rs As ResultSet)
If Success Then
  Do While rs.NextRow
    Log(rs.GetInt2(0))
  Loop
  rs.Close
Else
  Log(LastException)
End If
ExecQuerySingleResult (Query As String) As String
Executes the query and returns the value in the first column and the first row (in the result set).
Returns Null if no results were found.
Example:
Dim NumberOfMatches As Int
NumberOfMatches = SQL1.ExecQuerySingleResult("SELECT count(*) FROM table1 WHERE col2 > 300")
ExecQuerySingleResult2 (Query As String, StringArgs() As String) As String
Executes the query and returns the value in the first column and the first row (in the result set).
Returns Null if no results were found.
Example:
Dim NumberOfMatches As Int
NumberOfMatches = SQL1.ExecQuerySingleResult2("SELECT count(*) FROM table1 WHERE col2 > ?", Array As String(300))
Initialize (Dir As String, FileName As String, CreateIfNecessary As Boolean)
Opens the database file. A new database will be created if it does not exist and CreateIfNecessary is true.
IMPORTANT: this object should be declared in Sub Process_Globals.
Example:
Dim SQL1 As SQL
SQL1.Initialize(File.InternalDir, "MyDb.db", True)
IsInitialized As Boolean
Tests whether the database is initialized and opened.
TransactionSuccessful
Marks the transaction as a successful transaction. No further statements should be executed till calling EndTransaction.
Top