catfan / Medoo

The lightweight PHP database framework to accelerate the development.
https://medoo.in
MIT License
4.84k stars 1.15k forks source link

Cannot insert multiple commands into a prepared statement #897

Closed sevakalashnikov closed 3 years ago

sevakalashnikov commented 4 years ago

Hello.

How can I import init db file with multiple postgres create and insert statements? I'm currently doing

$installFile = $scriptPath . DIRECTORY_SEPARATOR . 'install/sql' . DIRECTORY_SEPARATOR . 'postgres.sql';
    $executed = $db->query(Medoo::raw(file_get_contents($installFile)));

which throws Cannot insert multiple commands into a prepared statement error

Information

Detail Code Cannot insert multiple commands into a prepared statement

Expected output Import db dump file

catfan commented 4 years ago

Firstly, you don't need to build raw object for query().

Just put string as first parameter.

$db->query(file_get_contents($installFile));

And if still throws multiple command error, that's database engine not supported it. Just check out the database doc or setting.

sevakalashnikov commented 4 years ago

I tried both with and without Medoo::raw and still fails on a file with multiple statements like..

CREATE TABLE IF NOT EXISTS log (
  id SERIAL,
  inventory_id INTEGER NOT NULL,
  user_id INTEGER,
  item_id INTEGER,
  action TEXT,
  timestamp TIMESTAMP NOT NULL,
  PRIMARY KEY (id)
);

CREATE INDEX inventory_id ON log ("inventory_id");
CREATE INDEX user_id ON log ("user_id");
catfan commented 3 years ago

That because the database didn't support that.

Just split it all into a single query and execute it alone.