我当前正在创建一个MySQL数据库,该数据库想使用for循环遍历包含表中值的数据字段。我已经成功完成了代码,但是当我想运行脚本以最终写入数据库时,会出现
错误python3 blueteamcolorassignment9.py
/usr/bin/python3: can't find '__main__' module in 'blueteamcolorassignment9.py'
假设要从此for循环中接收值。
python3
Python 3.5.3 (default, Sep 27 2018, 17:25:39)
[GCC 6.3.0 20170516] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> #!/usr/bin/python3
... import mysql.connector
>>> conn = mysql.connector.connect(user='root',password='alozon23',database='pytest')
>>> cursor = conn.cursor()
>>> query = ('SELECT empid,lastname,firstname,salary FROM employees')
>>> cursor.execute(query)
>>> for (empid,lastname,firstname,salary) in cursor:
... print(empid,lastname,firstname,salary)
... cursor.close()
File "<stdin>", line 3
cursor.close()
^
SyntaxError: invalid syntax
>>> cursor.close()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib/python3/dist-packages/mysql/connector/cursor.py", line 336, in close
raise errors.InternalError("Unread result found.")
mysql.connector.errors.InternalError: Unread result found.
>>> conn.close()
>>> exit;
Use exit() or Ctrl-D (i.e. EOF) to exit
>>> exit()