0
私はノートパソコンのUbuntuを入手しました。私はpipを使ってフラスコ、sqliteなどのものを手に入れました。テーブルにデータベースを作成してデータを作成するコードを作成しようとしましたが、データベースには何もありません。このコードの多くは機能しますが、テーブルは作成されません。Ubuntuにテーブルを持つSQLiteデータベースを作成しようとしましたが、何もしません。
import sqlite3
#Connects to the list database so we can create tables and insert data.
with sqlite3.connect('/home/muba/PycharmProjects/Work/Practise.db') as connection:
c = connection.cursor()
c.execute("""CREATE TABLE IF NOT EXISTS User(User_ID INTEGER PRIMARY KEY,
FirstName text, LastName text)""")
c.execute("PRAGMA foreign_keys = on")
c.execute("""CREATE TABLE IF NOT EXISTS Todo(todo_ID INTEGER PRIMARY KEY, UserID INTEGER, details,
FOREIGN KEY(UserID) REFERENCES User(User_ID))""")
#Code for inserting all data into tables.
私はそれが私にこのエラーが発生します。この部分に
c.execute("""INSERT INTO User (User_ID, FirstName,LastName) values("1","Henry","Sen")""")
c.execute("""INSERT INTO User (User_ID, FirstName,LastName) values("2","Megan","Knox")""")
c.execute("""INSERT INTO User (User_ID, FirstName,LastName) values("3","Muba","Lenk")""")
c.execute("""INSERT INTO User (User_ID, FirstName,LastName) values("4","Steve","Cook")""")
c.execute("""INSERT INTO User (User_ID, FirstName,LastName) values("5","Mubasher","Khan")""")
c.execute("""INSERT INTO User (User_ID, FirstName,LastName) values("6","Jeff","Hardy")""")
c.execute("""INSERT INTO User (User_ID, FirstName,LastName) values("7","Matthew","Levine")""")
c.execute("""INSERT INTO User (User_ID, FirstName,LastName) values("8","Daniel","Potter")""")
c.execute("""INSERT INTO User (User_ID, FirstName,LastName) values("9","Chris","Sanders")""")
c.execute("""INSERT INTO User (User_ID, FirstName,LastName) values("10","Mike","Hefferson")""")
c.execute("""INSERT INTO todo(todo_ID, UserID, details) Values("1","1","Clean the kitchen")""")
c.execute("""INSERT INTO todo(todo_ID, UserID, details) Values("2","2","Finish her Sprint 1")""")
c.execute("""INSERT INTO todo(todo_ID, UserID, details) Values("3","3","Work on all remaining Assignments")""")
c.execute("""INSERT INTO todo(todo_ID, UserID, details) Values("4","3","Document all his assignments")""")
c.execute("""INSERT INTO todo(todo_ID, UserID, details) Values("5","4","Starting studying for his exams")""")
c.execute("""INSERT INTO todo(todo_ID, UserID, details) Values("6","5","Clean his bedroom")""")
c.execute("""INSERT INTO todo(todo_ID, UserID, details) Values("7","6","Take the dog for a walk.")""")
c.execute("""INSERT INTO todo(todo_ID, UserID, details) Values("8","7","Go out and see the sun for the first time")""")
c.execute("""INSERT INTO todo(todo_ID, UserID, details) Values("9","8","Practise for upcoming soccer game")""")
c.execute("""INSERT INTO todo(todo_ID, UserID, details) Values("10","9","Catch the bus on time")""")
c.execute("""INSERT INTO todo(todo_ID, UserID, details) Values("11","10","Go grocery shopping")""")
を追加します。
/usr/bin/python2.7 /home/muba/PycharmProjects/Work/Database.py
Traceback (most recent call last):
File "/home/muba/PycharmProjects/Work/Database.py", line 12, in <module>
c.execute("""INSERT INTO User (User_ID, FirstName,LastName) values("1","Henry","Sen")""")
sqlite3.IntegrityError: UNIQUE constraint failed: User.User_ID
それがうまく働いた前に私は窓にそれをやったとき、私は、私が間違って何をしたかわかりません。私は何かの上に主キーを言及する必要があるかのように私が何かを逃したかどうか分からない> _ <
:
あなたが挿入する前に、レコードを削除し、ゼロからスタートしたい場合作成しましたか? – Muba
変更を永続化するために 'commit()'と呼ばれるようには見えません。 – cco
おお、私は、このような馬鹿が> _ <私はどのようにしてデータベースを確認した、データベース> _ < – Muba