python如何查看实际的MsSQL查询

2024-04-19 14:07:55 发布

您现在位置:Python中文网/ 问答频道 /正文

我知道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'


Tags: numberexecutedatamysqlpostgresquerylengthcursor
2条回答

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

queryText=“插入bla bla bla”

打印查询文本

你知道吗游标.执行(查询文本)

我想您还希望将值(插入)到字符串中。你知道吗

我建议创建一个函数(也许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)

相关问题 更多 >