douzi7711 2014-05-14 08:38
浏览 175

使用MySQL中的事务插入数据时读取记录

Similar questions have been asked multiple times but I have gone through them and couldn't find a solution.

I am trying to import a simple CSV file containing around 500 orders into my database table. And I am using transactions as shown below.

        $dbAdapter->beginTransaction();
        $file = new SplFileObject($fileName, 'r');
        while(!$file->eof()){               
            try{
                $order = $file->fgetcsv();
                if(!$this->orderExists($order)){ 
                     $this->saveOrder($order);
                }
            } catch (Exception $ex) {
                $dbAdapter->rollBack();
                throw new Om_Model_Exception($ex->getMessage());
            }
        }
        $dbAdapter->commit();

This works fine. But the same CSV file can have duplicate orders and I need to skip them. To do that before inserting the order I am checking the database table to see if the order exists but this always returns false because the transaction is still running.

So my question is, is there a way to check for inserted records before the transaction is committed? (May be MySQL stores data in to a temporary table and I can query that).

I can solve this by storing orders into an array and checking the array for duplicates but this seems like a waste of lot of memory. And might not work properly if the order file is large.

Thanks for your help.

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 如何在scanpy上做差异基因和通路富集?
    • ¥20 关于#硬件工程#的问题,请各位专家解答!
    • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
    • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
    • ¥30 截图中的mathematics程序转换成matlab
    • ¥15 动力学代码报错,维度不匹配
    • ¥15 Power query添加列问题
    • ¥50 Kubernetes&Fission&Eleasticsearch
    • ¥15 報錯:Person is not mapped,如何解決?
    • ¥15 c++头文件不能识别CDialog