um mein Projekt anzuschließen muss ich noch eine Kleinigkeit hinbekommen.
Aus der DB werte auslesen und weiter verarbeiten
Leider bekomme ich bei einer multi query abfrage Fehler, leider weiß ich nicht wie ich diese Fehler abfangen kann. Im Netzt habe ich bereits gesucht, gefunden habe ich das es in Python 3.7 einen Bug gab.
Ich benutze Ubuntu 20.04. Python 3.8, VI als editor.
Vielleicht hat einer mal das gleiche Problem gehabt und hat die Lösung, ich komme leider nicht weiter, vielleicht geht es so auch nicht dann muss ich das Projekt begraben oder irgendwie mit linux shell es versuchen.
Ihr seid meine letzte Hoffnung, google schon seit Montag, ich denke es muss gehen, aber leider fehlt mir an dieser stelle die Erfahrung.
Danke im Voraus.
Hier mal der Fehler aus der shell:
Traceback (most recent call last):
File "/usr/local/lib/python3.8/dist-packages/mysql/connector/cursor.py", line 486, in _execute_iter
result = next(query_iter)
StopIteration
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "./script.py", line 86, in <module>
funktion1()
File "./script", line 74, in abfrage_int
for result in results:
RuntimeError: generator raised StopIteration
Hier ist der Code, ich hoffe das ihr es versteht das ich nicht alles posten kann. Queries funktionieren wenn ich in der DB direkt ausführe, bekomme ich werte und keine Fehler.
versuch 2
Code: Alles auswählen
#!/usr/bin/python3
import mysql.connector
from mysql.connector.cursor import MySQLCursor
def funktion1():
b = mysql.connector.connect(option_files="file", use_pure=True)
cursor = db.cursor()
sql1 = "DROP TEMPORARY TABLE IF EXISTS table1"
sql2 = "DROP TEMPORARY TABLE IF EXISTS table2"
sql3 = "DROP TEMPORARY TABLE IF EXISTS table3"
sql4 = """
CREATE TEMPORARY TABLE table1 SELECT und so weiter
inhalt
inhalt
"""
sql5 = "CREATE TEMPORARY TABLE table2 SELECT * FROM table1"
sql6 = """
CREATE TEMPORARY TABLE table3 SELECT *,'ETWAS' FROM table1 UNION SELECT
inhalt
inhalt
"""
sql7 = "DROP TEMPORARY TABLE IF EXISTS table1"
sql8 = "CREATE TEMPORARY TABLE table1 SELECT * FROM table3"
sql9 = """
SELECT * FROM (
SELECT Inhalt
union
SELECT inhalt
"""
queries = [sql1, sql2, sql3, sql4, sql5, sql6, sql7, sql8, sql9]
#data = (etwas)
results = cursor.execute(";".join(queries), multi=True)
count = 1
for result in results:
print ("Query {0} - {1} :".format(count, result.statement))
if result.with_rows:
for row in result:
print (row)
#return row
count = count + 1
else:
print ("nichts da ")
print ()
cursor.close()
db.close
Code: Alles auswählen
with closing(
mysql.connector.connect(option_files="file")
) as connection:
with closing(connection.cursor()) as cursor:
cursor.execute("""
DROP TEMPORARY TABLE IF EXISTS table1;
DROP TEMPORARY TABLE IF EXISTS table2;
DROP TEMPORARY TABLE IF EXISTS table;
CREATE TEMPORARY TABLE table1 SELECT inhalt;
CREATE TEMPORARY TABLE table2 SELECT * FROM table1;
CREATE TEMPORARY TABLE table3 SELECT *,'etwas' FROM table11 UNION SELECT inhalt;
DROP TEMPORARY TABLE IF EXISTS table1;
CREATE TEMPORARY TABLE table1 SELECT * FROM table3;
SELECT * FROM (
SELECT inhalt
union
inhalt;
""", multi=True
)
#cursor.fetchall()
return cursor.fetchall()
#print (cursor.fetchall())