Python如何查看实际的Ms SQL查询被激发

Python如何查看实际的Ms SQL查询被激发,python,sql,sql-server,pymssql,Python,Sql,Sql Server,Pymssql,我知道MySQL和Postgres,但不知道MsSQL?我该怎么做 在 它返回'pyodbc.Cursor'对象没有属性“query”不确定这是最好的方法,但可能是最简单的方法。只需在执行前打印查询 queryText=“插入bla bla bla” 打印查询文本 cursor.execute(queryText)我想您也希望将值(插入)到字符串中 我建议创建一个函数(可能是verboseQuery()),如下所示: def verboseQuery(query, *data): # S

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


它返回
'pyodbc.Cursor'对象没有属性“query”

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

queryText=“插入bla bla bla”

打印查询文本


cursor.execute(queryText)

我想您也希望将值(插入)到字符串中

我建议创建一个函数(可能是
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)
然后,您可以执行以下操作:

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)
这将打印:(如果
数据=[0,1,2,3,4,5,6,7]