在for循环中插入查询会出错:数据库已被锁定

时间:2018-03-30 05:20:15

标签: php for-loop sqlite transactions

我有一个for循环,里面有一个SQLite插入查询。它给了我一个错误"数据库被锁定"。我在此代码末尾的try块内循环,其中有一个for循环,我将一些数据插入到数据库中。我有一个错误,即行号。 904 这是我的代码:

        $products = $_REQUEST['Products'];
        $cname = $_REQUEST['cname'];
        $total = $_REQUEST['totalAmt'];
        $paid = $_REQUEST['paidAmt'];
        $bill_id = $_REQUEST['billId'];
        $customer_id = $_REQUEST['mobile'];
        $curr_date_time = date("Y-m-d H:i:s");
        $tid = "";
        $total_payment = 0;
        $snAbr = explode(" ",$cname);
        foreach($snAbr as $val){
            $tid .= substr($val,0,1);
        }
        $tid .= date("dmyis");
        $prod_arr = explode("-", $products);
        $mop = $_REQUEST['modeOfPayment'];
        if(!empty($cname) AND !empty($total) AND !empty($customer_id)){

            $validate_cust_sql = 'SELECT mobile FROM customers WHERE mobile = "$customer_id"';
            $validate_cust_result = $conn->query($validate_cust_sql);
            $sql_cbal = "SELECT balance FROM cash_transactions ORDER BY transaction_date DESC LIMIT 1";
            $result_cbal = $conn->query($sql_cbal);
            if(num_rows($result_cbal)>0){ 
                while($row_cbal=$result_cbal->fetchArray(SQLITE3_ASSOC)){
                    $cbal = $row_cbal['balance'];
                }
            }
            else{
                $cbal = 0;
            }
            $sql_bbal = "SELECT bbalance FROM bank_transactions ORDER BY btransaction_date DESC LIMIT 1";
            $result_bbal = $conn->query($sql_bbal);
            if(num_rows($result_bbal)>0){ 
                while($row_bbal=$result_bbal->fetchArray(SQLITE3_ASSOC)){
                    $bbal = $row_bbal['bbalance'];
                }
            }
            else{
                $bbal = 0;
            }
            switch($mop){
                case "both":
                    $cash_n_card_amt = explode("," ,$paid);
                    $cash_amt = $cash_n_card_amt[0];
                    $card_amt = $cash_n_card_amt[1];
                    $total_payment = $cash_amt+$card_amt;
                    $cbal += $cash_amt;
                    $bbal += $card_amt;
                    $sql_bill_info = "INSERT INTO bill_info (bill_id, bill_date, customer_id, total, paid)
                                    VALUES('$bill_id','$curr_date_time','$customer_id','$total','$total_payment')";

                    if(!empty($total_payment)){
                        $cash_transactions_sql = "INSERT INTO cash_transactions (transaction_id, customer_id, bill_id, remarks, type, amount, balance, transaction_date)
                                            VALUES('$tid','$customer_id','$bill_id','sale','in','$cash_amt','$cbal','$curr_date_time')";
                        $bank_transactions_sql = "INSERT INTO bank_transactions (btransaction_id, bcustomer_id, bbill_id, bremarks, btype, bamount, bbalance, btransaction_date)
                                            VALUES('$tid','$customer_id','$bill_id','sale','in','$card_amt','$bbal','$curr_date_time')";
                    }
                break;
                case "cash":
                    $cbal += $paid;
                    $sql_bill_info = "INSERT INTO bill_info (bill_id, bill_date, customer_id, total, paid)
                                    VALUES('$bill_id','$curr_date_time','$customer_id','$total','$paid')";

                    if(!empty($paid)){
                        $transactions_sql = "INSERT INTO cash_transactions (transaction_id, customer_id, bill_id, remarks, type, amount, balance, transaction_date)
                                            VALUES('$tid','$customer_id','$bill_id','sale','in','$paid','$cbal','$curr_date_time')";
                    }
                break;
                case "card":
                    $bbal += $paid;
                    $sql_bill_info = "INSERT INTO bill_info (bill_id, bill_date, customer_id, total, paid)
                                    VALUES('$bill_id','$curr_date_time','$customer_id','$total','$paid')";

                    if(!empty($paid)){
                        $transactions_sql = "INSERT INTO bank_transactions (btransaction_id, bcustomer_id, bbill_id, bremarks, btype, bamount, bbalance, btransaction_date)
                                            VALUES('$tid','$customer_id','$bill_id','sale','in','$paid','$bbal','$curr_date_time')";
                    }
                break;
                case "":
                    $cbal += $paid;
                    $sql_bill_info = "INSERT INTO bill_info (bill_id, bill_date, customer_id, total, paid)
                                    VALUES('$bill_id','$curr_date_time','$customer_id','$total','$paid')";

                    if(!empty($paid)){
                        $transactions_sql = "INSERT INTO cash_transactions (transaction_id, customer_id, bill_id, remarks, type, amount, balance, transaction_date)
                                            VALUES('$tid','$customer_id','$bill_id','sale','in','$paid','$cbal','$curr_date_time')";
                    }
                break;
            }

            try{
                $conn->query("BEGIN;");

                //sales
                for($i=0;$i<count($prod_arr);$i++){
                    $prod_obj = $prod_arr[$i];
                    $obj_to_arr = explode("/", $prod_obj);
                    for($j=0;$j<1;$j++){
                        $sql_sales = "INSERT INTO sales (sale_date, bill_id, customer_id, products, quantity, discount, amount, status)
                        VALUES('$curr_date_time','$bill_id','$customer_id','$obj_to_arr[0]','$obj_to_arr[1]', '$obj_to_arr[2]', '$obj_to_arr[3]', 'sale');";
                        if($conn->query($sql_sales) == false){  //Line 904. Error Here
                            throw new Exception($conn->lastErrorMsg());
                        }
                    }
                }
                //customers
                if(num_rows($validate_cust_result) == 0){
                    $sql_customer = "INSERT INTO customers (name, mobile) VALUES('$cname','$customer_id')";
                    if($conn->query($sql_customer) == false) throw new Exception($conn->lastErrorMsg());
                }
                //if mop == both
                if(!empty($total_payment)){
                    if($conn->query($cash_transactions_sql) == FALSE || $conn->query($bank_transactions_sql) == FALSE) throw new Exception($conn->lastErrorMsg());
                }

                if(!empty($paid)){
                    if($conn->query($transactions_sql) == false) throw new Exception($conn->lastErrorMsg());
                }

                if($conn->query($sql_bill_info)){   ?>
                    <div class='alert alert-success'>Record Successfully Entered</div> <?php
                }
                else{
                    throw new Exception($conn->lastErrorMsg());
                }

                $conn->query("COMMIT;");
            }
            catch(Exception $e){
                $conn->query('ROLLBACK;');
                echo "<div class='alert alert-danger'>ERROR! ".$e->getMessage()."</div>";
            }
        }
        else echo "<div class='alert alert-danger'> All fields are mandatory </div>";

上面的代码抛出异常

  

警告:SQLite3 :: query():无法执行语句:数据库是   锁定在F:\ INSTALLED \ XAMPP \ htdocs \ SA \ ajax-req-handler.php上线   904

每次我运行上面的代码

1 个答案:

答案 0 :(得分:0)

您可以参考以下链接

// Source: https://www.sqlite.org/wal.html
$db->exec('PRAGMA journal_mode = wal;');