I am using FMDB as a dependency in my cocoapod. Insert,delete and select operations works fine but i am not able to update a row.
Here is my code
Database and table creation
let filemgr = FileManager.default
let dirPaths = filemgr.urls(for: .documentDirectory,
in: .userDomainMask)
let databasePath = dirPaths[0].appendingPathComponent("database.sqlite").path
defaults.set(String(databasePath), forKey: "databasePath")
let dbObj = FMDatabase(path: defaults.value(forKey: "databasePath") as! String)
if dbObj == nil {
print("Error: \(dbObj?.lastErrorMessage())")
}
if (dbObj?.open())!
{
let sql_stmt = "CREATE TABLE IF NOT EXISTS TABLE_TICKET (ID INTEGER PRIMARY KEY AUTOINCREMENT, TICKET_SUBJECT TEXT, TICKET_DESCRIPTION TEXT, DEPARTMENT_ID TEXT, PRIORITY_ID TEXT NOT NULL, TYPE_ID TEXT NOT NULL, TICKET_ATTACHMENT TEXT, STAFF_NAME TEXT NOT NULL, TICKET_NO TEXT NOT NULL, TICKET_ID TEXT NOT NULL, TICKET_STATUS TEXT NOT NULL,TICKET_OFFLINE_FLAG TEXT NOT NULL,TICKET_DATE TEXT NOT NULL, TICKET_UPDATE_TIME TEXT )"
if !(dbObj?.executeStatements(sql_stmt))! {
print("Error: \(dbObj?.lastErrorMessage())")
}
dbObj?.close()
}
else
{
print("Error: \(dbObj?.lastErrorMessage())")
}
update operation
if (dbObj?.open())!
{
let updatedTime = String(describing: json["response"]["CurrentDateTime"])
print(updatedTime)
let deleteSQL = "UPDATE TABLE_TICKET SET ticket_update_time = '\(updatedTime)' WHERE ticket_id ='\(self.currentTicketID!)'"
do {
try dbObj?.executeUpdate("UPDATE TABLE_TICKET SET ticket_update_time=? WHERE ticket_id=?", withArgumentsIn: [updatedTime, self.currentTicketID!])
}
catch {
print(error)
}
} else {
print("Error: \(dbObj?.lastErrorMessage())")
}
Upon searching for this issue i found that the db must be copied to a specific location so that it won't be read only. If so am i storing it in a wrong location?
Note: I didn't get any error while executing this code. updated column has blank value after updation (inserted as blank).
the location is fine as long as you are comfortable with it being backed up to iCloud:
As far as updating, if you're able to insert, I'd think your database isn't read only. In my experience, I had to set permissions on any new database files that I downloaded, like so:
I would double check the data and the queries if only update queries are failing.