Python-如何查看实际发送的Ms-SQL查询

时间:2014-08-14 11:34:59

标签: python sql sql-server pymssql

我知道MySQL和Postgres但不是MsSQL吗?我该怎么做?

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])
print cursor.query

返回'pyodbc.Cursor' object has no attribute 'query'

2 个答案:

答案 0 :(得分:0)

不确定这是最好的方法,但也许是最简单的方法。只需在执行前打印您的查询。

queryTxt =“插入bla bla bla”

print queryTxt

cursor.execute(queryTxt)

答案 1 :(得分:0)

我想你也希望将值(插入)放入你的字符串中。

我建议创建一个函数(可能是verboseQuery()),如下所示:

def verboseQuery(query, *data):
    # See if the data matches up
    if(len(data) == query.count('?')):
        count = 0
        while(count != len(data)):
            # Replace the first occurrence of '?' with the first data point
            query = query.replace('?', str(data[count]), 1)
            count += 1

    # If the data did not match the query, return the original query,
    # otherwise, return the query we modified earlier
    return query

然后,您可以执行以下操作:

query = verboseQuery("insert into data_AutoScale_DELHUB(AWB_Number,Weight,Length,Width,Height,Customer_Name,Scan_Time,Series_F ag) VALUES (?, ?, ?, ?, ?, ?, ?, ?)" , data[0],data[1],data[2],data[3],data[4],data[5],data[6],data[7])
print query
cursor.execute(query)

将打印:(如果data = [0, 1, 2, 3, 4, 5, 6, 7]

insert into data_AutoScale_DELHUB(AWB_Number,Weight,Length,Width,Height,Customer_Name,Scan_Time,Series_F ag) VALUES (0, 1, 2, 3, 4, 5, 6, 7)
相关问题