【问题标题】:Creating tables in MySQL in Python 3.4在 Python 3.4 中在 MySQL 中创建表
【发布时间】:2016-05-22 09:51:13
【问题描述】:

我在 Windows 10 上使用 Python 3.4 作为 cgi 和 MySQL 数据库服务器通过 Apache 运行服务器。当我尝试运行用于创建数据库的函数时,出现错误:

DatabaseError: 1005 (HY000): Can't create table `testdb`.`studentexam` (errno: 150 
"Foreign key constraint is incorrectly formed") 

创建数据库的函数

import mysql.connector as conn

#connect to server
db=conn.connect(host="localhost",user="root",password="")
cursor=db.cursor()
#create database
cursor.execute("""CREATE DATABASE IF NOT EXISTS Testdb""")
db.commit()
#use database
cursor.execute("""USE Testdb""")
db.commit()
#create Teacher table
cursor.execute("""CREATE TABLE IF NOT EXISTS Teacher(
    TeacherUsername VARCHAR(255) PRIMARY KEY,
    TeacherPassword TEXT)""")
db.commit()
#create student table
cursor.execute("""CREATE TABLE IF NOT EXISTS Student(
    StudentNo INT PRIMARY KEY,
    StudentSurname TEXT,
    StudentForename TEXT,
    StudentTeacher VARCHAR(255),
    StudentPassword TEXT,
    FOREIGN KEY(StudentTeacher) REFERENCES Teacher(TeacherUsername))""")
db.commit()
#create exam table
cursor.execute("""CREATE TABLE IF NOT EXISTS Exam(
    TestName VARCHAR(255) PRIMARY KEY,
    TestTotalMarks TEXT,
    Teacher VARCHAR(255),
    FOREIGN KEY(Teacher) REFERENCES Teacher(TeacherUsername))""")
db.commit()
#create StudentExam table
cursor.execute("""CREATE TABLE IF NOT EXISTS StudentExam(
    TestName VARCHAR(255),
    StudentID INT,
    StudentTotalMarks INT,
    PRIMARY KEY(TestName,StudentID),
    FOREIGN KEY(TestName) REFERENCES Exam(TestName),
    FOREIGN KEY(StudentID) REFERENCES Student(StudentID))""")
db.commit()
#create ExamSection table
cursor.execute("""CREATE TABLE IF NOT EXISTS ExamSection(
    TestName VARCHAR(255),
    SectionID INT,
    PRIMARY KEY(TestName,SectionID),
    FOREIGN KEY(TestName) REFERENCES Exam(TestName),
    FOREIGN KEY(SectionID) REFERENCES Section(SectionID))""")
db.commit()
#create Section table
cursor.execute("""CREATE TABLE IF NOT EXISTS Section(
    SectionID INT PRIMARY KEY,
    SectionName TEXT,
    SectionTotalMarks INT)""")
db.commit()
#create Question table
cursor.execute("""CREATE TABLE IF NOT EXISTS Question(
    QuestionID INT PRIMARY KEY,
    SectionID VARCHAR(255),
    Image TEXT,
    Question TEXT,
    PossibleAnswer TEXT,
    CorrectAnswer TEXT,
    QuestionType TEXT,
    FOREIGN KEY(SectionID) REFERENCES Section(SectionID))""")
db.commit()
#create QuestionResults Table
cursor.execute("""CREATE TABLE IF NOT EXISTS QuestionResults(
    QuestionID INT,
    StudentID INT,
    SectionID VARCHAR(255),
    StudentAnswer TEXT,
    PRIMARY KEY(QuestionID,StudentID),
    FOREIGN KEY(QuestionID) REFERENCES Question(QuestionID)
    FOREIGN KEY(StudentID) REFERENCES Student(StudentID))""")
db.commit()
#create Revision table
cursor.execute("""CREATE TABLE IF NOT EXISTS Revision(
    RevisionID INT PRIMARY KEY,
    RevisionSheet TEXT,
    TeacherUsername VARCHAR(255),
    FOREIGN KEY(TeacherUsername) REFERENCES Teacher(TeacherUsername))""")
db.commit()
#create StudentRevition table
cursor.execute("""CREATE TABLE IF NOT EXISTS StudentRevision(
    RevisionID INT,
    StudentID INT,
    PRIMARY KEY(RevisionID,StudentID),
    FOREIGN KEY(RevisionID) REFERENCES Revision(RevisionID),
    FOREIGN KEY(StudentID) REFERENCES Student(StudentID))""")
db.commit()
#create StudentResults table
cursor.execute("""CREATE TABLE IF NOT EXISTS StudentResults(
    SectionID VARCHAR(255),
    StudentID INT,
    StudentSectionMarks INT,
    PRIMARY KEY(SectionID,StudentID),
    FOREIGN KEY(SectionID) REFERENCES Section(SectionID),
    FOREIGN KEY(StudentID) REFERENCES Student(StudentID))""")
db.commit()
cursor.close()         

编辑:我将 StudentNo 更改为 StudentID,现在得到错误:

DatabaseError: 1005 (HY000): Can't create table `testdb`.`examsection` (errno: 150
"Foreign key constraint is incorrectly formed") 

【问题讨论】:

  • 你的代码比较乱,很难查。尝试将所有 SQL 语句收集到文件 .sql 中,并通过键入 \i name_file.sqlpsql 命令行运行它。可能你会有更多的控制和更好的错误自省。顺便说一句,错误在 SQL 中而不是在 Python 代码中。

标签: python mysql cgi


【解决方案1】:

改变这个:

#create student table
cursor.execute("""CREATE TABLE IF NOT EXISTS Student(
StudentNo INT PRIMARY KEY,
StudentSurname TEXT,
StudentForename TEXT,
StudentTeacher VARCHAR(255),
StudentPassword TEXT,
FOREIGN KEY(StudentTeacher) REFERENCES Teacher(TeacherUsername))""")
db.commit()

到这里:

#create student table
cursor.execute("""CREATE TABLE IF NOT EXISTS Student(
StudentID INT PRIMARY KEY,
StudentSurname TEXT,
StudentForename TEXT,
StudentTeacher VARCHAR(255),
StudentPassword TEXT,
FOREIGN KEY(StudentTeacher) REFERENCES Teacher(TeacherUsername))""")
db.commit()

【讨论】:

    【解决方案2】:

    创建 StudentExam 时,您引用了不存在的 Student.StudentId。您似乎更愿意引用 Student.StudentNo。

    编辑:

    创建 ExamSection 时,您引用了尚不存在的 Section 表。将 Section 创建状态向上移动,以便它在您创建 ExamSection 之前运行并提交。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-09-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-10-05
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多