Certainly! Here’s an example of Swift code that demonstrates how to delete data from a SQLite database using the SQLite.swift library:
import SQLite
// Create a connection to the SQLite database
let db = try Connection("path_to_your_database")
// Create a table object
let yourTable = Table("your_table")
// Define the columns in the table
let conditionColumn = Expression<String>("condition_column")
do {
// Define the delete statement
let delete = yourTable.filter(conditionColumn == "ConditionValue").delete
// Execute the delete statement
let rowsDeleted = try db.run(delete)
print("Data deleted successfully. Rows affected: \(rowsDeleted)")
} catch {
print("Error deleting data: \(error)")
}
Make sure to replace the following placeholders with your own values:
path_to_your_database
: The file path or URL to your SQLite database file.your_table
: The name of the table from which you want to delete data.condition_column
: The name of the column to use as a condition for deleting the data."ConditionValue"
: The specific value to match for deletion.
This code uses the SQLite.swift library to establish a connection to the SQLite database, define the table and columns, create a delete statement with a condition, execute the statement, and handle any potential errors that may occur. The code also outputs the number of affected rows upon successful deletion.
Make sure to include the SQLite.swift library in your Swift project. You can add it using Swift Package Manager or Cocoapods, depending on your project’s setup.