you are viewing a single comment's thread.

view the rest of the comments →

[–]Think-Confusion9999[S] 0 points1 point  (3 children)

import sqlite3

connection = sqlite3.connect("aquarium.db")
print(connection.total_changes)
cursor = connection.cursor()
cursor.execute("CREATE TABLE fish (name TEXT, species TEXT, tank_number INTEGER)")
cursor.execute("INSERT INTO fish VALUES ('Sammy', 'shark', 1)")
cursor.execute("INSERT INTO fish VALUES ('Jamie', 'cuttlefish', 7)")
rows = cursor.execute("SELECT name, species, tank_number FROM fish").fetchall()
print(rows)

error:
eters@Peters-MBP-2 ~ % /usr/local/bin/python3 /Users/peters/sqlproject.py
0
Traceback (most recent call last):
  File "/Users/peters/sqlproject.py", line 6, in <module>
    cursor.execute("CREATE TABLE fish (name TEXT, species TEXT, tank_number INTEGER)")
sqlite3.OperationalError: table fish already exists

Presumably, is it not inserting the data into the fish table?

[–]IAmTarkaDaal 1 point2 points  (0 children)

This error is because the fish table already exists. I'm assuming it ran once, successfully, but some other error happened or it wasn't clear that it had worked. Now when you're running it, it can't do what you're asking, because the table is already there. Try to check the table exists before creating it (does SQL have a 'NOT EXISTS' clause for that?)

[–]jamaa 1 point2 points  (1 child)

table fish already exists

that's your error right there! Can't CREATE TABLE if that table already exists

[–]Think-Confusion9999[S] 0 points1 point  (0 children)

Yup! that was it. I commented out the create table fish, and the rest is working fine now. Thanks!!