为什么我在sqlite3中不断收到数据类型不匹配错误

xxslljrj  于 2023-03-30  发布在  SQLite
关注(0)|答案(1)|浏览(310)

在sqlite3中运行以下代码时,我一直收到数据类型不匹配的错误。我将包括从数据库创建到收到错误的所有错误。

CREATE TABLE PilotAirlines (
...> PilotID INTEGER PRIMARY KEY,
...> FName TEXT,
...> LName TEXT,
...> JobTitle TEXT,
...> Salary REAL,
...> AirlineID INTEGER,
...> NPlanes TEXT,
...> NRoutes TEXT,
...> NPilots TEXT,
...> FOREIGN KEY (AirlineID) REFERENCES Airlines (AirlineID)
...> );
CREATE TABLE Airlines (
...> AirlineID INTEGER PRIMARY KEY,
...> NPlanes TEXT,
...> NRoutes TEXT,
...> NPilots TEXT
...> );
INSERT INTO Airlines (AirlineID, NPLanes, NRoutes, NPilots)
...> VALUES
...> (1, 130, 75, 50),
...> (2, 200, 90, 85),
...> (3, 250, 125, 105);
INSERT INTO PilotAirlines (PilotID, FName, LName, JobTitle, Salary, AirlineID, NPLanes, NRoutes, NPilots)
 VALUES
 ...> ('P00100', 'John', 'Hancock', 'Pilot', 110000, 1, NULL, NULL, NULL),
 ...> ('P00101', 'Sally', 'Smith', 'CoPilot', 95000, 2, NULL, NULL, NULL),
 ...> ('P00102', 'Nancy', 'Smith', 'Pilot', 115000, 3, NULL, NULL, NULL),
 ...> ('P00103', 'William', 'Hart', 'CoPilot', 85000, 1, NULL, NULL, NULL),
 ...> ('P00104', 'Jane', 'Doe', 'Pilot', 95000, 3, NULL, NULL, NULL);
Runtime error: datatype mismatch (20)

我想可能需要更改AirlineID,因为我的ID中有一个“P”,这应该是一个INT,所以我在下面更改了它。

INSERT INTO PilotAirlines (PilotID, FName, LName, JobTitle, Salary, AirlineID, NPLanes, NRoutes, NPilots)
...> VALUES
...> ('00100', 'John', 'Hancock', 'Pilot', 110000, 1, NULL, NULL, NULL),
...> ('P00101', 'Sally', 'Smith', 'CoPilot', 95000, 2, NULL, NULL, NULL),
...> ('00101', 'Sally', 'Smith', 'CoPilot', 95000, 2, NULL, NULL, NULL),
...> ('00102', 'Nancy', 'Smith', 'Pilot', 115000, 3, NULL, NULL, NULL),
...> ('00103', 'William', 'Hart', 'CoPilot', 85000, 1, NULL, NULL, NULL),
...> ('00104', 'Jane', 'Doe', 'Pilot', 95000, 3, NULL, NULL, NULL);
Runtime error: datatype mismatch (20)

请帮我这个!!这可能是一个明显的答案,但我今晚有这个问题,这是我的项目论文的一部分,所以请!!!
提前感谢大家!!
我想可能需要更改AirlineID,因为我的ID中有一个“P”,这应该是一个INT,所以我在下面更改了它。

INSERT INTO PilotAirlines (PilotID, FName, LName, JobTitle, Salary, AirlineID, NPLanes,     NRoutes, NPilots)
...> VALUES
...> ('00100', 'John', 'Hancock', 'Pilot', 110000, 1, NULL, NULL, NULL),
...> ('P00101', 'Sally', 'Smith', 'CoPilot', 95000, 2, NULL, NULL, NULL),
...> ('00101', 'Sally', 'Smith', 'CoPilot', 95000, 2, NULL, NULL, NULL),
...> ('00102', 'Nancy', 'Smith', 'Pilot', 115000, 3, NULL, NULL, NULL),
...> ('00103', 'William', 'Hart', 'CoPilot', 85000, 1, NULL, NULL, NULL),
...> ('00104', 'Jane', 'Doe', 'Pilot', 95000, 3, NULL, NULL, NULL);
Runtime error: datatype mismatch (20)
bq3bfh9z

bq3bfh9z1#

数据类型不匹配有一个原因,即当试图为显式或隐式定义为INTEGER PRIMARY KEY(带或不带AUTOINCRMENT)的列分配非整数值时,即该列是rowid表的别名。

  • 其它列可以存储任何类型的值而没有这个问题。
  • 参见https://www.sqlite.org/rescode.html#mismatch

根据您显示的代码,尝试仍然具有不是整数值的值,如下所示:

...> ('P00101', 'Sally', 'Smith', 'CoPilot', 95000, 2, NULL, NULL, NULL),

更正此错误(将'P00101'更改为'00101'(实际上是整数101))并用途:-

DROP TABLE IF EXISTS PilotAirlines;
DROP TABLE IF EXISTS Airlines;
CREATE TABLE PilotAirlines (
    PilotID INTEGER PRIMARY KEY,
    FName TEXT,
    LName TEXT,
    JobTitle TEXT,
    Salary REAL,
    AirlineID INTEGER,
    NPlanes TEXT,
    NRoutes TEXT,
    NPilots TEXT,
    FOREIGN KEY (AirlineID) REFERENCES Airlines (AirlineID)
);
CREATE TABLE Airlines (
    AirlineID INTEGER PRIMARY KEY,
    NPlanes TEXT,
    NRoutes TEXT,
    NPilots TEXT
);
INSERT INTO Airlines (AirlineID, NPLanes, NRoutes, NPilots)
    VALUES
    (1, 130, 75, 50),
    (2, 200, 90, 85),
    (3, 250, 125, 105);
INSERT INTO PilotAirlines (PilotID, FName, LName, JobTitle, Salary, AirlineID, NPLanes, NRoutes, NPilots)
 VALUES
    ('00100', 'John', 'Hancock', 'Pilot', 110000, 1, NULL, NULL, NULL),
    ('00101', 'Sally', 'Smith', 'CoPilot', 95000, 2, NULL, NULL, NULL),
    ('00102', 'Nancy', 'Smith', 'Pilot', 115000, 3, NULL, NULL, NULL),
    ('00103', 'William', 'Hart', 'CoPilot', 85000, 1, NULL, NULL, NULL),
    ('00104', 'Jane', 'Doe', 'Pilot', 95000, 3, NULL, NULL, NULL);

那么结果是:

DROP TABLE IF EXISTS PilotAirlines
> OK
> Time: 0s

DROP TABLE IF EXISTS Airlines
> OK
> Time: 0s

CREATE TABLE PilotAirlines (
    PilotID INTEGER PRIMARY KEY,
    FName TEXT,
    LName TEXT,
    JobTitle TEXT,
    Salary REAL,
    AirlineID INTEGER,
    NPlanes TEXT,
    NRoutes TEXT,
    NPilots TEXT,
    FOREIGN KEY (AirlineID) REFERENCES Airlines (AirlineID)
)
> OK
> Time: 0.453s

CREATE TABLE Airlines (
    AirlineID INTEGER PRIMARY KEY,
    NPlanes TEXT,
    NRoutes TEXT,
    NPilots TEXT
)
> OK
> Time: 0.042s

INSERT INTO Airlines (AirlineID, NPLanes, NRoutes, NPilots)
    VALUES
    (1, 130, 75, 50),
    (2, 200, 90, 85),
    (3, 250, 125, 105)
> Affected rows: 3
> Time: 0.024s

INSERT INTO PilotAirlines (PilotID, FName, LName, JobTitle, Salary, AirlineID, NPLanes, NRoutes, NPilots)
 VALUES
    ('00100', 'John', 'Hancock', 'Pilot', 110000, 1, NULL, NULL, NULL),
    ('00101', 'Sally', 'Smith', 'CoPilot', 95000, 2, NULL, NULL, NULL),
    ('00102', 'Nancy', 'Smith', 'Pilot', 115000, 3, NULL, NULL, NULL),
    ('00103', 'William', 'Hart', 'CoPilot', 85000, 1, NULL, NULL, NULL),
    ('00104', 'Jane', 'Doe', 'Pilot', 95000, 3, NULL, NULL, NULL)
> Affected rows: 5
> Time: 0.024s

相关问题