multiple SQL statements

55 views
Skip to first unread message

Conversation Exchange Help

unread,
Nov 6, 2017, 12:39:24 AM11/6/17
to f3-fra...@googlegroups.com
Hi,
I am new to F3. Reading the DB notes about transactions I came across this statement:

$db->exec( array( 'DELETE FROM diet WHERE food="cola"', 'INSERT INTO diet (food) VALUES ("carrot")', 'SELECT * FROM diet' ) );

I was wondering if instead I could write the same statement in one single string as:

$db->exec(' DELETE FROM diet WHERE food="cola"; INSERT INTO diet (food) VALUES ("carrot"); SELECT * FROM diet;' );

This would make it easier to copy multiple statements from a SQL file or to paste and test them on Toad.
Thanks!
Fed

xfra35

unread,
Nov 7, 2017, 2:35:22 AM11/7/17
to f3-fra...@googlegroups.com
Have you tried it?

If it doesn't work, you can easily workaround it with:

$sql='    

        DELETE FROM diet WHERE food="cola";
        INSERT INTO diet (food) VALUES ("carrot");
        SELECT * FROM diet;';
$db
->exec(explode(';',rtrim($sql,'; ')));

Conversation Exchange Help

unread,
Nov 16, 2017, 1:58:46 AM11/16/17
to Fat-Free Framework
Thanks man,
I will try even though it does not seem safe. Imagine a scenario like:

INSERT INTO diet (food) VALUES("carrot; salad; broccoli;");

The explode would chop that insert in in 3 pieces!

xfra35

unread,
Nov 16, 2017, 4:38:25 AM11/16/17
to f3-fra...@googlegroups.com
That's right.

Anyway, you could refine the splitting like this:

$sql='

    INSERT INTO diet (food) VALUES("carrot; salad; broccoli;");
    DELETE FROM diet WHERE food="cola";
    INSERT INTO diet (food) VALUES ("carrot");
    SELECT * FROM diet;'
;
$db
->exec(preg_split('/;(?=\v|$)/',$sql,NULL,PREG_SPLIT_NO_EMPTY));

(provided each statement is followed by a line feed)
Reply all
Reply to author
Forward
0 new messages