HiveBrain v1.2.0
Get Started
← Back to all entries
patternpythonMinor

MS Access to PostgreSQL converter

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
postgresqlconverteraccess

Problem

I was hoping to get some feedback on the implementation of this class for programmatically converting an access file into a PostgreSQL schema (works by obtaining schema data from the cursor object).

```
import os
import json
import pyodbc
import psycopg2
import time
import sys

# Refer to https://code.google.com/p/pyodbc/wiki/Cursor for information on
# cursor.tables and cursor.columns field names

class Converter:

def __init__(self, access_con_string, pg_con_string, print_SQL):

self.access_cur = pyodbc.connect(access_con_string).cursor()

self.pg_con = psycopg2.connect(pg_con_string)
self.pg_cur = self.pg_con.cursor()

self.print_SQL = print_SQL

self.schema_name = self.get_access_db_name()

def get_access_db_name(self):

# The full path of the database is stored in the table information
# We can parse it to get the file name (to use as scheme_name)
for table in self.access_cur.tables():
return os.path.splitext(os.path.basename(table.table_cat))[0]

def create_schema(self):

SQL = """
CREATE SCHEMA "{schema_name}"
""".format(schema_name=self.schema_name)

if self.print_SQL:
print SQL

self.pg_cur.execute(SQL)
self.pg_con.commit()

def create_tables(self):

# Generate list of tables in schema
table_list = list()
for table in self.access_cur.tables():
if table.table_type == "TABLE":
table_list += [table.table_name, ]

for table in table_list:
SQL = """
CREATE TABLE "{schema}"."{table}"
(
""".format(schema=self.schema_name, table=table)

SQL += self.create_fields(table)

SQL += """
) """

if self.print_SQL:
print SQL

self.pg_cur.execute(SQL)
self.pg_con.commit()

def create_fields(self, table):

postgresql_fiel

Solution

First off, if you're not using Python 3.x, you need to explicitly inherit from object like this: class MyClass(object). If you are using Python 3.x, then you're fine.

If you're going to format your queries based on user input, then the following piece of code may have certain issues, like SQL injection. See this for more information.

SQL = """
CREATE TABLE "{schema}"."{table}"
(
""".format(schema=self.schema_name, table=table)

SQL += self.create_fields(table)

SQL += """
) "


The above block of code can also be shortened to the below:

SQL = "\nCREATE TABLE \"{schema}\".\"{table}\"\n(".format(schema=self.schema_name, table=table)
SQL += self.create_fields(table)
SQL += "\n) "


There are also other places in your code like the code block above the above code block that can be shortened as well.

Finally, you should also add some docstrings to your functions, rather than using inline comments, #. For example:

def my_func( ... ):
    """
    Add a description of your function and
    it's arguments here.
    """
    pass

Code Snippets

SQL = """
CREATE TABLE "{schema}"."{table}"
(
""".format(schema=self.schema_name, table=table)

SQL += self.create_fields(table)

SQL += """
) "
SQL = "\nCREATE TABLE \"{schema}\".\"{table}\"\n(".format(schema=self.schema_name, table=table)
SQL += self.create_fields(table)
SQL += "\n) "
def my_func( ... ):
    """
    Add a description of your function and
    it's arguments here.
    """
    pass

Context

StackExchange Code Review Q#83800, answer score: 5

Revisions (0)

No revisions yet.