Multiple inserts not working with SQLite 3

那年仲夏 提交于 2019-12-24 12:05:03

问题


The specific error, using a SQLite DB with PDO in PHP:

Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[HY000]: General error: 1 near ",": syntax error' in D:\Projects\2013\Stat collection plugin\stats\htdocs\index.php:8 Stack trace: #0 D:\Projects\2013\Stat collection plugin\stats\htdocs\index.php(8): PDO->exec('CREATE TABLE IF...') #1 {main} thrown in D:\Projects\2013\Stat collection plugin\stats\htdocs\index.php on line 8

The code:

$dbSchema = file_get_contents('../schema.sql');

$PDO = new PDO('sqlite:../stats.db');
$PDO->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$PDO->exec($dbSchema);

Here's my schema. The error seems to be there when I just have the first CREATE TABLE statement, but if I've made the same error in the following statements I'd appreciate it if you told me. Thanks in advance!

CREATE TABLE IF NOT EXISTS game (
    id              INTEGER PRIMARY KEY AUTOINCREMENT,
    serverName      STRING NOT NULL,
    map             STRING NOT NULL,
    winner          INTEGER NOT NULL,
    gameMode        STRING NOT NULL,
    controlPoints   INTEGER,
    setupGate       BOOLEAN,
    capsRed         INTEGER,
    capsBlue        INTEGER,
    winsRed         INTEGER,
    winsBlue        INTEGER,
    CONSTRAINT game_winner_teamTypes_id FOREIGN KEY (winner) REFERENCES teamTypes(id)
);

CREATE TABLE IF NOT EXISTS player (
    id              INTEGER PRIMARY KEY AUTOINCREMENT,
    gameId          INTEGER NOT NULL,
    name            STRING NOT NULL,
    team            INTEGER NOT NULL,
    class           INTEGER NOT NULL,
    queueJump       BOOLEAN NOT NULL,
    CONSTRAINT player_gameId_game_id FOREIGN KEY (gameId) REFERENCES game(id),
    CONSTRAINT player_team_teamTypes_id FOREIGN KEY (team) REFERENCES teamTypes(id),
    CONSTRAINT player_class_classTypes_id FOREIGN KEY (class) REFERENCES classTypes(id)
);

CREATE TABLE IF NOT EXISTS stat (
    playerId        INTEGER NOT NULL,
    type            INTEGER NOT NULL,
    value           INTEGER NOT NULL,
    CONSTRAINT stat_playerId_player_id FOREIGN KEY (playerId) REFERENCES player(id),
    CONSTRAINT stat_type_statTypes_id FOREIGN KEY (type) REFERENCES statTypes(id)
);

CREATE TABLE IF NOT EXISTS teamTypes (
    id              INTEGER PRIMARY KEY AUTOINCREMENT,
    name            STRING NOT NULL
);

INSERT OR IGNORE INTO teamTypes(id, name) VALUES (0, 'Red'), (1, 'Blue'), (2, 'Spectator');

CREATE TABLE IF NOT EXISTS classTypes (
    id              INTEGER PRIMARY KEY AUTOINCREMENT,
    name            STRING NOT NULL
);

INSERT OR IGNORE INTO classTypes(id, name) VALUES (0, 'Runner'), (1, 'Rocketman'), (2, 'Rifleman'), (3, 'Detonator'), (4, 'Healer'), (5, 'Constructor'), (6, 'Overweight'), (7, 'Infiltrator'), (8, 'Firebug'), (9, 'Querly');

CREATE TABLE IF NOT EXISTS statTypes (
    id              INTEGER PRIMARY KEY AUTOINCREMENT,
    name            STRING NOT NULL
);

INSERT OR IGNORE INTO statTypes(id, name) VALUES (0, 'Kills'), (1, 'Deaths'), (2, 'Caps'), (3, 'Assists'), (4, 'Destruction'), (5, 'Stabs'), (6, 'Healing'), (7, 'Defenses'), (8, 'Invulns'), (9, 'Bonus'), (10, 'Dominations'), (11, 'Revenge'), (12, 'Points');

回答1:


Turns out that the problem was that I had multiple VALUES groups (VALUES (1,2,3), (4,5,6)).

The feature enabling multiple rows to be inserted with one statement (INSERT INTO foobar VALUES (1,2), (3,4);) instead of INSERT INTO foobar VALUES (1,2); INSERT INTO foobar VALUES (3,4); was added in SQLite 3.7.11:

2012-03-20 (3.7.11)

Enhance the INSERT syntax to allow multiple rows to be inserted via the VALUES clause.

http://www.sqlite.org/changes.html#version_3_7_11

However, my SQLite version is older. According to my phpinfo() output:

pdo_sqlite

PDO Driver for SQLite 3.x => enabled
SQLite Library => 3.7.7.1

Hence I can't use it.

When I changed this last part,

CREATE TABLE IF NOT EXISTS teamTypes (
    id              INTEGER PRIMARY KEY AUTOINCREMENT,
    name            STRING NOT NULL
);

INSERT OR IGNORE INTO teamTypes(id, name) VALUES (0, 'Red'), (1, 'Blue'), (2, 'Spectator');

CREATE TABLE IF NOT EXISTS classTypes (
    id              INTEGER PRIMARY KEY AUTOINCREMENT,
    name            STRING NOT NULL
);

INSERT OR IGNORE INTO classTypes(id, name) VALUES (0, 'Runner'), (1, 'Rocketman'), (2, 'Rifleman'), (3, 'Detonator'), (4, 'Healer'), (5, 'Constructor'), (6, 'Overweight'), (7, 'Infiltrator'), (8, 'Firebug'), (9, 'Querly');

CREATE TABLE IF NOT EXISTS statTypes (
    id              INTEGER PRIMARY KEY AUTOINCREMENT,
    name            STRING NOT NULL
);

INSERT OR IGNORE INTO statTypes(id, name) VALUES (0, 'Kills'), (1, 'Deaths'), (2, 'Caps'), (3, 'Assists'), (4, 'Destruction'), (5, 'Stabs'), (6, 'Healing'), (7, 'Defenses'), (8, 'Invulns'), (9, 'Bonus'), (10, 'Dominations'), (11, 'Revenge'), (12, 'Points');

to this,

CREATE TABLE IF NOT EXISTS teamTypes (
    id              INTEGER PRIMARY KEY AUTOINCREMENT,
    name            STRING NOT NULL
);

INSERT OR IGNORE INTO teamTypes(id, name) VALUES (0, 'Red');
INSERT OR IGNORE INTO teamTypes(id, name) VALUES (1, 'Blue');
INSERT OR IGNORE INTO teamTypes(id, name) VALUES (2, 'Spectator');

CREATE TABLE IF NOT EXISTS classTypes (
    id              INTEGER PRIMARY KEY AUTOINCREMENT,
    name            STRING NOT NULL
);

INSERT OR IGNORE INTO classTypes(id, name) VALUES (0, 'Runner');
INSERT OR IGNORE INTO classTypes(id, name) VALUES (1, 'Rocketman');
INSERT OR IGNORE INTO classTypes(id, name) VALUES (2, 'Rifleman');
INSERT OR IGNORE INTO classTypes(id, name) VALUES (3, 'Detonator');
INSERT OR IGNORE INTO classTypes(id, name) VALUES (4, 'Healer');
INSERT OR IGNORE INTO classTypes(id, name) VALUES (5, 'Constructor');
INSERT OR IGNORE INTO classTypes(id, name) VALUES (6, 'Overweight');
INSERT OR IGNORE INTO classTypes(id, name) VALUES (7, 'Infiltrator');
INSERT OR IGNORE INTO classTypes(id, name) VALUES (8, 'Firebug');
INSERT OR IGNORE INTO classTypes(id, name) VALUES (9, 'Querly');

CREATE TABLE IF NOT EXISTS statTypes (
    id              INTEGER PRIMARY KEY AUTOINCREMENT,
    name            STRING NOT NULL
);

INSERT OR IGNORE INTO statTypes(id, name) VALUES (0, 'Kills');
INSERT OR IGNORE INTO statTypes(id, name) VALUES (1, 'Deaths');
INSERT OR IGNORE INTO statTypes(id, name) VALUES (2, 'Caps');
INSERT OR IGNORE INTO statTypes(id, name) VALUES (3, 'Assists');
INSERT OR IGNORE INTO statTypes(id, name) VALUES (4, 'Destruction');
INSERT OR IGNORE INTO statTypes(id, name) VALUES (5, 'Stabs');
INSERT OR IGNORE INTO statTypes(id, name) VALUES (6, 'Healing');
INSERT OR IGNORE INTO statTypes(id, name) VALUES (7, 'Defenses');
INSERT OR IGNORE INTO statTypes(id, name) VALUES (8, 'Invulns');
INSERT OR IGNORE INTO statTypes(id, name) VALUES (9, 'Bonus');
INSERT OR IGNORE INTO statTypes(id, name) VALUES (10, 'Dominations');
INSERT OR IGNORE INTO statTypes(id, name) VALUES (11, 'Revenge');
INSERT OR IGNORE INTO statTypes(id, name) VALUES (12, 'Points');

the problem was solved.



来源:https://stackoverflow.com/questions/19340535/multiple-inserts-not-working-with-sqlite-3

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!