stephencelis / SQLite.swift

A type-safe, Swift-language layer over SQLite3.
MIT License
9.57k stars 1.55k forks source link

Problem inserting data #1116

Open eolverabea opened 2 years ago

eolverabea commented 2 years ago

I try to insert a row in a table, sometimes it inserts the data and sometimes it doesn't and sends this error

Inserted data:

Ticket(id: 0, price: 3.0, date_first_used: 2022-03-02 20:11:53 +0000, used: 0, product_id: 2, history_id: 16, is_first_used: 0, date_expiration: " ", ws_product_id: "2", product_days: 1, exp_annual: "03-02-2023 14:11:53")
true

data not inserted:

Ticket(id: 0, price: 3.0, date_first_used: 2022-03-02 19:23:11 +0000, used: 0, product_id: 2, history_id: 14, is_first_used: 0, date_expiration: " ", ws_product_id: "2", product_days: 1, exp_annual: "03-02-2023 13:23:11")
2022-03-02 13:23:11.435208-0600 Transit[1959:416920] [logging] near "WHERE": syntax error in "INSERT INTO "ticket" ("price", "used", "product_id", "history_id", "date_first_used", "date_expiration", "ws_product_id", "product_days", "exp_annual") VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?) WHERE ("id" = ?)"
Insert Ticket failed: near "WHERE": syntax error (code: 1)

func inserta data:

 static func insertTicket(ticketValues: Ticket) -> Bool?{
        guard let datebase = SQLiteDataBase.sharedInstance.database
        else {
            print("Datastore conection error")
            return nil
        }

        do{
            try datebase.run(tableTicket.insert(
                price <- ticketValues.price,
                used <- ticketValues.used,
                product_id <- ticketValues.product_id,
                history_id <- ticketValues.history_id,
                date_first_used <- ticketValues.date_first_used,
                date_expiration <- ticketValues.date_expiration,
                ws_product_id <- ticketValues.ws_product_id,
                product_days <- ticketValues.product_days,
                exp_annual <- ticketValues.exp_annual))
            return true
        }catch let Result.error(message, code, statement) where
                code == SQLITE_CONSTRAINT{
            print("Insert Ticket failed: \(message), in \(String(describing: statement))")
            return false
        }catch let error{
            print("Insert Ticket failed: \(error)")
            return false
        }
    }
nathanfallet commented 2 years ago

There shouldn't be a WHERE ("id" = ?) in the SQL request generated by the insertTicket(ticketValues:) function. Are you sure that you are calling the same function to insert both Tickets?