Created
May 26, 2011 14:09
-
-
Save esperlu/993216 to your computer and use it in GitHub Desktop.
MySQL to Sqlite converter in Lua
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#! /usr/bin/lua | |
-- Usage: mysql2sqlite.lua [data base name] | |
-- $ ./mysql2sqlite.lua my_database | |
-- $ ./mysql2sqlite.lua "my_database my_table" | |
-- $ ./mysql2sqlite | |
-- Set here the path to the temp file you want mysqldump to dump the database into | |
local mysqldump_file = '/tmp/lua.sql' | |
-- Set here your MySQL default database name and MySQL credentials | |
local dbName = arg[1] and arg[1] or 'default database name' | |
local dbUser, dbPw = '****', '***********' | |
-- Various variable initialisation | |
local fh, line, tableName, indexName, indexKey, findUs, replaceBy | |
local previous, keys = nil, {} | |
-- Extracts database from MySQL, remove <--no-data> if you want to extract all INSERT's | |
os.execute('mysqldump --compatible=ansi --no-data --skip-extended-insert --compact -u '..dbUser..' -p'..dbPw..' '..dbName..' > '..mysqldump_file) | |
fh, err = io.open(mysqldump_file, 'r') | |
if not fh then return print(err) end | |
for line in fh:lines() do | |
-- skip comments | |
if not line:find('^/%*') then | |
-- Print all INSERT's replacing `5 o\'clock` by the sqlite escape format: `5 o''clock`, then loop. | |
if line:find('^INSERT', 1) then | |
if line:find("\\'") then line = line:gsub("\\'", "''") end | |
print(line) | |
else | |
-- Remove the KEY declaration from the create block except for the PRIMARY KEY and store it in table for later use | |
-- then loop | |
if line:find('^ KEY') or line:find('^ UNIQUE KEY') or line:find('^ FULLTEXT KEY') and not line:find('PRIMARY') then | |
indexName = line:match('"([^"]+)') | |
indexKey = line:match('%("([^"]+)') | |
table.insert(keys, 'CREATE INDEX "'..tableName..'_'..indexName..'" ON "'..tableName..'"("'..indexKey..'");') | |
else | |
-- Stores table name in variable | |
if line:find('CREATE', 1, true) then | |
tableName = line:match('"([^"]+)', 1) | |
end | |
if line:find(');', 1, true) then | |
previous = previous:gsub(',$', '',1) | |
print(previous, '\n);') | |
previous = nil | |
else | |
if previous then print(previous) end | |
-- Various replacements of MySQL specifics | |
findUs = { | |
'AUTO_INCREMENT','auto_increment', | |
'CHARACTER SET [^ ]+ ','character set [^ ]+ ', | |
'DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP', 'default current_timestamp on update current_timestamp', | |
'COLLATE [^ ]+','collate [^]+', | |
'UNSIGNED','unsigned', | |
'ENUM[^)]+%)', 'enum[^)]+%)', | |
'SET%([^)]+%)', 'set%([^)]+%)' | |
} | |
replaceBy = { '', '', '', '', '', '', '', '', '', '', 'text', 'text','text', 'text'} | |
for k, findMe in pairs(findUs) do | |
line = line:gsub(findMe, replaceBy[k]) | |
end | |
previous = line | |
end | |
end | |
end | |
end | |
end | |
fh:close() | |
for k, v in pairs(keys) do | |
print(v) | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Generalities
This small Lua script should help you to convert a mysqldump file into a Sqlite3 compatible file.
Lua is, surprisingly enough, faster than my awk version of this converter. However, for large files - multi million lines - you could also consider using a csv export from MySQL and the import Sqlite command.
As usual: your mileage may vary but if you have any issue with this snippet, ping me and I'll see what I can do.
Usage
Usual stuff:
$ chmod +x mysql2sqlite.lua
$ /dir/to/mysql2sqlite.lua | sqlite3 newDataBase.sqlite
or$ /dir/to/mysql2sqlite.lua "databaseName tableName" | sqlite3 newDataBase.sqlite
If you only need to create the table structures without data, just change:
Remarks
Ten o\'clock
mysqldump format. It is changed toTen o''clock
varchar()
data typa as Sqlite accepts them in version 3. Read more over Data Typa affinity in the Sqlite doc.AUTO_INCREMENT
is not necessary as it is more or less automatically activated for keys declared as PRIMARY. Read more...enum
andset
data types are not supported in Sqlite. They are therefore converted totext
.Example
Typical mysqldump output:
This will be converted as: