无法将值插入Sqlite关系数据库中的表

时间:2019-06-22 16:44:53

标签: java database sqlite rdbms

我已经使用DB Browser for Sqlite v3.10.1创建了一个Sqlite数据库,其中包含三个表:Book,Patron和Loan。我成功地将一些示例数据添加到Book和Patron表中,但是当我尝试将一些数据插入到Loans中时,我收到一条错误消息,指出外键约束失败。

  • 我尝试从数据库浏览器和Java程序(包括代码)插入数据

用于创建表的SQL代码:

CREATE TABLE `Book` (
    `BookID`    INTEGER PRIMARY KEY AUTOINCREMENT,
    `ISBN`  TEXT NOT NULL,
    `Title` TEXT NOT NULL,
    `Author`    TEXT NOT NULL
);
CREATE TABLE `Patron` (
    `PatronID`  INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
    `Name`  TEXT NOT NULL,
    `Address`   INTEGER NOT NULL
);
CREATE TABLE `Loan` (
    `LoanID`    INTEGER NOT NULL,
    `BookID`    INTEGER NOT NULL,
    `PatronID`  INTEGER NOT NULL,
    `BorrowDate`    TEXT NOT NULL,
    `DueDate`   TEXT NOT NULL,
    FOREIGN KEY(`BookID`) REFERENCES `Book`(`BookID`),
    FOREIGN KEY(`PatronID`) REFERENCES `Patron`(`PatronID`),
    PRIMARY KEY(`LoanID`)
);

Java程序:

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;

public class LibraryDatabase
{
    public static Connection connect() throws SQLException
    {
        Connection conn = null;

        // db parameters
        String url = "jdbc:sqlite:database.db";
        // create a connection to the database
        conn = DriverManager.getConnection(url);

        System.out.println("Connection to SQLite has been established.");

        return conn;
    }

    public static void main(String[] args) throws SQLException
    {
        Connection c = connect();

        Statement stmt = c.createStatement();
        String sql = "INSERT INTO Book (ISBN, Title, Author) VALUES ('978-1-4842-3272-9', 'Programming PIC Microcontrollers with XC8', 'Armstrong Subero');";
        stmt.execute(sql);
        stmt.close();

        String sql2 = "INSERT INTO Loans (BookID, PatronID, BorrowDate, DueDate) VALUES (1, 1, '23/06/2019', '23/07/2019')";
        stmt.execute(sql2);
    }
}

来自数据库浏览器的错误消息:

Error adding record:
FOREIGN KEY constrained failed (INSERT INTO
'Loan'() VALUES(1,0,0,",");)

来自Java程序的堆栈跟踪:

Connection to SQLite has been established.
Exception in thread "main" java.sql.SQLException: [SQLITE_ERROR] SQL error or missing database (no such table: Loans)
    at org.sqlite.DB.newSQLException(DB.java:383)
    at org.sqlite.DB.newSQLException(DB.java:387)
    at org.sqlite.DB.throwex(DB.java:374)
    at org.sqlite.NativeDB.prepare(Native Method)
    at org.sqlite.DB.prepare(DB.java:123)
    at org.sqlite.Stmt.execute(Stmt.java:113)
    at LibraryDatabase.main(LibraryDatabase.java:32)

1 个答案:

答案 0 :(得分:0)

您需要先插入一个赞助人,然后使用此表的外键插入贷款

尝试

    Statement stmt = c.createStatement();
    String sql = "INSERT INTO Book (ISBN, Title, Author) VALUES ('978-1-4842-3272-9', 'Programming PIC Microcontrollers with XC8', 'Armstrong Subero');";
    stmt.execute(sql);
    stmt.close();

    String sql2 = "insert into Patron (Name, Address) values ('a name', 'a adress')";
    stmt.execute(sql2);

    String sql3 = "INSERT INTO Loans (BookID, PatronID, BorrowDate, DueDate) VALUES (1, 1, '23/06/2019', '23/07/2019')";
    stmt.execute(sql3);