letitbefornow
letitbefornow

Reputation: 425

How to delete table or update column with SQLite.swift ?

I need to re-create my table in my database. Is there anyway to update table's columns or delete table with SQLite.swift ?

Upvotes: 2

Views: 7149

Answers (2)

Muffin Man
Muffin Man

Reputation: 81

Swift 5

If you're willing to use sqlite.swift cocoa pods

Replace the path with the path to your database. This is the project's Documents directory.

Replace the "table" string with the name of your table.

let path = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)

let db = try Connection("\(path.first ?? "")/db.sqlite3")       
let table = Table("table")
            
let drop = table.drop(ifExists: true)
try db.run(drop)

Upvotes: 0

area28
area28

Reputation: 1425

Link to docs

Assuming you have your variable for the open database:

let db = Database("path/to/db.sqlite3")

// what table to drop and recreate
db.drop(table: yourTable, ifExists: true)

And for altering a table

db.alter(table: yourTable, add: suffix)

Upvotes: 4

Related Questions