I am still a novice in python, but I have the long term goal to create a plugin one day. The first step to this plugin is a test with QGIS python scripts to convert an Access mdb into sqlite db and then to proceed from there.
I found a python script here that will dump an Access mdb into a sqlite db.
The script is normally called from commandline with this patter: python AccessDump.py access.mdb | sqlite3 new.db
I now try to change the script, so i can run it as a QGIS script from the Processing Toolbox, but I really lack the basics so far.
Original script:
#!/usr/bin/env python
#
# AccessDump.py
# A simple script to dump the contents of a Microsoft Access Database.
# It depends upon the mdbtools suite:
# http://sourceforge.net/projects/mdbtools/
import sys, subprocess, os
DATABASE = sys.argv[1]
# Dump the schema for the DB
subprocess.call(["mdb-schema", DATABASE, "mysql"])
# Get the list of table names with "mdb-tables"
table_names = subprocess.Popen(["mdb-tables", "-1", DATABASE],
stdout=subprocess.PIPE).communicate()[0]
tables = table_names.splitlines()
print "BEGIN;" # start a transaction, speeds things up when importing
sys.stdout.flush()
# Dump each table as a CSV file using "mdb-export",
# converting " " in table names to "_" for the CSV filenames.
for table in tables:
if table != '':
subprocess.call(["mdb-export", "-I", "mysql", DATABASE, table])
print "COMMIT;" # end the transaction
sys.stdout.flush()
I already understood the basic mechanism to first read out the table schema of the .mdb for each table, and then export tables content into the newly created db. However, I have no clue how to change the script so that I do not need that | sqlite3 new.db
- part from the original shell command, but integrate it into the script, respectivly change new.db to be the output variable from the script dialog.
##Database=group
##Access.mdb to SqLite=name
##inputmdb= input file
##outputsqlite = output file
subprocess.call()
you need to pipe them back into a variable (take the table names as example) and execute the sql-commands viaexecutescript()
from within python.