mysql_utils
Flutter mysql1 plugin util, Use mysql1 easily.
Pub
dependencies:
mysql_utils: ^1.0.7
APIs
Initialization connection
var db = MysqlUtils(
settings: ConnectionSettings(
host: '127.0.0.1',
port: 3306,
user: 'user',
password: 'password',
db: 'db',
useCompression: false,
useSSL: false,
timeout: const Duration(seconds: 10),
),
prefix: 'prefix_',
pool: true,
errorLog: (error) {
print('|$error\n├───────────────────────────');
},
sqlLog: (sql) {
print('|$sql\n├───────────────────────────');
},
connectInit: (db1) async {
print('whenComplete');
}
);
query
Native query
var row = await db
.query('select id from Product where id=? or description like ?', [1, 'ce%']);
print(row);
getAll(getOne) Multi table
Query Multi data , multi-table query
var res = await db.getAll(
table: 'user tb1,upload tb2',
fields: 'tb2.fileSize',
where: 'tb2.id>0 and tb2.uid=tb1.id',
debug: true,
);
print(res);
getOne
Query one data
var row = await db.getOne(
table: 'table',
fields: '*',
//group: 'name',
//having: 'name',
//order: 'id desc',
//limit: 10,//10 or '10 ,100'
//where: 'email=xxx@google.com',
where: {'email': 'xxx@google.com'},
);
print(row);
getAll
Query multiple data
var row = await db.getAll(
table: 'table',
fields: '*',
//group: 'name',
//having: 'name',
//order: 'id desc',
//limit: 10,//10 or '10 ,100'
where: {
'email': 'xxx@dd.com',
//'id': ['between', 0, 1],
//'id': ['notbetween', 0, 2],
//'id': ['in', [1,2,3]],
//'id': ['notin', [1,2,3]],
//'email': ['=', 'sss@cc.com'],
//'news_title': ['like', '%name%'],
//'user_id': ['>', 1],
},
);
print(row);
insert
Add a data
await db.insert(
table: 'table',
insertData: {
'telphone': '+113888888888',
'create_time': 1620577162252,
'update_time': 1620577162252,
},
);
insertAll
Add multiple data
await db.insertAll(
table: 'table',
insertData: [
{
'telphone': '13888888888',
'create_time': 1111111,
'update_time': 12121212,
'email': 'teenagex@dd.com'
},
{
'telphone': '13881231238',
'create_time': 324234,
'update_time': 898981,
'email': 'xxx@dd.com'
}
]);
update
Update data
await db.update(
table: 'table',
updateData:{
'telphone': '1231',
'create_time': 12,
'update_time': 12121212,
'email': 'teenagex@dd.com'
},
where:{
'id':1,
});
delete
Delete data
await db.delete(
table:'table',
where: {'id':1}
);
count
Statistical data
await db.count(
table: 'table',
fields: '*',
//group: 'name',
//having: 'name',
);
avg
await db.avg(
table: 'table',
fields: 'price',
//group: 'name',
//having: 'name',
);
min
await db.min(
table: 'table',
fields: 'price',
//group: 'name',
//having: 'name',
);
max
await db.max(
table: 'table',
fields: 'price',
//group: 'name',
//having: 'name',
);
Transaction
Transaction support
await db.startTrans();
var res1 = await db.delete(
table:'table',
where: {'id':1}
);
if(res1>0){
await db.commit();
}else{
await db.rollback();
}
isConnectionAlive
Connection is open or closed
var isAlive = await db.isConnectionAlive();
if (isAlive) print('mysql is isAlive');
createConnection
Create a new connection
var newDb = await db.createConnection();
demo test
cd example
dart run lib/main.dart
Libraries
- mysql1/mysql1
- mysql1/src/auth/auth_handler
- mysql1/src/auth/character_set
- mysql1/src/auth/handshake_handler
- mysql1/src/auth/ssl_handler
- mysql1/src/blob
- mysql1/src/buffer
- mysql1/src/buffered_socket
- mysql1/src/constants
- mysql1/src/handlers/debug_handler
- mysql1/src/handlers/handler
- mysql1/src/handlers/ok_packet
- mysql1/src/handlers/parameter_packet
- mysql1/src/handlers/ping_handler
- mysql1/src/handlers/quit_handler
- mysql1/src/handlers/use_db_handler
- mysql1/src/mysql_client_error
- mysql1/src/mysql_exception
- mysql1/src/mysql_protocol_error
- mysql1/src/prepared_statements/binary_data_packet
- mysql1/src/prepared_statements/close_statement_handler
- mysql1/src/prepared_statements/execute_query_handler
- mysql1/src/prepared_statements/prepare_handler
- mysql1/src/prepared_statements/prepare_ok_packet
- mysql1/src/prepared_statements/prepared_query
- mysql1/src/query/query_stream_handler
- mysql1/src/query/result_set_header_packet
- mysql1/src/query/standard_data_packet
- mysql1/src/results/field
- mysql1/src/results/results_impl
- mysql1/src/results/row
- mysql1/src/single_connection
- mysql_utils