Mahlzeit,
habe das nachfolgende Script mit der Fehlermeldung:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | # -*- coding: utf-8 -*- #!/usr/bin/env python3 import sys import mysql.connector table = sys.argv[1] #Verbindung erstellen try: connection = mysql.connector.connect(host = 'localhost', user = 'user1', passwd = 'password1', db = 'data') except: print ("Keine Verbindung zum Server") exit(0) cursor = connection.cursor() cursor.execute("SELECT field1 from %s",(table, )) # Diese Zeilen gehen auch nicht! # sql="""SELECT field1 from %s""" # cursor.execute (sql, table) result = cursor.fetchall() cursor.close() |
Traceback (most recent call last): File "save_db-content_into_file.py", line 52, in <module> cursor.execute (sql, table) File "/usr/lib/python3/dist-packages/mysql/connector/cursor.py", line 507, in execute self._handle_result(self._connection.cmd_query(stmt)) File "/usr/lib/python3/dist-packages/mysql/connector/connection.py", line 722, in cmd_query result = self._handle_result(self._send_cmd(ServerCmd.QUERY, query)) File "/usr/lib/python3/dist-packages/mysql/connector/connection.py", line 640, in _handle_result raise errors.get_exception(packet) mysql.connector.errors.ProgrammingError: 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '%s' at line 1
Aufruf des Scriptes mit: python3 my_script.py table1