如何插入PDO (sqllite3)?

时间:2022-04-22 23:06:02

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.

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

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;

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.

我只是想在我的数据库上执行SQL命令,然后完成它。虽然执行此脚本不会导致错误,但它不会更新数据库。我已经尝试了数据库本身的各种权限,甚至让它成为777,但这并没有什么不同。

Could someone help me?

有人能帮我吗?

2 个解决方案

#1


21  

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

PDO的最大好处之一是可以创建准备好的语句。下面是我的一个PHP项目的代码:

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

如果您这样做,您的查询将会更加安全,并且更有可能工作(因为如果您像您这样直接插入变量,那么丢失的值将停止您的查询。)

#2


2  

You can have an error in your SQL query. You could print it and then try to execute it in some SQLite GUI interface like SQLite Database Browser.

您可以在SQL查询中出现错误。您可以将其打印出来,然后尝试在SQLite数据库浏览器这样的SQLite GUI界面中执行它。

// you can skip PDO part for now, because we know it doesn't work
// $dbh = new PDO('sqlite:vets.db');
$query = "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)";
echo $query;
// $count = $dbh->exec($query);
// $dbh = null;

I see that you are not wrapping your values in quotes, probably that's the source of the problem. Maybe some typos in table field names as well. All will come out once you actually see the query.

我看到你没有把你的价值用引号括起来,这可能是问题的根源。也可能是表字段名中的一些拼写错误。当您实际看到查询时,所有内容都将显示出来。

#1


21  

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

PDO的最大好处之一是可以创建准备好的语句。下面是我的一个PHP项目的代码:

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

如果您这样做,您的查询将会更加安全,并且更有可能工作(因为如果您像您这样直接插入变量,那么丢失的值将停止您的查询。)

#2


2  

You can have an error in your SQL query. You could print it and then try to execute it in some SQLite GUI interface like SQLite Database Browser.

您可以在SQL查询中出现错误。您可以将其打印出来,然后尝试在SQLite数据库浏览器这样的SQLite GUI界面中执行它。

// you can skip PDO part for now, because we know it doesn't work
// $dbh = new PDO('sqlite:vets.db');
$query = "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)";
echo $query;
// $count = $dbh->exec($query);
// $dbh = null;

I see that you are not wrapping your values in quotes, probably that's the source of the problem. Maybe some typos in table field names as well. All will come out once you actually see the query.

我看到你没有把你的价值用引号括起来,这可能是问题的根源。也可能是表字段名中的一些拼写错误。当您实际看到查询时,所有内容都将显示出来。