如何插入带有 PDO 扩展名的 SQLite? [英] How do I insert into SQLite with PDO extension?

查看:44
本文介绍了如何插入带有 PDO 扩展名的 SQLite?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在使用 pdo 插入 sqlite3 数据库时遇到了一些麻烦.你必须原谅我对 PDO 的无知,它似乎来自 Python 的数据库接口.

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 interface.

所以这是我的问题.我有一个简单的插入:

So here's my problem. I have a simple insert:

$dbh = new PDO('sqlite:vets.db');
    $count = $dbh->exec("INSERT INTO vets(name,email,clinic,streetname,citystatezip,link,phone,fax,animal,region,visible) VALUES ($name,$email,$clinic,$streetname,$citystatezip,$link,$phone,$fax,$animal,$region,$visible)");
    $dbh = null;

我只想在我的数据库上执行该 SQL 命令并完成它.尽管执行此脚本不会导致任何错误,但它永远不会更新数据库.我已经尝试了对数据库本身的各种权限,甚至将其设为 777,但没有任何区别.

I simply want to execute that SQL command on my database and be done with it. Though executing this script causes no errors, it never updates the database. I've tried all sorts of permissions on the database itself, even made it 777 but it doesn't make a difference.

有人可以帮我吗?

推荐答案

PDO 的一大好处是您可以创建准备好的语句.这是我的一个 PHP 项目中的一些代码:

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.)

这篇关于如何插入带有 PDO 扩展名的 SQLite?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆