Oracle is the most popular commercial SQL database. Dart can talk to Oracle using the pub package oracledart
by Alexander Aprelev (https://github.com/aam/oracledart). This is a Dart native extension of C++, using the dart_api
interface to integrate into Dart. It requires Oracle Instant Client to be present on the machine, and its OCCI binaries must be included in the PATH variable.
In this section, we will demonstrate how to use this driver step by step. You can see it in action in the project using_oracle
.
To get the database software, download and execute the Oracle installer from http://www.oracle.com/technetwork/database/enterprise-edition/downloads/index-092322.html.
jobs.txt
. This creates the table jobs
, together with the dbKey
, type
, salary
, company
, posted
, and open
columns; dbKey
is the primary key.oracledart
to pubspec.yaml
and save it. A pub get
command is then done automatically.The application starts from using_oracle.dart
, where a JobStore
object is created, the database is opened, records are written to the jobs
table, and then these records are retrieved. The code from using_oracle.dart
is identical to the code from using_postgresql.dart
, except that we now import jobstore_oracle.dart
; so please refer to the previous recipe. The jobstore_oracle.dart
file contains the code to talk to the database driver:
// 1- importing the driver: import 'package:oracledart/oracledart.dart'; import 'job.dart'; class JobStore { final List<Job> jobs = new List(); Job job; OracleConnection conn; // connection object OracleResultset resultset; OracleStatement stmt; var connStr = ' "(DESCRIPTION=" "(ADDRESS=(PROTOCOL=TCP)(HOST=oracledb)(PORT=1521))"' '"(CONNECT_DATA=(SERVICE_NAME=XE)(SERVER=DEDICATED)))"'; // 2- opening a connection to the database: openAndStore() { connect("SYS", "avalon", connStr).then((oracleconnection) { conn = oracleconnection; print('connected with Oracle!'), storeData(); }).catchError(print); } storeData() { for (job in jobs) { insert(job); } } // 3- inserting a record in a table: insert(Job job) { var jobMap = job.toMap(); var insertSql = 'insert into jobs values (:1, :2, :3, :4, :5, :6)'; stmt = conn.createStatement(insertSql); stmt.setInt(1, jobMap['dbKey']); stmt.setString(2, jobMap['type']); stmt.setInt(3, jobMap['salary']); stmt.setString(4, jobMap['company']); stmt.setString(5, jobMap['posted']); stmt.setString(6, jobMap['open']); stmt.executeQuery(); } openAndRead() { connect("SYS", "avalon", connStr).then((oracleconnection) { conn = oracleconnection; readData(); }).catchError(print); } // 8- reading records from a table: readData() { resultset = conn.select("select * from jobs"); processJob(resultset); } // 9- working with record data: processJob(results) { while (resultset.next()) { print('dbKey: ${resultset.getInt(0)}'), print('type: ${resultset.getString(1)}'), print('salary: ${resultset.getInt(2)}'), print('company: ${resultset.getString(3)}'), print('posted: ${resultset.getString(4)}'), print('open: ${resultset.getString(5)}'), } } }
First, import the oracledart
package. The connect
method takes the user, his/her password, and then a connection string. In its callback, an OracleConnection
object conn
is made available. Querying tables is done with conn.select(selectSql)
, where selectSql
is the selected SQL string. This returns an OracleResultset
object that you can iterate throughout with next()
. Values of fields can only be extracted by an index with getInt
or getString
.
If you need parameters in your statement, as is probably the case for insert, update, or delete statements, you first need to call conn.createStatement(sqlStr)
, where sqlStr
contains :i
indicators (i equals to ith index, starting from 0). These :i
position holders must be filled with stmt.setInt(i, value)
or stmt.setString(i, value)
. Then, the statement can be executed with stmt.executeQuery()
.
In this and the previous recipes, we discussed the SQL database servers that Dart can talk to at this time using specialized drivers. However, for example, for the popular Microsoft SQL Server there is no driver yet. In this case, we can use the pub odbc
package by Juan Mellado (https://code.google.com/p/dart-odbc/). The ODBC binding is made with a Dart native extension, which makes it a bit more involved to be used, and for now it only exists in a 32-bit version.