Zu Übungszwecken versuche ich mich an einer Musik-Datenbank:
Code: Alles auswählen
import sqlite3
def create_db_tables(db_conn, tables):
db_conn.execute("""CREATE TABLE IF NOT EXISTS {0} (
id INTEGER PRIMARY KEY,
name TEXT UNIQUE,
sort_name TEXT DEFAULT '',
musicbrainz_url TEXT DEFAULT '' )""".format(tables["artist"]))
db_conn.execute("""CREATE TABLE IF NOT EXISTS {0} (
id INTEGER PRIMARY KEY,
name TEXT UNIQUE,
sort_name TEXT DEFAULT '',
musicbrainz_url TEXT DEFAULT '' )""".format(tables["release"]))
db_conn.execute("""CREATE TABLE IF NOT EXISTS {0} (
id INTEGER PRIMARY KEY,
name TEXT UNIQUE)""".format(tables["genre"]))
db_conn.execute("""CREATE TABLE IF NOT EXISTS {0} (
id INTEGER PRIMARY KEY,
title TEXT UNIQUE,
musicbrainz_url TEXT DEFAULT '',
release INTEGER,
FOREIGN KEY(release) REFERENCES {1}(id),
artist INTEGER,
FOREIGN KEY(artist) REFERENCES {2}(id),
my_genre INTEGER,
FOREIGN KEY(my_genre) REFERENCES {3}(id) )""".format(tables["track"], tables["release"], tables["artist"], tables["genre"]))
project = "test"
tables = { "artist": "artists",
"track": "tracks",
"release": "releases",
"genre": "genres"}
db_connection = sqlite3.connect(project + ".db")
cur = db_connection.cursor()
create_db_tables(cur, tables)
Code: Alles auswählen
File "/home/werner/workspace_db.py", line 51, in <module>
create_db_tables(cur, tables)
File "/home/werner/workspace_db.py", line 35, in create_db_tables
FOREIGN KEY(my_genre) REFERENCES {3}(id) )""".format(tables["track"], tables["release"], tables["artist"], tables["genre"]))
sqlite3.OperationalError: near "artist": syntax error
Habt ihr eine Idee?
Vielen Dank, Werner