简体   繁体   中英

How to add a 2d array to a database in python

I am trying to add a list of values into a table in a database

QuestionAnswer = [['x','y','z'],['x','y','z']]

(cursor.executemany('INSERT question (QuizID,question,answer) VALUES (%s,%s,%s)',QuestionAnswer)

db.commit()

the error is the following:

Traceback (most recent call last): File "C:\\Users\\georgia vost\\AppData\\Local\\Programs\\Python\\Python37-32\\lib\\tkinter__init__.py", line 1705, in call return self.func(*args) File "C:\\Users\\georgia vost\\Documents\\Private Document\\NEA WORK\\Code Folder\\appJar\\appjar.py", line 3494, in return lambda *args: funcName(param) File "C:\\Users\\georgia vost\\Documents\\Private Document\\NEA WORK\\Code Folder\\prastice program 1.py", line 37, in press cursor.executemany('INSERT question (QuizID,question,answer) VALUES (%s,%s,%s)',QuestionAnswer) File "C:\\Users\\georgia vost\\AppData\\Local\\Programs\\Python\\Python37-32\\lib\\site-packages\\mysql\\connector\\cursor.py", line 675, in executemany self.execute(operation, params) File "C:\\Users\\georgia vost\\AppData\\Local\\Programs\\Python\\Python37-32\\lib\\site-packages\\mysql\\connector\\cursor.py", line 557, in execute psub = _ParamSubstitutor(self._process_params(params)) File "C:\\Users\\georgia vost\\AppData\\Local\\Programs\\Python\\Python37-32\\lib\\site-packages\\mysql\\connecto r\\cursor.py", line 437, in _process_params "Failed processing format-parameters; %s" % err) mysql.connector.errors.ProgrammingError: Failed processing format-parameters; Python 'method' cannot be converted to a MySQL type

您现有的代码中可能需要进行许多更改,包括查询语法

cursor.executemany('INSERT INTO question (QuizID,question,answer) VALUES (?,?,?)',list(map(tuple,QuestionAnswer)))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM