我想您还想将值(插入)到您的字符串中。
我建议创建一个函数(也许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)