mysql_utils 是 mysql_client_plus 的一個輔助庫,可簡化 Flutter 應用程式中的 MySQL 資料庫互動。它提供連接 MySQL 資料庫、執行查詢和處理結果的簡化方法,最大限度地減少樣板程式碼。該庫旨在提升開發人員的生產力,抽象常見操作,同時保持與 Android、iOS、macOS、Windows 和 Linux 等多個平台的相容性。該套件非常適合需要可靠且簡單的 MySQL 集成而無需複雜設定的專案。
適用情境
簡單的 MySQL 查詢執行
資料庫連接管理
跨平台資料庫存取
簡化版 CRUD 操作
與 mysql_client_plus 的整合
主要特色
易於使用的查詢介面
基於 mysql_client_plus
支援所有主要的桌面和行動平台
減少樣板程式碼
自動結果解析
適合對象
需要 MySQL 連接的 Flutter 應用
尋求簡單資料庫助手的開發者
使用 mysql_client_plus 的項目
後端服務與 MySQL 資料庫
使用資料庫進行快速原型設計
使用建議
需要 mysql_client_plus 作為依賴項
僅限 MySQL 資料庫
不適合複雜的SQL交易
無內建遷移支援
依賴外部 MySQL 伺服器的可用性
原始 README
以下為關聯原始碼儲存庫的 README 原文快照,最新內容請造訪原始碼儲存庫。
mysql_utils
Flutter mysql plugin helps extend classes.
mysql_client has been abandoned and is now maintained by the author of this library. The new library is mysql_client_plus。
Updated settings from Map to MysqlUtilsSettings ⚠️⚠️⚠️
Added support for BLOB and JSON types
Added support for SSL certificates
Added support for calling stored procedures
Added support for sha256_password authentication
Added global debug option
Added tests
APIs
Initialization connection
Use the singleton mode, and call close() after use to close the database connection.
var db = MysqlUtils(
// ⚠️⚠️⚠️ settings from `Map` to `MysqlUtilsSettings`
settings: MysqlUtilsSettings(
// The hostname of the MySQL server, defaults to localhost.
host: '127.0.0.1',
// The port number to connect to, defaults to 3306.
port: 3306,
// The username to connect as, defaults to root.
user: 'your_user_sha256',
// The password to connect with, defaults to empty string.
password: 'your_password_sha256',
// The database to use, defaults to empty string.
db: 'testdb',
// Whether to use SSL, defaults to false.
secure: true,
// The prefix to use for table names, defaults to empty string.
prefix: '',
// The maximum number of connections to keep open, defaults to 1000.
maxConnections: 1000,
// The timeout in milliseconds for each connection, defaults to 10000.
timeoutMs: 10000,
// Whether to escape all queries, defaults to true.
sqlEscape: true,
// Whether to use a connection pool, defaults to false.
pool: true,
// The collation to use for all queries, defaults to utf8mb4_general_ci.
collation: 'utf8mb4_general_ci',
// Whether to use SSL, defaults to false.
debug: true,
// The SSL options to use for all queries, defaults to null.// securityContext: SecurityContext(),// sslMode: SslMode.require,// onBadCertificate: (certificate) => true,
),
errorLog: (error) {
print(error);
},
sqlLog: (sql) {
print(sql);
},
connectInit: (db1) async {
print('whenComplete');
}
);
query
Native query
//var row = await db.query('select id from test_data2 where id=? or string_column like ?', whereValues: [1, '%串%'], isStmt: true);var row = await db.query("select id from test_data2 where id=1 or string_column like '%mysql%'");
print(row.toMap());
print(row.toMap());
//// print(row.rowsAssoc.first.assoc());// for (var item in row.rowsAssoc) {// print(item.assoc());// }// for (final row in row.rowsAssoc) {// print(row.colAt(0));// print(row.colByName("nickname"));// print(row.assoc());// }// db.close();
Multi table query
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: '*',
//excludeFields: 'telphone,image',//group: 'name',//having: 'name',//order: 'id desc',//limit: 10,//10 or '10 ,100'// where: {// 'email': 'xxx@google.com',// 'id': ['between', '1,4'],// 'email2': ['=', 'sss@google.com'],// 'news_title': ['like', '%name%'],// 'user_id': ['>', 1],// 'user_id': ['<', 1],// 'user_id': ['<>', 1],// 'user_id': ['=', 1],// 'email3': ['!=', 'sss@google.com'],// '_SQL': '(`isNet`=1 OR `isNet`=2)',// },//where:'`id`=1 AND name like "%jame%"',
);
print(row);
getAll
Query multiple data
var row = await db.getAll(
table: 'table',
fields: '*',
//excludeFields: 'telphone,image',//group: 'name',//having: 'name',//order: 'id desc',//limit: 10,//10 or '10 ,100'// where: {// 'email': 'xxx@google.com',// 'id': ['between', '1,4'],// 'email2': ['=', 'sss@google.com'],// 'news_title': ['like', '%name%'],// 'user_id': ['>', 1],// '_SQL': '(`isNet`=1 OR `isNet`=2)',// },//where:'`id`=1 AND name like "%jame%"',
);
print(row);