Using special characters in MySQL table name with Python's tkinter

74 views Asked by At

I've researched and there are solutions to special characters when dealing with dynamic queries; however, I have yet to come across solutions dealing with both sql and a gui.

Code as follows:

cur.execute("CREATE TABLE IF NOT EXISTS " + entry.get() + " (table values and columns)")

So apparently the above code works with normal entries, but with special characters such as hyphen('-') and whitespace( ), the code will not execute.

entry.get() refers to the a declared entry widget from the python's tkinter module.

Any ideas on how to get around this? Thanks.

1

There are 1 answers

0
Akash senta On

I believe the problem can be avoided by converting all strings to unicode.
You can use as below code for unicode conversion

def get_unicode(widget):
    val = widget.get()
    try:
        val = val.decode('utf-8')
    except UnicodeEncodeError:
        pass
    return val