MySQL client for Dart.
var s = ConnectionSettings(
user: "root",
password: "dart_jaguar",
host: "localhost",
port: 3306,
db: "example",
);
var conn = await MySqlConnection.connect(s);
await conn.close();
Results results = await conn.execute('select name, email from users');
Results
is an iterable of Row
. Columns can be accessed from Row
using
integer index or by name.
results.forEach((Row row) {
// Access columns by index
print('Name: ${row[0]}, email: ${row[1]}');
// Access columns by name
print('Name: ${row.name}, email: ${row.email}');
});
await conn.prepared('insert into users (name, email, age) values (?, ?, ?)',
['Bob', 'bob@bob.com', 25]);
An insert query's results will be empty, but will have an id if there was an auto-increment column in the table:
print("New user's id: ${result.insertId}");
var results = await query.preparedMulti(
'insert into users (name, email, age) values (?, ?, ?)',
[['Bob', 'bob@bob.com', 25],
['Bill', 'bill@bill.com', 26],
['Joe', 'joe@joe.com', 37]]);
Transaction trans = await pool.begin();
try {
var result1 = await trans.execute('...');
var result2 = await trans.execute('...');
await trans.commit();
} catch(e) {
await trans.rollback();
}
await pool.transaction((trans) {
var result1 = await trans.execute('...');
var result2 = await trans.execute('...');
});