Hướng dẫn could not process parameters mysql python - không thể xử lý tham số mysql python

Tôi đang cố gắng lặp qua một mảng và chèn từng phần tử vào một bảng. Theo như tôi có thể thấy cú pháp của tôi là chính xác và tôi đã lấy mã này từ tài liệu của Microsoft Azure.

try:
   conn = mysql.connector.connect(**config)
   print("Connection established")
except mysql.connector.Error as err:
  if err.errno == errorcode.ER_ACCESS_DENIED_ERROR:
    print("Something is wrong with the user name or password")
  elif err.errno == errorcode.ER_BAD_DB_ERROR:
    print("Database does not exist")
  else:
    print(err)
else:
  cursor = conn.cursor()
data = ['1','2','3','4','5']


for x in data:
   cursor.execute("INSERT INTO test (serial) VALUES (%s)",(x))
   print("Inserted",cursor.rowcount,"row(s) of data.")

conn.commit()
cursor.close()
conn.close()
print("Done.")

Khi tôi chạy, điều này sẽ đến cursor.execute(...) và sau đó thất bại. Đây là dấu vết ngăn xếp.

Traceback (cuộc gọi gần đây nhất cuối cùng): Tệp "test.py", dòng 29, trong con trỏ.execute ("chèn vào các giá trị kiểm tra (nối tiếp) (%s)", ("test")) AlexJ \ AppData \ Local \ Program \ Python \ Python37 \ lib \ Site-Packages \ MySQL \ Connector \ Cursor_Cext.Py " \ AppData \ Local \ Program \ Python \ Python37 \ lib \ Site-Packages \ MySQL \ Connector \ Connection_Cext.Py "

Đã hỏi ngày 4 tháng 2 năm 2019 lúc 14:52Feb 4, 2019 at 14:52

Hướng dẫn could not process parameters mysql python - không thể xử lý tham số mysql python

ajjohnson190ajjohnson190ajjohnson190

4752 Huy hiệu vàng6 Huy hiệu bạc20 Huy hiệu đồng2 gold badges6 silver badges20 bronze badges

2

Thử cái này:

for x in data:
    value = "test"
    query = "INSERT INTO test (serial) VALUES (%s)"
    cursor.execute(query,(value,))
    print("Inserted",cursor.rowcount,"row(s) of data.")

Vì bạn đang sử dụng mô -đun MySQL, cursor.execute yêu cầu truy vấn SQL và Tuple làm tham số

Hướng dẫn could not process parameters mysql python - không thể xử lý tham số mysql python

Wes Hardaker

21.3k2 Huy hiệu vàng38 Huy hiệu bạc69 Huy hiệu đồng2 gold badges38 silver badges69 bronze badges

Đã trả lời ngày 4 tháng 2 năm 2019 lúc 15:03Feb 4, 2019 at 15:03

Lucas Hortlucas HortLucas Hort

7626 Huy hiệu bạc9 Huy hiệu Đồng6 silver badges9 bronze badges

3

Câu trả lời hay từ @lucas, nhưng có lẽ điều này giúp đỡ khác, tôi nghĩ rằng sạch hơn

sql = "INSERT INTO your_db (your_table) VALUES (%s)"
val = [("data could be array")]
cursor = cnx.cursor()
cursor.execute(sql, val)
print("Inserted",cursor.rowcount,"row(s) of data.")
cnx.commit()
cnx.close()

CZ Điều này rất hữu ích cho mục đích của tôi, để nhập nhiều dữ liệu.

Đã trả lời ngày 21 tháng 12 năm 2020 lúc 4:32Dec 21, 2020 at 4:32

Hướng dẫn could not process parameters mysql python - không thể xử lý tham số mysql python

Budi Mulyobudi MulyoBudi Mulyo

3855 Huy hiệu bạc22 Huy hiệu Đồng5 silver badges22 bronze badges

Tôi đang phải đối mặt với cùng một vấn đề nhưng thay vì mảng, tôi đang lặp qua một bộ và chèn từng mục vào DB MySQL và có lỗi này mysql.connector.errors.ProgrammingError: Could not process parameters: str(Data_Tokens), it must be of type list, tuple or dict.

Uniquetokenset bao gồm kiểu dữ liệu chuỗi, nhưng vì lỗi cho thấy nó phải là danh sách, tuple hoặc dict. Bằng cách chuyển đổi

for x in data:
    value = "test"
    query = "INSERT INTO test (serial) VALUES (%s)"
    cursor.execute(query,(value,))
    print("Inserted",cursor.rowcount,"row(s) of data.")
0 thành Danh sách Tuple
for x in data:
    value = "test"
    query = "INSERT INTO test (serial) VALUES (%s)"
    cursor.execute(query,(value,))
    print("Inserted",cursor.rowcount,"row(s) of data.")
1 làm việc cho tôi.

uniqueTokenSet = set()

for item in uniqueTokenSet:
    tokenSql = "insert into tokens(token) values (%s)"
    data = [(item)]
    mycursor.execute(tokenSql, data)

print('data inserted')
mydb.commit()

Đã trả lời ngày 10 tháng 8 lúc 7:27Aug 10 at 7:27

Hướng dẫn could not process parameters mysql python - không thể xử lý tham số mysql python

10.5.4 & nbsp; mysqlcursor.execute () Phương thức

Cú pháp:

cursor.execute(operation, params=None, multi=False)
iterator = cursor.execute(operation, params=None, multi=True)

Phương thức này thực thi cơ sở dữ liệu đã cho

for x in data:
    value = "test"
    query = "INSERT INTO test (serial) VALUES (%s)"
    cursor.execute(query,(value,))
    print("Inserted",cursor.rowcount,"row(s) of data.")
2 (truy vấn hoặc lệnh). Các tham số được tìm thấy trong tuple hoặc từ điển
for x in data:
    value = "test"
    query = "INSERT INTO test (serial) VALUES (%s)"
    cursor.execute(query,(value,))
    print("Inserted",cursor.rowcount,"row(s) of data.")
3 được liên kết với các biến trong hoạt động. Chỉ định các biến bằng cách sử dụng kiểu tham số
for x in data:
    value = "test"
    query = "INSERT INTO test (serial) VALUES (%s)"
    cursor.execute(query,(value,))
    print("Inserted",cursor.rowcount,"row(s) of data.")
4 hoặc ____ 15) (nghĩa là sử dụng kiểu
for x in data:
    value = "test"
    query = "INSERT INTO test (serial) VALUES (%s)"
    cursor.execute(query,(value,))
    print("Inserted",cursor.rowcount,"row(s) of data.")
6 hoặc
for x in data:
    value = "test"
    query = "INSERT INTO test (serial) VALUES (%s)"
    cursor.execute(query,(value,))
    print("Inserted",cursor.rowcount,"row(s) of data.")
7).
for x in data:
    value = "test"
    query = "INSERT INTO test (serial) VALUES (%s)"
    cursor.execute(query,(value,))
    print("Inserted",cursor.rowcount,"row(s) of data.")
8 Trả về một trình lặp nếu
for x in data:
    value = "test"
    query = "INSERT INTO test (serial) VALUES (%s)"
    cursor.execute(query,(value,))
    print("Inserted",cursor.rowcount,"row(s) of data.")
9 là
sql = "INSERT INTO your_db (your_table) VALUES (%s)"
val = [("data could be array")]
cursor = cnx.cursor()
cursor.execute(sql, val)
print("Inserted",cursor.rowcount,"row(s) of data.")
cnx.commit()
cnx.close()
0.

Ghi chú

Trong Python, một tuple chứa một giá trị duy nhất phải bao gồm dấu phẩy. Ví dụ, ('ABC') được đánh giá là vô hướng trong khi ('ABC',) được đánh giá là một tuple.('abc') is evaluated as a scalar while ('abc',) is evaluated as a tuple.

Ví dụ này chèn thông tin về một nhân viên mới, sau đó chọn dữ liệu cho người đó. Các câu lệnh được thực thi dưới dạng các hoạt động

for x in data:
    value = "test"
    query = "INSERT INTO test (serial) VALUES (%s)"
    cursor.execute(query,(value,))
    print("Inserted",cursor.rowcount,"row(s) of data.")
8 riêng biệt:

insert_stmt = (
  "INSERT INTO employees (emp_no, first_name, last_name, hire_date) "
  "VALUES (%s, %s, %s, %s)"
)
data = (2, 'Jane', 'Doe', datetime.date(2012, 3, 23))
cursor.execute(insert_stmt, data)

select_stmt = "SELECT * FROM employees WHERE emp_no = %(emp_no)s"
cursor.execute(select_stmt, { 'emp_no': 2 })

Các giá trị dữ liệu được chuyển đổi khi cần thiết từ các đối tượng Python thành thứ mà MySQL hiểu. Trong ví dụ trước, ví dụ

sql = "INSERT INTO your_db (your_table) VALUES (%s)"
val = [("data could be array")]
cursor = cnx.cursor()
cursor.execute(sql, val)
print("Inserted",cursor.rowcount,"row(s) of data.")
cnx.commit()
cnx.close()
2 được chuyển đổi thành
sql = "INSERT INTO your_db (your_table) VALUES (%s)"
val = [("data could be array")]
cursor = cnx.cursor()
cursor.execute(sql, val)
print("Inserted",cursor.rowcount,"row(s) of data.")
cnx.commit()
cnx.close()
3.

Nếu

for x in data:
    value = "test"
    query = "INSERT INTO test (serial) VALUES (%s)"
    cursor.execute(query,(value,))
    print("Inserted",cursor.rowcount,"row(s) of data.")
9 được đặt thành
sql = "INSERT INTO your_db (your_table) VALUES (%s)"
val = [("data could be array")]
cursor = cnx.cursor()
cursor.execute(sql, val)
print("Inserted",cursor.rowcount,"row(s) of data.")
cnx.commit()
cnx.close()
0,
for x in data:
    value = "test"
    query = "INSERT INTO test (serial) VALUES (%s)"
    cursor.execute(query,(value,))
    print("Inserted",cursor.rowcount,"row(s) of data.")
8 có thể thực thi nhiều câu lệnh được chỉ định trong chuỗi
for x in data:
    value = "test"
    query = "INSERT INTO test (serial) VALUES (%s)"
    cursor.execute(query,(value,))
    print("Inserted",cursor.rowcount,"row(s) of data.")
2. Nó trả về một trình lặp cho phép xử lý kết quả của mỗi câu lệnh. Tuy nhiên, sử dụng các tham số không hoạt động tốt trong trường hợp này và thường là một ý tưởng tốt để tự thực hiện từng câu lệnh.

Ví dụ sau chọn và chèn dữ liệu trong một thao tác

for x in data:
    value = "test"
    query = "INSERT INTO test (serial) VALUES (%s)"
    cursor.execute(query,(value,))
    print("Inserted",cursor.rowcount,"row(s) of data.")
8 duy nhất và hiển thị kết quả của mỗi câu lệnh:

operation = 'SELECT 1; INSERT INTO t1 VALUES (); SELECT 2'
for result in cursor.execute(operation, multi=True):
  if result.with_rows:
    print("Rows produced by statement '{}':".format(
      result.statement))
    print(result.fetchall())
  else:
    print("Number of rows affected by statement '{}': {}".format(
      result.statement, result.rowcount))

Nếu kết nối được cấu hình để tìm nạp các cảnh báo, các cảnh báo được tạo bởi hoạt động có sẵn thông qua phương thức mysqlcursor.fetchwarnings ().