Skip to content

Commit

Permalink
Fixed a bug with string quoting.
Browse files Browse the repository at this point in the history
  • Loading branch information
alexanderjfink committed Nov 10, 2013
1 parent 6a347b6 commit a4fae35
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 4 deletions.
27 changes: 24 additions & 3 deletions messy2sql/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,14 @@
Messy2SQL returns SQL strings
"""

import os
import messytables
# Python standard libraries
import os, re
from dateutil import parser

# External libraries
import messytables


"""
Need a slightly different 'dialect' to be spoken depending on which db type we are
wanting to insert into
Expand Down Expand Up @@ -187,7 +191,24 @@ def create_sql_insert(self, rowset, headers=None, table_name=None):
# need to force types to headers
if str(headers[i]) == "String":
if cell.value:
value = '"' + str(cell.value.strip()) + '"'
value = str(cell.value.strip())

Q = '"'
re_quoted_items = re.compile(r'" \s* [^"\s] [^"]* \"')

woqi = re_quoted_items.sub('', value)

if len(value) == 0:
value = Q + value + Q
elif len(woqi) > 0 and not (woqi[0] == Q and woqi[-1] == Q):
value = Q + value + Q

# check start and end of string for quotes
# if not (cell.value[0] == '"' or cell.value[0]) == "'":
# value = '"' + value
# if not (cell.value[-1] == '"' or cell.value[-1]) == "'":
# value = value + '"'

elif "Date" in str(headers[i]):
# Use dateutil parse to turn into a python date time regardless of what it comes in as
if cell.value:
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

setup(
name='messy2sql',
version='0.1.0a9',
version='0.1.0a10',
description="Convert messytables types to SQL create/insert statements",
long_description=long_desc,
classifiers=[
Expand Down

0 comments on commit a4fae35

Please sign in to comment.