Bundle ODBC database access layer. Connection manages database sessions; Statement and ParameterStatement execute SQL queries; ResultSet iterates over rows and extracts typed column values. Compile with -lib odbc.
Connection
ODBC database connection
Operations
- New
- Close
- Commit
- CreateParameterStatement
- GetColumns
- GetLastError
- GetTables
- IsOpen
- Rollback
- Select
- SetAutoCommit
- Update
Commit #
Commits the current transaction.
method : public : Commit() ~ BoolReturn
| Type | Description |
|---|---|
| Bool | true if successful |
CreateParameterStatement #
Create a parametrized SQL statement
method : public : CreateParameterStatement(sql:String) ~ ParameterStatementParameters
| Name | Type | Description |
|---|---|---|
| sql | String | SQL statement |
Return
| Type | Description |
|---|---|
| ParameterStatement | result set |
Example
conn := Database.ODBC.Connection->New("mydsn", "user", "pass");
if(conn->IsOpen()) {
stmt := conn->CreateParameterStatement("INSERT INTO users(name,age) VALUES(?,?)");
stmt->SetVarchar(1, "Alice"); # bind first parameter
stmt->SetInt(2, 30); # bind second parameter
stmt->Update();
conn->Close();
};GetColumns #
Gets column information for a table as name:type pairs.
method : public : GetColumns(table:String) ~ String[]Parameters
| Name | Type | Description |
|---|---|---|
| table | String | table name |
Return
| Type | Description |
|---|---|
| String | array of column descriptors (name:type), or Nil on failure |
Example
conn := Database.ODBC.Connection->New("mydsn", "user", "pass");
if(conn->IsOpen()) {
cols := conn->GetColumns("users"); # get column descriptors
each(c in cols) { cols[c]->PrintLine(); };
conn->Close();
};GetLastError #
Gets the last error message from the connection.
method : public : GetLastError() ~ StringReturn
| Type | Description |
|---|---|
| String | error message string, or empty if no error |
Example
conn := Database.ODBC.Connection->New("mydsn", "user", "pass");
if(conn->Update("BAD SQL") < 0) {
conn->GetLastError()->PrintLine(); # print connection error
};
conn->Close();GetTables #
Gets the list of table names in the data source.
method : public : GetTables() ~ String[]Return
| Type | Description |
|---|---|
| String | array of table names, or Nil on failure |
Example
conn := Database.ODBC.Connection->New("mydsn", "user", "pass");
if(conn->IsOpen()) {
tables := conn->GetTables(); # list all tables
each(t in tables) { tables[t]->PrintLine(); };
conn->Close();
};IsOpen #
Check rather a connect is open
method : public : IsOpen() ~ BoolReturn
| Type | Description |
|---|---|
| Bool | true if connected, false otherwise |
Example
conn := Database.ODBC.Connection->New("mydsn", "user", "pass");
if(conn->IsOpen()) { # verify connection succeeded
"Connected!"->PrintLine();
conn->Close();
};New # constructor
Constructor
New(ds:String, username:String, password:String)Parameters
| Name | Type | Description |
|---|---|---|
| ds | String | ODBC data source |
| username | String | ODBC connection name |
| password | String | ODBC connection password |
Example
conn := Database.ODBC.Connection->New("mydsn", "admin", "pass");
if(conn->IsOpen()) {
rs := conn->Select("SELECT name FROM users");
while(rs->Next()) { rs->GetVarchar(1)->PrintLine(); };
rs->Close();
conn->Close();
};New # constructor
Constructor using a connection string
New(conn_str:String)Parameters
| Name | Type | Description |
|---|---|---|
| conn_str | String | ODBC connection string (e.g. "Driver={SQL Server};Server=localhost;Database=mydb;Uid=user;Pwd=pass;") |
Example
cs := "Driver={SQLite3};Database=app.db;";
conn := Database.ODBC.Connection->New(cs); # connect via connection string
if(conn->IsOpen()) {
conn->Update("CREATE TABLE IF NOT EXISTS log (msg VARCHAR(255))");
conn->Close();
};Rollback #
Rolls back the current transaction.
method : public : Rollback() ~ BoolReturn
| Type | Description |
|---|---|
| Bool | true if successful |
Select #
Executes a SQL select statement
method : public : Select(sql:String) ~ ResultSetParameters
| Name | Type | Description |
|---|---|---|
| sql | String | SQL statement |
Return
| Type | Description |
|---|---|
| ResultSet | result set |
Example
conn := Database.ODBC.Connection->New("mydsn", "user", "pass");
if(conn->IsOpen()) {
rs := conn->Select("SELECT id, name FROM users"); # execute query
while(rs->Next()) {
rs->GetVarchar("name")->PrintLine();
};
rs->Close();
conn->Close();
};SetAutoCommit #
Sets autocommit mode. When false, changes must be explicitly committed.
method : public : SetAutoCommit(enable:Bool) ~ BoolParameters
| Name | Type | Description |
|---|---|---|
| enable | Bool | true for autocommit, false for manual transaction control |
Return
| Type | Description |
|---|---|
| Bool | true if successful |
Example
conn := Database.ODBC.Connection->New("mydsn", "user", "pass");
conn->SetAutoCommit(false); # start manual transaction
conn->Update("DELETE FROM temp_log");
conn->Commit(); # commit changes
conn->Close();Update #
Executes a SQL update statement
method : public : Update(sql:String) ~ IntParameters
| Name | Type | Description |
|---|---|---|
| sql | String | SQL statement |
Return
| Type | Description |
|---|---|
| Int | result code |
Example
conn := Database.ODBC.Connection->New("mydsn", "user", "pass");
if(conn->IsOpen()) {
rc := conn->Update("INSERT INTO log(msg) VALUES ('hello')");
"rows affected: {$rc}"->PrintLine();
conn->Close();
};