0

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

4

2 回答 2

0

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

queryTxt = "插入 bla bla bla"

打印查询Txt

cursor.execute(queryTxt)

于 2014-08-14T11:49:47.280 回答
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)
于 2014-08-15T12:07:04.747 回答