A collection of possible problems when migrating sqlite3 to mysql

A collection of possible problems when migrating sqlite3 to mysql

Brief description

Suitable for readers: Mobile development

There are many syntax problems when migrating sqlite3 data to mysql. It is impossible to list all the problems at once. This article will try to list as many problems as possible, because everyone encounters different problems. Readers can use this article to predict in advance whether they will face any problems. Knowing what to expect can help avoid or reduce unexpected problems that may occur after migrating data. According to the list of problems, you can understand what problems you need to solve. I will also provide a solution for your reference. There is no one-size-fits-all solution to the migration problem, because everyone encounters different problems. If some conflicting problems exist in the solution at the same time, it will become a problem.

Okay, let’s get straight to the point!

Question List

1. Various variables dumped by sqlite3 are not recognized in mysql, such as (BEGIN TRANSACTION, COMMIT, etc.)

2. The sqlite database data cannot export the hidden field rowid

3. The data export format of sqlite database is incompatible with other databases, such as single quotes and double quotes

4. The exported sqlite data does not have column names, as shown in INSERT INTO protocol VALUES('大類頁(新)', ' ') . What we may need is INSERT INTO protocol('text1','text2') VALUES('大類頁(新)', '刷新頁')

5. Special symbol processing, such as escape character "\"

6. Table field length limits are different

7. Data writing efficiency

Problem Solving

First of all, you cannot use tools such as sqliteStudio and Navicat. Here, you can use shell commands to directly avoid content generation.

BEGIN TRANSACTION

COMMIT

CREATE UNIQUE INDEX

PRAGMA foreign_keys=OFF

At the same time, using the shell method can be implemented with very little code.

1. Solve the problem that the field column name is not output

Running the sqlite3 command " pragma table_info(表名); " yields the following output

0|name|TEXT|0||0

1|description|TEXT|0||0

Then get the column name through string replacement, as follows:

Assume the variables are COLS = name, description

2. Solve the problem of single quotes and double quotes

Running the sqlite3 command " .mode insert && .dump 表名" yields the following output

INSERT INTO table name VALUES ('test',' test');

INSERT INTO table name VALUES ('test','test');

INSERT INTO table name VALUES ('test','test');

This method can solve the problem of single quotes and double quotes. Here we directly output single quotes uniformly.

3. Solve the problem that the default field rowid of sqlite3 cannot be displayed. Here, directly change rowid to id

Set the current mode to .dump insert mode

Running the sqlite3 command " select rowid as id,$COLS from 表名" yields the following output

INSERT INTO table name VALUES (1,'test',' test');

INSERT INTO table name VALUES (2,'test','test');

INSERT INTO table name VALUES (3,'test','test');

4. Use the shell string command to add the previously obtained column names to the following SQL statement

After modification, it is as follows:

INSERT INTO table name ('name',' description') VALUES (1,'test',' test');

INSERT INTO table name('name',' description') VALUES(2,'test','test');

INSERT INTO table name('name',' description') VALUES(3,'test','test');

5. Escape character processing

If there are escape characters in the data in the database, such as: {\"lastname\":\\"天津\\"} . If this type of data is not processed, it will become {"lastname":\"天津\"} when inserted into the database. So we need to process the escape character. It is very simple to process it with shell command, such as command: sed 's#\\#\\\\#g'
Solution Implementation

This script mainly solves the above 1~5 problems. You can modify the script as needed.

 #!/bin/sh
 SQLITE=sqlite3
 if [ -z "$1" ] ; then
   echo usage: $0 sqlite3.db
   exit
 fi
 DB="$1"
 TABLES=`"$SQLITE" "$DB" .tables`
 for TABLE in $TABLES ; do
   CREATE=`"$SQLITE" "$DB" "SELECT sql FROM sqlite_master WHERE type=\"table\" AND name = \"$TABLE\";"`
   echo $CREATE";" |
   cut -d'=' -f2 |
   sed "s/^CREATE TABLE $TABLE (/CREATE TABLE $TABLE (id int auto_increment primary key ,/g"
   COLS=`"$SQLITE" "$DB" "pragma table_info($TABLE)" | cut -d'|' -f2 `
   COLS_CS=`echo $COLS | sed 's/ /,/g'`
   echo ".mode insert \n.header on \n select rowid as id,$COLS_CS from $TABLE;\n" |
   "$SQLITE" "$DB" |
   sed "s/^INSERT INTO \"table\"/INSERT INTO $TABLE /g" |
   sed 's#\\#\\\\#g'
 done

Summarize

The above is the full content of this article. I hope that the content of this article will have certain reference learning value for your study or work. Thank you for your support of 123WORDPRESS.COM.

You may also be interested in:
  • Migrating Ghost from SQLite3 database to MySQL database

<<:  Detailed explanation of linux crm deployment code

>>:  Element Plus implements Affix

Recommend

How to use mysqldump for full and point-in-time backups

Mysqldump is used for logical backup in MySQL. Al...

How to wrap HTML title attribute

When I was writing a program a few days ago, I wan...

Vue implements zip file download

This article example shares the specific code of ...

MySQL master-slave replication delay causes and solutions

Table of contents A brief overview of the replica...

An example of implementing a simple finger click animation with CSS3 Animation

This article mainly introduces an example of impl...

Detailed code for adding electron to the vue project

1. Add in package.json "main": "el...

Flame animation implemented with CSS3

Achieve results Implementation Code html <div ...

Detailed steps for adding hosts you need to monitor in zabbix

Add monitoring host Host 192.168.179.104 is added...

Prototype and prototype chain prototype and proto details

Table of contents 1. Prototype 2. Prototype chain...

How to deploy FastDFS in Docker

Install fastdfs on Docker Mount directory -v /e/f...

Vue dynamic menu, dynamic route loading and refresh pitfalls

Table of contents need: Ideas: lesson: Share the ...

Record the whole process of MySQL master-slave configuration based on Linux

mysql master-slave configuration 1. Preparation H...

Vue3.0 uses the vue-grid-layout plug-in to implement drag layout

Table of contents 1. Plugins 2. Interlude 3. Impl...

64-bit CentOs7 source code installation mysql-5.6.35 process sharing

First install the dependent packages to avoid pro...

Tutorial on installing GreasyFork js script on mobile phone

Table of contents Preface 1. Iceraven Browser (Fi...