Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
Paulo V C Medeiros
Merge SQLite DBs
Commits
d3e00b88
Commit
d3e00b88
authored
Oct 19, 2020
by
Paulo V C Medeiros
Browse files
Fix ignored table if not in first input file
parent
2fe3ea0f
Changes
1
Show whitespace changes
Inline
Side-by-side
merge_sqlite_dbs.py
View file @
d3e00b88
...
...
@@ -4,6 +4,7 @@ import shutil
import
sqlite3
from
pathlib
import
Path
import
logging
import
re
def
merge_sqlite_dbs
(
input_files
,
output_file
,
overwrite
):
...
...
@@ -54,9 +55,34 @@ def merge_sqlite_dbs(input_files, output_file, overwrite):
cur
.
execute
(
"SELECT name FROM current_db.sqlite_master WHERE type='table';"
)
all_tables
=
cur
.
fetchall
()
for
table
in
all_tables
:
table_name
=
table
[
0
]
logger
.
debug
(
" > '%s' table"
,
table_name
)
try
:
# Create destination table with same signature as origin if it doesn't exist
# (i) Get sql employed to create the table in the origin DB
cur
.
execute
(
"SELECT sql FROM current_db.sqlite_master WHERE type='table' AND name='%s'"
%
(
table_name
)
)
# (ii) Create the table
create_table_sql
=
re
.
sub
(
"create table"
,
"CREATE TABLE IF NOT EXISTS"
,
cur
.
fetchall
()[
0
][
0
],
flags
=
re
.
IGNORECASE
,
)
cur
.
execute
(
create_table_sql
)
except
IndexError
:
raise
RuntimeError
(
"Could not get sql for creation of table '%s' from input database."
%
(
table_name
)
)
# Append data assuming same columns in the same position
logger
.
debug
(
" > '%s' table"
,
table
)
sql
=
"INSERT INTO {0} SELECT * FROM current_db.[{0}];"
.
format
(
table
[
0
])
sql
=
"INSERT INTO {0} SELECT * FROM current_db.[{0}];"
.
format
(
table_name
)
cur
.
execute
(
sql
)
conn
.
commit
()
...
...
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment