denisenkom / pytds

Python DBAPI driver for MSSQL using pure Python TDS (Tabular Data Stream) protocol implementation
MIT License
190 stars 52 forks source link

space in column name #130

Closed m32 closed 8 months ago

m32 commented 2 years ago

I know it's a bad idea to have a space in the column name, but others allow it.

import pytds

table_name = "issue_7"
with pytds.connect('127.0.0.1', 'testing', 'sa', 'SaAdmin1@') as conn:
    with conn.cursor() as cur:
        sql = f"DROP TABLE IF EXISTS {table_name}"
        cur.execute(sql)
        sql = f"CREATE TABLE {table_name} (id int primary key, [my column] int)"
        cur.execute(sql)

        sql = "INSERT INTO issue_7 (id, [my column]) VALUES (%(id)s, %(my column)s)"
        parameters = {'id': 1, 'my column': 123}
        cur.execute(sql, parameters)
WinstonKyu commented 2 years ago

thanks for your updates. some of us are handed down these databases that were done with spaces which makes all aspects of development difficult.

denisenkom commented 9 months ago

It is not clear to me why this is needed. Name of the parameter does not need to match name of the column, for example you can do this:

sql = "INSERT INTO issue_7 (id, [my column]) VALUES (%(id)s, %(my_column)s)"
parameters = {'id': 1, 'my_column': 123}
cur.execute(sql, parameters)

or this:

sql = "INSERT INTO issue_7 (id, [my column]) VALUES (%s, %s)"
cur.execute(sql, (1, 123))
m32 commented 9 months ago

It is not clear to me why this is needed. Name of the parameter does not need to match name of the column, for example you can do this:

sql = "INSERT INTO issue_7 (id, [my column]) VALUES (%(id)s, %(my_column)s)"
parameters = {'id': 1, 'my_column': 123}
cur.execute(sql, parameters)

or this:

sql = "INSERT INTO issue_7 (id, [my column]) VALUES (%s, %s)"
cur.execute(sql, (1, 123))

there is no problem when writing the query manually, but if you read the database structure and prepare a dictionary, there may be spaces in the names and column names are created from dictionary keys