Ich arbeite an einem lernen, wie man SQL in Python ausführen (ich kenne SQL, nicht Python).Lesen von externen SQL-Skript in Python
Ich habe eine externe SQL-Datei. Es erstellt und fügt Daten in drei Tabellen "Zookeeper", "Handles", "Animal" ein.
Dann habe ich eine Reihe von Abfragen von den Tabellen ablaufen. Die folgenden Abfragen befinden sich in der zoekeeper.sql-Datei, die ich am Anfang des Python-Skripts einlade. Beispiel für die ersten beiden sind:
--1.1
SELECT ANAME,zookeepid
FROM ANIMAL, HANDLES
WHERE AID=ANIMALID;
--1.2
SELECT ZNAME, SUM(TIMETOFEED)
FROM ZOOKEEPER, ANIMAL, HANDLES
WHERE AID=ANIMALID AND ZOOKEEPID=ZID
GROUP BY zookeeper.zname;
Diese alle ausführen fein in SQL. Jetzt muss ich sie in Python ausführen. Ich habe Code erhalten und vervollständigt, um die Datei einzulesen. Führen Sie anschließend alle Abfragen in der Schleife aus.
Die 1.1 und 1.2 ist, wo ich verwirrt bin. Ich glaube an die Schleife, das ist die Zeile, wo ich etwas einfügen sollte, um die erste und dann zweite Abfrage auszuführen.
Ergebnis = c.execute ("SELECT * FROM% s;"% Tabelle);
aber was? Ich denke, ich vermisse etwas sehr Offensichtliches. Ich denke, was mich abwirft ist% Tabelle. In Abfrage 1.1 und 1.2 erstelle ich keine Tabelle, sondern suche nach einem Abfrageergebnis.
Mein gesamter Python-Code ist unten.
import sqlite3
from sqlite3 import OperationalError
conn = sqlite3.connect('csc455_HW3.db')
c = conn.cursor()
# Open and read the file as a single buffer
fd = open('ZooDatabase.sql', 'r')
sqlFile = fd.read()
fd.close()
# all SQL commands (split on ';')
sqlCommands = sqlFile.split(';')
# Execute every command from the input file
for command in sqlCommands:
# This will skip and report errors
# For example, if the tables do not yet exist, this will skip over
# the DROP TABLE commands
try:
c.execute(command)
except OperationalError, msg:
print "Command skipped: ", msg
# For each of the 3 tables, query the database and print the contents
for table in ['ZooKeeper', 'Animal', 'Handles']:
**# Plug in the name of the table into SELECT * query
result = c.execute("SELECT * FROM %s;" % table);**
# Get all rows.
rows = result.fetchall();
# \n represents an end-of-line
print "\n--- TABLE ", table, "\n"
# This will print the name of the columns, padding each name up
# to 22 characters. Note that comma at the end prevents new lines
for desc in result.description:
print desc[0].rjust(22, ' '),
# End the line with column names
print ""
for row in rows:
for value in row:
# Print each value, padding it up with ' ' to 22 characters on the right
print str(value).rjust(22, ' '),
# End the values from the row
print ""
c.close()
conn.close()
Welche SQL-Abfrage (n) sollen Sie ausführen, die 1.1 und 1.2, oder einfach alles aus jeder Tabelle holen? – Azeirah
Ich möchte 1.1 und 1.2 (plus ich habe etwa 6 andere) aus der .SQL-Datei ausführen. – mpg
Ich gehe ein bisschen zu schnell Ich glaube, brauchst du Hilfe, um den Code zu verstehen, oder brauchst du Hilfe beim Bearbeiten deines Codes, um etwas extra zu tun? – Azeirah