跳到正文
IntoSQL 首页

将 SQLite 转换为 MariaDB

拖入 SQLite 数据库文件(.db、.sqlite、.sqlite3)或 sqlite3 的 .dump 写出的 SQL,即可下载包含相同表、键、索引和数据的 MariaDB 脚本。每列获得其值都符合的类型,MariaDB 无法原样存储的每个值都会在报告中列出。

你的文件
sqlshop_2024_sqlite.sql示例文件,3.4 kB
你的数据库

已转换 18 行,无修正,无拒绝

显示

shop_2024_sqlite.sql 中的表SQLite 转储 · 由 sqlite3 写出 · utf-8

  • customers8 行
    7 列
    idbigint not null
    nametext not null
    emailvarchar(768) not null
    countrytext not null
    is_activetinyint(1) not null
    signed_update
    created_atdatetime not null
  • orders10 行
    9 列
    idbigint not null
    customer_idbigint not null
    statusvarchar(768) not null
    totaldecimal(10,2) not null
    itemsint not null
    ordered_atdatetime not null
    shipped_atdatetime
    notetext
    receiptblob

未转换

  • 视图 order_totals
  • 触发器 orders_shipped

每种数据库都用自己的语言编写这些对象。请为 MariaDB 重写它们。

-- Written by IntoSQL from a SQLite dump. Load it with: mariadb --default-character-set=utf8mb4 <database> < <file>
SET NAMES utf8mb4;
SET FOREIGN_KEY_CHECKS = 0;
SET UNIQUE_CHECKS = 0;
SET SQL_MODE = 'NO_AUTO_VALUE_ON_ZERO,ALLOW_INVALID_DATES';

CREATE TABLE `customers` (
  `id` bigint NOT NULL AUTO_INCREMENT,
  `name` text NOT NULL,
  `email` varchar(768) NOT NULL,
  `country` text NOT NULL DEFAULT 'US',
  `is_active` tinyint(1) NOT NULL DEFAULT 1,
  `signed_up` date,
  `created_at` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,
  PRIMARY KEY (`id`),
  UNIQUE KEY `email` (`email`),
  CONSTRAINT `check1` CHECK (char_length(`country`) = 2)
) AUTO_INCREMENT=9 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin;

CREATE TABLE `orders` (
  `id` bigint NOT NULL AUTO_INCREMENT,
  `customer_id` bigint NOT NULL,
  `status` varchar(768) NOT NULL DEFAULT 'pending',
  `total` decimal(10,2) NOT NULL,
  `items` int NOT NULL DEFAULT 1,
  `ordered_at` datetime NOT NULL,
  `shipped_at` datetime,
  `note` text,
  `receipt` blob,
  PRIMARY KEY (`id`),
  KEY `idx_orders_customer` (`customer_id`),
  KEY `idx_orders_status_date` (`status`,`ordered_at`),
  CONSTRAINT `check1` CHECK (`status` IN ('pending', 'paid', 'shipped', 'cancelled')),
  FOREIGN KEY (`customer_id`) REFERENCES `customers` (`id`) ON DELETE CASCADE
) AUTO_INCREMENT=11 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin;

INSERT INTO `customers` (`id`,`name`,`email`,`country`,`is_active`,`signed_up`,`created_at`) VALUES (1,'Ana Martínez','ana@example.com','MX',1,'2023-01-15','2023-01-15 10:24:00');
INSERT INTO `customers` (`id`,`name`,`email`,`country`,`is_active`,`signed_up`,`created_at`) VALUES (2,'Zoë Müller','zoe@example.de','DE',1,'2023-02-03','2023-02-03 08:11:42');
INSERT INTO `customers` (`id`,`name`,`email`,`country`,`is_active`,`signed_up`,`created_at`) VALUES (3,'José Peña','jose@example.es','ES',0,'2023-03-21','2023-03-21 19:05:17');
INSERT INTO `customers` (`id`,`name`,`email`,`country`,`is_active`,`signed_up`,`created_at`) VALUES (4,'李雷','lilei@example.cn','CN',1,'2023-05-09','2023-05-09 02:40:00');
INSERT INTO `customers` (`id`,`name`,`email`,`country`,`is_active`,`signed_up`,`created_at`) VALUES (5,'Noah O\'Brien','noah@example.ie','IE',1,NULL,'2023-06-30 14:00:00');
INSERT INTO `customers` (`id`,`name`,`email`,`country`,`is_active`,`signed_up`,`created_at`) VALUES (6,'Amélie Laurent','amelie@example.fr','FR',1,'2024-01-02','2024-01-02 09:30:00');
INSERT INTO `customers` (`id`,`name`,`email`,`country`,`is_active`,`signed_up`,`created_at`) VALUES (7,'Ravi Patel','ravi@example.in','IN',0,'2024-02-29','2024-02-29 23:59:59');
INSERT INTO `customers` (`id`,`name`,`email`,`country`,`is_active`,`signed_up`,`created_at`) VALUES (8,'Sofia Rossi 🌻','sofia@example.it','IT',1,'2024-04-12','2024-04-12 12:12:12');
INSERT INTO `orders` (`id`,`customer_id`,`status`,`total`,`items`,`ordered_at`,`shipped_at`,`note`,`receipt`) VALUES (1,1,'shipped',129.9,3,'2024-03-01 10:00:00','2024-03-02 16:30:00',NULL,NULL);
INSERT INTO `orders` (`id`,`customer_id`,`status`,`total`,`items`,`ordered_at`,`shipped_at`,`note`,`receipt`) VALUES (2,2,'paid',54,1,'2024-03-02 11:15:00',NULL,'Gift wrap 🎁',NULL);
INSERT INTO `orders` (`id`,`customer_id`,`status`,`total`,`items`,`ordered_at`,`shipped_at`,`note`,`receipt`) VALUES (3,1,'cancelled',12.5,1,'2024-03-03 09:45:00',NULL,'Customer changed their mind',NULL);
INSERT INTO `orders` (`id`,`customer_id`,`status`,`total`,`items`,`ordered_at`,`shipped_at`,`note`,`receipt`) VALUES (4,4,'shipped',980,12,'2024-03-05 02:10:00','2024-03-07 08:00:00','请小心轻放',X'25504446');
INSERT INTO `orders` (`id`,`customer_id`,`status`,`total`,`items`,`ordered_at`,`shipped_at`,`note`,`receipt`) VALUES (5,5,'pending',45.75,2,'2024-03-08 17:20:00',NULL,'Leave at the door;\nring twice',NULL);
INSERT INTO `orders` (`id`,`customer_id`,`status`,`total`,`items`,`ordered_at`,`shipped_at`,`note`,`receipt`) VALUES (6,6,'shipped',310.4,5,'2024-03-10 13:00:00','2024-03-11 09:00:00','Fragile: "glass"',NULL);
INSERT INTO `orders` (`id`,`customer_id`,`status`,`total`,`items`,`ordered_at`,`shipped_at`,`note`,`receipt`) VALUES (7,8,'paid',76.2,2,'2024-03-11 18:45:00',NULL,NULL,NULL);
INSERT INTO `orders` (`id`,`customer_id`,`status`,`total`,`items`,`ordered_at`,`shipped_at`,`note`,`receipt`) VALUES (8,2,'paid',19.99,1,'2024-03-12 08:00:00',NULL,'Paid at the counter',NULL);
INSERT INTO `orders` (`id`,`customer_id`,`status`,`total`,`items`,`ordered_at`,`shipped_at`,`note`,`receipt`) VALUES (9,3,'shipped',64,2,'2024-03-14 15:30:00',NULL,'Shipping date never recorded',NULL);
INSERT INTO `orders` (`id`,`customer_id`,`status`,`total`,`items`,`ordered_at`,`shipped_at`,`note`,`receipt`) VALUES (10,7,'paid',1499,1,'2024-03-15 21:05:00',NULL,'C:\\Users\\ravi\\invoice.pdf',NULL);

SET UNIQUE_CHECKS = 1;
SET FOREIGN_KEY_CHECKS = 1;

修正

没有需要修正的内容。

需要检查

  • 源数据中有 1 个文本列比较时不区分大小写;MariaDB 精确比较,因此唯一键和搜索的行为可能不同。 在需要的列上使用不区分大小写的排序规则或列类型。
  • 2 个视图、触发器、例程或事件未转换;报告逐一列出了它们。 请在 MariaDB 中手动重新创建它们。

文件在这台电脑上转换,永远不会上传。

其他示例:

从 SQLite 到 MariaDB 的变化

每一行是转换器应用的一条规则,以及该规则更改你的数据时在报告中写入的记录。

  • declared types (any text: VARCHAR(255), DATETIME, BOOLEAN, JSON or none)

    列中每个值都符合的类型

    SQLite 允许任何列存放任何值,因此声明的类型只是提示:每列获得其所有值都符合的类型,包含混合值的列保留其文本。

  • BOOLEAN, BOOL

    tinyint(1)

    当每个值都是 0 或 1 时;同时包含其他值的列会将它们保留为数字或文本。

  • TEXT, VARCHAR(n), CHAR(n), CLOB

    不超过 1024 个字符时为 varchar(n),超过则为 text 类型

    SQLite 从不强制长度;只有当每个值都符合时,才保留声明的长度。

  • DECIMAL(p,s), NUMERIC

    每个值都符合时为精确类型,否则为 double

    SQLite 将这些值存储为整数或 double;只有当值符合时,才保留声明的精度。

  • BLOB, and columns holding bytes

    blob 类型

    字节保持为字节;同一列中的文本和数字按其字节保留。

  • DATE, DATETIME, TIMESTAMP holding ISO 8601 text

    date、datetime(p)

    当每个值都是日期或日期加时间时;纪元时间数字和其他文本保持原值不变。

  • JSON holding JSON text

    json

    当每个值都是 JSON 对象或数组时。

  • Infinity in REAL columns

    NULL

    MariaDB 的 double 没有无穷大,因此这些值变为 NULL,报告会统计其数量。

    报告记录: invalid_value_null

  • INTEGER PRIMARY KEY, AUTOINCREMENT

    AUTO_INCREMENT,从最大的 id 或 sqlite_sequence 的值之后继续

    只有声明恰好为 INTEGER PRIMARY KEY 的列才是 rowid;每一行都保留其 id,计数器从曾经分配过的最大 id 之后开始。

  • COLLATE NOCASE

    精确比较(utf8mb4_bin)

    表使用 utf8mb4_bin,因此文本像 SQLite 默认那样精确比较;NOCASE 列不再忽略大小写。

    报告记录: case_insensitive_collation

  • generated columns

    生成列,与源数据一样为 STORED 或 VIRTUAL

    MariaDB 以相同方式计算它们。

  • generated columns with functions IntoSQL doesn't write, and the || operator

    普通列

    无法写出的公式会留下一个普通列和一条报告记录;转储中没有生成列的值。

    报告记录: generated_not_translated

  • defaults with functions IntoSQL doesn't write (strftime, randomblob)

    无默认值

    无法写出的默认值会被省略,并在报告中列出;CURRENT_TIMESTAMP 和 datetime('now') 会被写出。

    报告记录: default_not_translated

  • CHECK constraints

    已转换

    检查约束相同,在导入每一行时检查。

  • CHECK constraints with functions IntoSQL doesn't write

    不创建

    无法写出的检查约束会被省略,并在报告中列出。

    报告记录: check_skipped

  • partial and expression indexes

    不创建

    IntoSQL 在整列上写出索引;其他索引请在导入后重新创建。

    报告记录: index_skipped

  • foreign keys (unenforced unless PRAGMA foreign_keys=ON)

    外键,以 FOREIGN_KEY_CHECKS=0 导入

    SQLite 只在被要求时才检查外键,因此孤立行很常见:它们会被导入,外键保持未检查状态并给出警告,而不是中止导入。

  • foreign keys to tables the dump doesn't create

    不创建

    外键无法引用不存在的表。

    报告记录: foreign_key_skipped

  • primary keys holding NULL, and NOT NULL columns with values that become NULL

    去除 NOT NULL

    SQLite 允许 rowid 以外的主键包含 NULL;该列在目标数据库中不能为 NOT NULL,否则导入会中止。

    报告记录: not_null_relaxed

  • views, triggers and virtual tables (FTS, R*Tree)

    在报告中列出

    它们的代码用 SQLite 自己的语言编写,虚拟表需要 SQLite 的模块;它们会被列出,由你重新创建。它们的内部表不会被复制。

    报告记录: not_converted_objects

  • dumps without CREATE TABLE (--data-only)

    根据值推断类型

    只含数据的转储只有行,因此类型来自值,列按编号命名。

    报告记录: no_schema

  • expressions in INSERT values other than the text functions dumps write

    NULL

    unistr()、replace()、char() 和 || 会按 shell 写出的形式求值;其他表达式在没有数据库时无法求值。

    报告记录: expression_value

  • NUL characters in text

    保留

    sqlite3 shell 和 Python 在转储时会在 NUL 字符处截断文本,因此转储中只有它之前的内容。

  • text that isn't valid UTF-8

    错误的字节变为替换字符

    转储会将这类文本写为原始字节;脚本是 UTF-8。

    报告记录: invalid_utf8_replaced

  • text decoded twice (ñ)

    按原样复制

    转储中存储的正是这些字符,因此按原样复制;报告会指出它们。

    报告记录: mojibake_suspected

在 SQLite 中在 MariaDB 中报告记录
declared types (any text: VARCHAR(255), DATETIME, BOOLEAN, JSON or none)列中每个值都符合的类型SQLite 允许任何列存放任何值,因此声明的类型只是提示:每列获得其所有值都符合的类型,包含混合值的列保留其文本。
BOOLEAN, BOOLtinyint(1)当每个值都是 0 或 1 时;同时包含其他值的列会将它们保留为数字或文本。
TEXT, VARCHAR(n), CHAR(n), CLOB不超过 1024 个字符时为 varchar(n),超过则为 text 类型SQLite 从不强制长度;只有当每个值都符合时,才保留声明的长度。
DECIMAL(p,s), NUMERIC每个值都符合时为精确类型,否则为 doubleSQLite 将这些值存储为整数或 double;只有当值符合时,才保留声明的精度。
BLOB, and columns holding bytesblob 类型字节保持为字节;同一列中的文本和数字按其字节保留。
DATE, DATETIME, TIMESTAMP holding ISO 8601 textdate、datetime(p)当每个值都是日期或日期加时间时;纪元时间数字和其他文本保持原值不变。
JSON holding JSON textjson当每个值都是 JSON 对象或数组时。
Infinity in REAL columnsNULLMariaDB 的 double 没有无穷大,因此这些值变为 NULL,报告会统计其数量。invalid_value_null
INTEGER PRIMARY KEY, AUTOINCREMENTAUTO_INCREMENT,从最大的 id 或 sqlite_sequence 的值之后继续只有声明恰好为 INTEGER PRIMARY KEY 的列才是 rowid;每一行都保留其 id,计数器从曾经分配过的最大 id 之后开始。
COLLATE NOCASE精确比较(utf8mb4_bin)表使用 utf8mb4_bin,因此文本像 SQLite 默认那样精确比较;NOCASE 列不再忽略大小写。case_insensitive_collation
generated columns生成列,与源数据一样为 STORED 或 VIRTUALMariaDB 以相同方式计算它们。
generated columns with functions IntoSQL doesn't write, and the || operator普通列无法写出的公式会留下一个普通列和一条报告记录;转储中没有生成列的值。generated_not_translated
defaults with functions IntoSQL doesn't write (strftime, randomblob)无默认值无法写出的默认值会被省略,并在报告中列出;CURRENT_TIMESTAMP 和 datetime('now') 会被写出。default_not_translated
CHECK constraints已转换检查约束相同,在导入每一行时检查。
CHECK constraints with functions IntoSQL doesn't write不创建无法写出的检查约束会被省略,并在报告中列出。check_skipped
partial and expression indexes不创建IntoSQL 在整列上写出索引;其他索引请在导入后重新创建。index_skipped
foreign keys (unenforced unless PRAGMA foreign_keys=ON)外键,以 FOREIGN_KEY_CHECKS=0 导入SQLite 只在被要求时才检查外键,因此孤立行很常见:它们会被导入,外键保持未检查状态并给出警告,而不是中止导入。
foreign keys to tables the dump doesn't create不创建外键无法引用不存在的表。foreign_key_skipped
primary keys holding NULL, and NOT NULL columns with values that become NULL去除 NOT NULLSQLite 允许 rowid 以外的主键包含 NULL;该列在目标数据库中不能为 NOT NULL,否则导入会中止。not_null_relaxed
views, triggers and virtual tables (FTS, R*Tree)在报告中列出它们的代码用 SQLite 自己的语言编写,虚拟表需要 SQLite 的模块;它们会被列出,由你重新创建。它们的内部表不会被复制。not_converted_objects
dumps without CREATE TABLE (--data-only)根据值推断类型只含数据的转储只有行,因此类型来自值,列按编号命名。no_schema
expressions in INSERT values other than the text functions dumps writeNULLunistr()、replace()、char() 和 || 会按 shell 写出的形式求值;其他表达式在没有数据库时无法求值。expression_value
NUL characters in text保留sqlite3 shell 和 Python 在转储时会在 NUL 字符处截断文本,因此转储中只有它之前的内容。
text that isn't valid UTF-8错误的字节变为替换字符转储会将这类文本写为原始字节;脚本是 UTF-8。invalid_utf8_replaced
text decoded twice (ñ)按原样复制转储中存储的正是这些字符,因此按原样复制;报告会指出它们。mojibake_suspected

将脚本导入 MariaDB

脚本先创建表、复制行,再添加键、索引和外键,因此导入速度快,并会在遇到第一个错误时停止。

mariadb --default-character-set=utf8mb4 mydb < sqlite.mariadb.sql

外键按转储中的声明创建,导入行时使用 FOREIGN_KEY_CHECKS=0,与 mysqldump 的做法相同。

对于较大的转储,请在服务器所在的机器上运行客户端;如果单行非常大,请调高 max_allowed_packet。

IntoSQL Pro 可以通过桌面应用或命令行将文件直接导入 MariaDB,无需生成脚本。

如何转储 SQLite 以获得最佳结果

转换器读取转储中的内容。以下选项能让转储保留更多数据:

  • 数据库文件本身会原样转换,在浏览器中逐页读取。请先关闭正在写入它的应用,以免有更改遗留在其 -wal 文件中。
  • sqlite3 app.db .dump > app.sql 会将同一个数据库写为 SQL,它同样可以转换,格式为 .sql 或 .sql.gz。
  • DB Browser for SQLite 通过 File、Export、Database to SQL file 也能写出该 SQL,Python 的 iterdump() 也一样。

哪些内容不会转换

视图和触发器是用 SQLite 自己的语言编写的。它们会按名称在报告中列出,绝不会被悄悄丢弃,以便你为 MariaDB 重写。FTS5 和 R*Tree 等虚拟表也会列出,其影子表则不包含在内。

关于 SQLite 转 MariaDB 的问题

这里没有你要的答案?请通过联系页面给我们留言,会有专人回复。 联系页面

我的转储会被上传吗?

不会。转换器在你的浏览器中运行,分块从磁盘读取转储,因此转储不会离开你的电脑。桌面应用和命令行也在你的电脑上转换。

转储可以多大?

转换器从不把整个转储放进内存:它按范围读取文件两次,边读边写出脚本。几 GB 的转储可以在浏览器、桌面应用和命令行中转换。

可以直接转换 .db 文件吗?

可以。拖入 .db、.sqlite 或 .sqlite3 文件:它在浏览器中读取,绝不会上传。含 NUL 字符的文本和 double 值的每一位数字都会按存储原样保留,而 .dump 可能会将其截断。

转储可以来自哪些 SQLite 版本?

SQLite 3.0 及更高版本的 UTF-8 或 UTF-16 数据库文件,以及由 sqlite3 shell 的 .dump 或 Python 的 iterdump() 写出的转储。每个测试文件和转储都来自 sqlite3 3.31 到 3.54,包括 STRICT 和 WITHOUT ROWID 表,并与其源数据逐个单元格比较。

SQLite 不强制类型时,列类型如何确定?

声明的类型只是提示,由值决定:当每个值都符合时,列会变为 integer、decimal、double、date、datetime、tinyint(1)、blob 或 text 类型。包含混合值的列保留其文本,报告会指出是哪些列。

INTEGER PRIMARY KEY 和 AUTOINCREMENT 会保留吗?

会。INTEGER PRIMARY KEY 变为 AUTO_INCREMENT,每一行都保留其 id,下一个 id 从最大的 id 之后继续;使用 AUTOINCREMENT 时则从 sqlite_sequence 的值之后继续。

视图和触发器怎么处理?

它们会按名称在报告中列出,但不会转换,因为每种数据库都用自己的语言编写它们。

脚本可以导入哪些 MariaDB 版本?

MariaDB 10.11 及更高版本。每个测试转储都能在 10.11 和 11.4 上导入,如有问题会在第一个错误处停止。