0

I believe I am part way there but am really quite unsure as to what to do to make Incident a table that links Student and Behaviour tables. I am writing in python in sqlite3.

c.execute('''CREATE TABLE Student (StudentID integer PRIMARY KEY, Forename text, Surname text, FormNumber text, YearGroup text, Date text, FormTutor text)''')

c.execute('''CREATE table Incident (Student ID integer, BehaviourID integer, Date text)''')

c.execute('''CREATE TABLE Behaviour (BehaviourID integer PRIMARY KEY, BehaviourType text, BehaviourDescription text)''')

1 Answer 1

1

I think you mostly have it (if I understand your question correctly). Take a look at this:

c.execute('''
CREATE TABLE Student (
    StudentID integer PRIMARY KEY, 
    Forename text, 
    Surname text, 
    FormNumber text, 
    YearGroup text, 
    Date text, 
    FormTutor text)''')

c.execute('''
CREATE TABLE Behaviour (
    BehaviourID integer PRIMARY KEY, 
    BehaviourType text, 
    BehaviourDescription text)''')

c.execute('''
CREATE table Incident (
    StudentID integer, 
    BehaviourID integer, 
    Date text,
    FOREIGN KEY(StudentID) REFERENCES Student(StudentID),
    FOREIGN KEY(BehaviourID) REFERENCES Behaviour(BehaviourID))''')

This creates the Student table, then the Behaviour table, and finally the Incident table with foreign keys referencing the primary keys in Student and Behaviour. The Incident table allows you to define a many-to-many relationship between Student and Behaviour.

6
  • Here is what I am trying to enter into the database. All the variables are populated when it it run but it returns an error..'table Student has 7 columns but 6 values were supplied'. I thought that my StudentID would be automatically filled? Thank you for the help so far but do you know what is going on here?
    – Jambofun
    Feb 25, 2014 at 21:22
  • Could you post a copy of your insert statement?
    – Peter
    Feb 25, 2014 at 22:00
  • Forename = self.Forename.get() Surname = self.Surname.get() FormNumber = self.formnumber.get() YearGroup = self.yeargroup.get() Date = self.currentdate.get() FormTutor = self.formtutor.get() BehaviourComment = self.behaviourComment.get() BehaviourType = self.behaviourType.get() BehaviourComment = self.behaviourComment.get() StudentDetails = [Forename,Surname,FormNumber,YearGroup,Date,FormTutor] BehaviourDetails = [BehaviourType, BehaviourComment] c.execute("INSERT INTO Student VALUES (?,?,?,?,?,?)",StudentDetails) c.execute("INSERT INTO Behaviour VALUES (?,?)",BehaviourDetails)
    – Jambofun
    Feb 26, 2014 at 8:04
  • Try listing the column names in your insert statements. For example: INSERT INTO Student(Forename,Surname,FormNumber,YearGroup,Date,FormTutor) VALUES (?,?,?,?,?,?) ... If you don't explicitly list all the columns, then sqlite expects you to provide values for all columns in the table, including the primary key.
    – Peter
    Feb 26, 2014 at 16:45
  • 1
    Glad I could help. By the way, don't forget to 'Accept' an answer when you ask a question. It looks like you've asked a few questions but haven't accepted any answers yet. :)
    – Peter
    Feb 26, 2014 at 17:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.