donglengyuan6826 2011-03-27 20:01
浏览 37
已采纳

如何为失败的sqlite插入生成基本CLI错误消息?

I'm using PHP for some simple shell scripting to convert existing data into SQLite tables on my local machine. However, when I run the scripts on the command line, I do not get errors even when the INSERT function clearly fails (i.e. the records aren't in the table when it's through). Is there an obvious solution to this?

The code below is a simplified version of what I'm using (which has more subloops, etc. which make it difficult to track down what exactly isn't being inserted in the tables). For example, if person's name has a newline char in it, it doesn't get inserted, but I have no error message to determine the problem. Also, I initially forgot to use the single quotes around the emp_name text field, and also did not get an error on the command line.

$db = new PDO('sqlite:mydatabase.sqlite');
$db->exec("CREATE TABLE employee_data (emp_id INTEGER, emp_name TEXT);");    
$db->beginTransaction();
foreach($people_data as $person) {
  $db->exec("INSERT INTO employee_data VALUES ('{$person->emp_id}', '{$person->emp_name}');");
}
$db->commit();
$db = NULL;

Thanks! Bryan

  • 写回答

4条回答 默认 最新

  • drahywu329376 2011-03-27 20:13
    关注

    Thanks for using PDO!

    PDO is silent by default. You might want to either enable Warnings mode or Exceptions mode using setAttribute, which will cause it to whine when you do something wrong. If you enable Warnings mode, then also make sure you can display those warnings, which can be done by the commands everyone else has posted.

    If you'd rather do it manually, there are errorCode and errorInfo methods that you can check after each query, the use of which is documented on the error mode page I linked above.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错
  • ¥15 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上
  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)
  • ¥15 相敏解调 matlab
  • ¥15 求lingo代码和思路
  • ¥15 公交车和无人机协同运输
  • ¥15 stm32代码移植没反应