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

报告相同问题?

悬赏问题

  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)