How do I insert into PDO (sqllite3)?

后端 未结 2 522
刺人心
刺人心 2021-02-05 20:30

I\'m having a bit of trouble inserting into a sqlite3 database with pdo. You\'ll have to excuse my ignorance with PDO, it seems so foreign coming from Python\'s database interfa

2条回答
  •  渐次进展
    2021-02-05 20:44

    One of the great benefits of PDO is that you can create prepared statements. Here's some code from a PHP project of mine:

    $qry = $db->prepare(
        'INSERT INTO twocents (path, name, message) VALUES (?, ?, ?)');
    $qry->execute(array($path, $name, $message));
    

    As you can see, I use ? where I want to insert a value, then I execute the query with an array of values that should be put in place of the question marks.

    If you do this, your query will be much safer, and more likely to work (since a missing value would stop your query from working if you insert variables directly in the query like you do.)

提交回复
热议问题