特定错误,在PHP中使用带有PDO的SQLite DB:
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
代码:
$dbSchema = file_get_contents('../schema.sql');
$PDO = new PDO('sqlite:../stats.db');
$PDO->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$PDO->exec($dbSchema);
这是我的架构。当我只有第一个CREATE TABLE语句时,错误似乎就在那里,但是如果我在以下语句中犯了同样的错误,如果你告诉我,我会很感激。提前谢谢!
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');
答案 0 :(得分:3)
原来问题是我有多个VALUES组(VALUES (1,2,3), (4,5,6)
)。
在SQLite 3.7.11中添加了使用一个语句(INSERT INTO foobar VALUES (1,2), (3,4);
)而不是INSERT INTO foobar VALUES (1,2); INSERT INTO foobar VALUES (3,4);
插入多行的功能:
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
但是,我的SQLite版本较旧。根据我的phpinfo()输出:
pdo_sqlite
PDO Driver for SQLite 3.x => enabled
SQLite Library => 3.7.7.1
因此我无法使用它。
当我改变这最后一部分时,
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');
到此,
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');
问题解决了。