在完全相同的行上插入2个不同的sql插入语句

问题描述 投票:0回答:1
def placeOrder_2():
    purchase_amount = int(input("Enter the amount different products you would like to purchase: "))

    for x in range(purchase_amount):
        p_id = input("Enter the ID of the product you would like to purchase: ")
        p_quantity = int(input("Enter the quantity you would like to purchase"))

        sqlstring = """
        INSERT INTO tblOrder_Line (costWhenPurchased)
        SELECT unitPrice FROM tblInventory
        WHERE itemID = ? VALUES(?)
                     """
        values = (p_id,)
        runsql(sqlstring, values)
        placeOrder_3(p_id, p_quantity)


def placeOrder_3(p_id, p_quantity):
    sqlstring = """
    INSERT INTO tblOrder_Line(orderNo, itemID, quantity) VALUES (?,?,?)
                """
    values = (p_id, p_quantity)
    runsql(sqlstring, values)
    placeOrder_4(p_id, p_quantity)

当我这样做时,它会插入值,但在一行上它只会有“costWhenPurchased”,其他值为空,然后在它下面的行上,它将有其他 2 个值,它们应该在同一行中作为购买时的成本

table example

sqlite
1个回答
0
投票

当我将数据保存到 SQLite DB 时,会调用此函数 收集变量并插入它们

   Private Sub InsertTxData()

    '===============================================
    tbNewBal.Text = NewBal.ToString("#0.00")
    '=== This REMOVES the comma before SAVING
    '================================================

    Using conn As New SQLiteConnection($"Data Source = '{gv_dbName}';Version=3;")
        conn.Open()

        Using cmd As New SQLiteCommand
            cmd.Connection = conn
            Try
                cmd.CommandText = "INSERT INTO TxData (txSortDate,txYear,txType,txAmount,txCKNum,txDesc,txBalance,txSearchMonth)VALUES (@txSortDate,@txYear,@txType,@txAmount,@txCKNum,@txDesc,@txBalance,@txSearchMonth)"

                cmd.Parameters.Add("@txSortDate", DbType.String).Value = tbDateToday.Text.Trim
                cmd.Parameters.Add("@txYear", DbType.String).Value = YEAR.Trim
                cmd.Parameters.Add("@txType", DbType.String).Value = tbTxType.Text.Trim
                cmd.Parameters.Add("@txAmount", DbType.String).Value = tbAmount.Text.Trim
                cmd.Parameters.Add("@txCKNum", DbType.String).Value = tbCKNum.Text.Trim
                cmd.Parameters.Add("@txDesc", DbType.String).Value = tbDesc.Text.Trim
                cmd.Parameters.Add("@txBalance", DbType.String).Value = tbNewBal.Text.Trim
                cmd.Parameters.Add("@txSearchMonth", DbType.Int64).Value = intSearchMonth
                cmd.ExecuteNonQuery()

            Catch ex As Exception
                MsgBox("Error " & ex.ToString)
            End Try
        End Using

    End Using

End Sub
© www.soinside.com 2019 - 2024. All rights reserved.