搜索插件、代码仓与 APP 分类

插件详情v0.22.0

mysql1

一个用于连接和查询 MySQL 和 MariaDB 数据库的库。

AndroidiOSmacOSWindowsLinux
近 30 天下载
1.2万
喜欢数
493
Pub 分数
160

安装插件

flutter pub add mysql1

插件介绍

基于插件公开信息、社区数据与项目使用情况整理的选型参考。

mysql1 包是一个 Dart 库,可让 Flutter 应用程序连接并查询 MySQL 和 MariaDB 数据库。它提供了一个简单的 API,用于执行 SQL 语句、处理事务以及高效管理数据库连接。该库专为跨平台使用而设计,支持 Android、iOS、macOS、Windows 和 Linux,非常适合移动和桌面应用程序的后端集成。该库积极维护,并基于原生 MySQL 协议构建,确保了可靠的性能和对现代数据库功能的兼容性。

适合场景

  • 将 Flutter 应用连接到 MySQL/MariaDB 数据库
  • 实时执行动态SQL查询
  • 管理数据库事务
  • 为移动应用构建后端服务
  • 与现有的基于 MySQL 的系统集成

主要特点

  • 跨平台支持(Android、iOS、macOS、Windows、Linux)
  • 支持 MySQL 和 MariaDB
  • 高效的连接池
  • 交易管理
  • 异步/等待兼容 API

适合对象

  • 需要数据库访问的移动和桌面应用程序
  • 使用 MySQL/MariaDB 的后端服务
  • 开发全栈 Flutter 应用程序的开发者
  • 与现有 MySQL 基础设施集成的 Teams

使用建议

  • 需要网络访问数据库服务器
  • 需要对连接问题进行适当的错误处理
  • 可能需要为 SSL/TLS 进行额外配置
  • 不适用于基于本地文件的数据库

原始 README

以下为关联源码仓库的 README 原文快照,最新内容请访问源码仓库。

mysql1

A MySQL driver for the Dart programming language. Works on Flutter and on the server.

This library aims to provide an easy to use interface to MySQL. mysql1 originated as a fork of the SQLJocky driver.

Usage

Connect to the database

var settings = new ConnectionSettings(
  host: 'localhost', 
  port: 3306,
  user: 'bob',
  password: 'wibble',
  db: 'mydb'
);
var conn = await MySqlConnection.connect(settings);

Execute a query with parameters:

var userId = 1;
var results = await conn.query('select name, email from users where id = ?', [userId]);

Use the results:

for (var row in results) {
  print('Name: ${row[0]}, email: ${row[1]}');
});

Insert some data

var result = await conn.query('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}");

Execute a query with multiple sets of parameters:

var results = await query.queryMulti(
    'insert into users (name, email, age) values (?, ?, ?)',
    [['Bob', 'bob@bob.com', 25],
    ['Bill', 'bill@bill.com', 26],
    ['Joe', 'joe@joe.com', 37]]);

Update some data:

await conn.query(
    'update users set age=? where name=?',
    [26, 'Bob']);

Parameters

Parameters are bound client side. The values are escaped, written into the statement as literals, and the result is sent as a single query - there is no server side prepare/execute/close. This is what mysqlclient does, and so how most MySQL deployments already send their statements.

The reason to do it this way is query planning. A bound value is opaque to the optimizer, so if the leading column of an index is a parameter it takes ref access on that column and never builds a range, and a keyset page can degrade into a scan.

? placeholders inside string literals and inside ` quoted identifiers are left alone. null, int, double, bool, String and DateTime (UTC only, second precision) can be bound, as can Blob and List<int> - both are written as hex literals, so bytes which are not valid in the connection charset survive. Anything else is bound as its toString().

Escaping assumes the connection charset is utf8 or utf8mb4, and that the server is not in NO_BACKSLASH_ESCAPES mode. Both hold for a connection opened by this driver with default settings.

Flutter Web

This package opens a socket to the database. The web platform does not support sockets and so this package does not work on flutter web.

所属分类

依赖

依赖清单 4 项

源码仓库

使用此插件的项目