Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CREATE TABLE partially fails with PHP + PDO for Sqlite

Tags:

php

sqlite

pdo

I am creating tables in a SQLite database from a database dump made by Navicat.

Of the 3 CREATE TABLE queries I'm running, 2 fail, 1 works - so it's not a permission / connection / config issue.

Here is the SQL:

CREATE TABLE "servers" (
"id"  INTEGER NOT NULL,
"name"  TEXT(32) NOT NULL,
"type"  INTEGER NOT NULL,
"host"  TEXT(128) NOT NULL,
"port"  INTEGER NOT NULL,
"rcon_port"  INTEGER NOT NULL,
PRIMARY KEY ("id")
);

CREATE TABLE "config" ("built"  INTEGER NOT NULL);

CREATE TABLE "users" (
"id"  INTEGER NOT NULL,
"username"  TEXT(32) NOT NULL,
"password"  TEXT(64) NOT NULL,
"salt"  TEXT(8) NOT NULL,
"last_login"  INTEGER NOT NULL,
PRIMARY KEY ("id")
);

Trying to run this - only the config table is created.

I can execute this script with Navicat and the tables are created - is there something syntactically wrong about this?

Also no errors / exceptions are being thrown

The PHP:

$path and $sql are confirmed to be correct

$db_conn = 'sqlite:' . $path;

try {
    $dbh = new PDO($db_conn);
    $dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

    $dbh->query(file_get_contents($sql);
} catch (Exception $e) {
    var_dump($e);
    exit;
}
like image 328
cloakedninjas Avatar asked Dec 18 '25 03:12

cloakedninjas


1 Answers

Try separating the file into individual queries and running those:

$db_conn = 'sqlite:' . $path;
$queries = file_get_contents($sql);

try {
    $dbh = new PDO($db_conn);
    $dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    $queries = explode(";", $queries);
    foreach ($queries as $query) {
        $dbh->query($query);
    }
} catch (Exception $e) {
    var_dump($e);
    exit;
}
like image 182
Madara's Ghost Avatar answered Dec 19 '25 20:12

Madara's Ghost