当我尝试运行代码时,出现此错误:
File "script.py", line 36, in <module>
result = cursor.execute(sql_insert_query)
File "/home/rexio18/.local/lib/python2.7/site-packages/mysql/connector/cursor_cext.py", line 266, in execute
raw_as_string=self._raw_as_string)
File "/home/rexio18/.local/lib/python2.7/site-packages/mysql/connector/connection_cext.py", line 405, in cmd_query
errno=2055, values=(addr, 'Connection not available.'))
mysql.connector.errors.OperationalError: 2055: Lost connection to MySQL server at 'localhost:3306', system error: Connection not available.
代码:
for child in root:
for element in child:
for subelement in element:
a = subelement.attrib['currency']
b = subelement.text
connection = mysql.connector.connect(host ='localhost', user ='root', passwd ='admin' , database ='python')
cursor = connection.cursor()
sql_insert_query = "INSERT INTO valoare(moneda, flux) VALUES (%s, %s)",(a, b)
result = cursor.execute(sql_insert_query)
connection.commit()