Development Tip

MySQL 데이터베이스를 SQLite 데이터베이스로 내보내기

yourdevel 2020. 10. 27. 23:33
반응형

MySQL 데이터베이스를 SQLite 데이터베이스로 내보내기


MySQL 데이터베이스를 SQLite 데이터베이스로 내보내는 데 도움을주세요.


Github 에는 Mysql을 Sqlite3 파일로 변환 하는 환상적인 Linux 쉘 스크립트가 있습니다. 서버에 mysqldump와 sqlite3를 모두 설치해야합니다. 잘 작동합니다.


@quassy가 편집 한 @ user2111698의 대답은 약속대로 작동합니다. 이 작업을 자주 수행하기 때문에 bash 스크립트에 지침을 넣습니다.

#!/bin/bash

mysql_host=localhost
mysql_user=george
mysql_dbname=database
sqlite3_dbname=database.sqlite3

# dump the mysql database to a txt file
mysqldump \
  --skip-create-options \
  --compatible=ansi \
  --skip-extended-insert \
  --compact \
  --single-transaction \
  -h$mysql_host \
  -u$mysql_user \
  -p $mysql_dbname \
  > /tmp/localdb.txt

# remove lines mentioning "PRIMARY KEY" or "KEY"
cat /tmp/localdb.txt \
  | grep -v "PRIMARY KEY" \
  | grep -v KEY \
  > /tmp/localdb.txt.1

# mysqldump leaves trailing commas before closing parentheses  
perl -0pe 's/,\n\)/\)/g' /tmp/localdb.txt.1 > /tmp/localdb.txt.2

# change all \' to ''
sed -e 's/\\'\''/'\'''\''/g' /tmp/localdb.txt.2 > /tmp/localdb.txt.3

if [ -e $sqlite3_dbname ]; then
    mv $sqlite3_dbname $sqlite3_dbname.bak
fi
sqlite3 $sqlite3_dbname < /tmp/localdb.txt.3

자세한 설명이있는 요지는 https://gist.github.com/grfiv/b79ace3656113bcfbd9b7c7da8e9ae8d 에서 찾을 수 있습니다.


상단 게시물에서 언급 한 mysql2sqlite.sh는 PRIMARY KEY 행에 잘 대처하지 못하며 )CREATE 문을 완료하기 위해 후행 작성하지 않습니다 .

이것이 내가 한 일입니다. mysql 덤프를 다음과 같이 실행했습니다.

mysqldump --skip-create-options --compatible=ansi --skip-extended-insert --compact --single-transaction -h<host> -u<user> -p<passwd> <database name> > localdb.txt

그런 다음 grep을 사용하여 PRIMARY KEY 및 KEY를 제거했습니다.

cat localdb.txt | grep -v "PRIMARY KEY' | grep -v KEY > localdb.txt.1

그런 다음 편집기를 사용하여 파일을 수정했습니다. 키가 제거되면 다음과 같은 CREATE 문이 생성됩니다.

CREATE ...
  ...,
)

그 후행 ,을 제거해야합니다. vi에서이 표현식은, $ \ n)과 일치합니다.

그런 다음 모두 \'''

그런 다음 가져 오기를 수행 할 수 있습니다.

sqlite3 local.sqlite3 < localdb.txt.1

그리고 그게 다야. 저에게 효과가있는 단일 프로그램을 찾지 못했습니다. 누군가에게 도움이되기를 바랍니다.


sqlite 데이터베이스에서 테이블 구조를 수동으로 만들었습니다.

다음 명령으로 데이터를 업로드했습니다.

mysqldump -u root {database} {table} --no-create-info --skip-extended-insert  --complete-insert --skip-add-locks  --compatible=ansi | sed "s/\\\'/''/g" |sqlite3 flora.db

두 데이터베이스에서 다른 apex 인코딩을 수정 하기 위해 sed 를 사용해야 했습니다.


There is a fantastic, lightweight tool called SQLite Database Browser that allows you to create and edit sqlite databases. I used it to craete databases for Android apps. You can run SQL statements against it to load up the data so if you export the data from a mySQL database you can just import it using this tool. Here's a link: http://sqlitebrowser.sourceforge.net/


Personally I like the simple usage of mysqldump, yet some adjustments are need (depending on your art with Unix and what you want to do).

Ex. for just one table (prods) with PK:

$ mysqldump mysql prods -u ME -pPASS  --compatible ansi --compact |grep -v "^\/\*" |sqlite3 testme2.db
$ mysqldump mysql prods -u ME -pPASS  --compatible ansi --compact |grep -v "^\/\*" |sqlite3 testme2.db
    Error: near line 1: table "prods" already exists
    Error: near line 7: UNIQUE constraint failed: prods.id, prods.ts
$ sqlite3 testme2.db '.schema'
    CREATE TABLE "prods" (
      "id" varchar(30) NOT NULL DEFAULT '',
      "ts" int(11) NOT NULL DEFAULT '0',
      "val" double DEFAULT NULL,
      PRIMARY KEY ("id","ts")
    );

For more complex things, probably better to write a wrapper, or then, use the already mentioned fantastic awk Linux shell script on Gist .


export the data with

  mysqldump database > database.sql

and import the data with

  sqlite3 database < database.sql

you may need -u (user) and -p (password) options

참고URL : https://stackoverflow.com/questions/5164033/export-a-mysql-database-to-sqlite-database

반응형