我正在尝试使用序列号从2个单独的数据库中返回数据(这2个数据库对相同的物理部件使用不同的序列号格式(其中一个部件号位于实际序列号之前),因此现在我正在使用2个输入,直到我可以确认查询正常运行为止。我能够使用单个序列执行整个脚本,但是复制它只能在我尝试多个序列时读取字符串中的第一个序列。更改一些内容后,如果将输入粘贴到mySQL工作台中,则输入似乎是正确的,但脚本由于某种原因将无法读取它们。由于我在python上是新手,所以出了什么问题在我的联盟中有些出入。
我到处都在寻找答案,但是大多数人都没有使用来自用户输入的列表,所以我对如何使用该信息应用于当前问题感到困惑
import mysql.connector
import xlsxwriter
import datetime
ogserials = input_string = input('Paste QR serials now')
ogserials = input_string.split(",")
newserials = input_string = input('paste CT serials now')
newserials = input_string.split(",")
print(ogserials)
print(newserials)
mos_cnx = mysql.connector.connect(user='User',
password='Password,
host='host.com,
port=1234,
database='schema')
mos_cursor = mos_cnx.cursor()
mos_query = ("""
SELECT
t1.serial,
t2.testparametername,
t3.parameterresult,
t3.testvalue
FROM
t3
JOIN
t1 ON t3.t1id = t1.id
join t2 on t2.id= t3.t2id
WHERE
t3.parameterid = '6358150'
and t1.serial in (%s)
""")
mos_cursor.executemany(mos_query, (ogserials,))
mos_results = mos_cursor.fetchall()
mos_cursor.close()
mos_cnx.close()
combined_results = []
for i in range(len(mos_results)):
ct21_cnx = mysql.connector.connect(user='User2',
password='Password2',
host='Host2.com',
port='5678',
database='schema2’)
ct21_cursor = ct21_cnx.cursor(buffered=True)
ct21_query = ("""
SELECT
TASK_RESULT,
actor_name,
electricaltest.current,
voltage,
TOTAL_TEST_TIME
FROM
electricaltest
WHERE
electricaltest.SERIAL_NUMBER IN (%s)
""")
ct21_cursor.executemany(ct21_query, (newserials,))
ct21_results = ct21_cursor.fetchall()
ct21_cursor.close()
ct21_cnx.close()
combined_results.append((mos_results[i][0], mos_results[i][1],
mos_results[i][2], mos_results[i][3],
ct21_results[0][0], ct21_results[0][1],
ct21_results[0][2], ct21_results[0][3],
ct21_results[0][4]))
workbook = xlsxwriter.Workbook('CT data.xlsx')
time_format = workbook.add_format({'num_format': 'yyyy-mm-dd hh:mm:ss'})
counter_format = workbook.add_format({'num_format': 'hh:mm:ss'})
worksheet = workbook.add_worksheet()
headers = ['Serial', 'Test', 'Pass/Fail', 'Value', 'HiPot Pass/Fail', 'HiPot Line', 'Current', 'Voltage', 'Test Time']
for i in range(len(headers)):
worksheet.write(0, i, headers[i])
xlsx_row = 1
for row in combined_results:
for j in range(len(headers)):
worksheet.write(xlsx_row, j, row[j])
xlsx_row += 1
workbook.close()
如前所述,仅适用于单个序列,而不是列表。不知道我哪里做错了。以下是pycharm控制台中的错误代码:
Traceback (most recent call last):
File "C:\Program Files\JetBrains\PyCharm Community Edition 2019.1.2\helpers\pydev\pydevd.py", line 1758, in <module>
main()
File "C:\Program Files\JetBrains\PyCharm Community Edition 2019.1.2\helpers\pydev\pydevd.py", line 1752, in main
globals = debugger.run(setup['file'], None, None, is_module)
File "C:\Program Files\JetBrains\PyCharm Community Edition 2019.1.2\helpers\pydev\pydevd.py", line 1147, in run
pydev_imports.execfile(file, globals, locals) # execute the script
File "C:\Program Files\JetBrains\PyCharm Community Edition 2019.1.2\helpers\pydev\_pydev_imps\_pydev_execfile.py", line 18, in execfile
exec(compile(contents+"\n", file, 'exec'), glob, loc)
File "C:/Users/User/PycharmProjects/CT21 to MOS/ct 21 and MOS.py", line 35, in <module>
mos_cursor.executemany(mos_query, (ogserials,))
File "C:\Users\User\PycharmProjects\CT21 to MOS\venv\lib\site-packages\mysql\connector\cursor.py", line 675, in executemany
self.execute(operation, params)
File "C:\Users\User\PycharmProjects\CT21 to MOS\venv\lib\site-packages\mysql\connector\cursor.py", line 561, in execute
"Not all parameters were used in the SQL statement")
mysql.connector.errors.ProgrammingError: Not all parameters were used in the SQL statement