Python-Data truncated and spaces are added between data while inserting into MsSQL

亡梦爱人 提交于 2019-12-12 04:07:33

问题


Upon inserting the data into the MS SQL Server DB, my values are getting truncated and single extra space is added in between. For eg:-

HomeShop18 is saved as H o m e s. It is truncated as well and space is also included.

But when I do select * from table where col= 'Homes' it shows the data. What is the problem? How can I insert the data into the SQL Server DB?

Here's what I'm currently doing? PS : I have read threads on SO pointing out to increase the size and TDS version. I've tried it all but still the inserting entry is truncated.

def post_data(data):
    os.environ['TDSVER'] = '8.0'

    data = (
        data['AWB_Number'], data['Weight'], data['Length'], data['Height'],
        data['Width'], data['Customer_Name'], datetime.datetime.strptime(data['Scan_Time'][:19] , '%Y-%m-%d %H:%M:%S'),data['Series_Flag']
        )

    print data # ('40642847766', '0.011', '1.1', '1.1', '1.1', 'flipkart', datetime.datetime(2014, 8, 14, 11, 14, 53), 'True')
con_string = 'DSN=%s;UID=%s;PWD=%s;DATABASE=%s;' % (dsn, user, password, database)
    cnxn = pyodbc.connect(con_string)

    cursor = cnxn.cursor()
    cursor.execute("insert into data_AutoScale_DELHUB(AWB_Number,Weight,Length,Width,Height,Customer_Name,Scan_Time,Series_Flag) VALUES (?, ?, ?, ?, ?, ?, ?, ?)" , data[0],data[1],data[2],data[3],data[4],data[5],data[6],data[7])
    cursor.commit()

    cnxn.close()

回答1:


Be careful of the order of parameters. It seems 'Height' and 'Width' are inverted in the query and in the parameters.

Check if the order of your parameters in execute() is the same as

(AWB_Number,Weight,Length,Width,Height,Customer_Name,Scan_Time,Series_Flag)


来源:https://stackoverflow.com/questions/25308351/python-data-truncated-and-spaces-are-added-between-data-while-inserting-into-mss

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!