I need to re-create my table in my database. Is there anyway to update table's columns or delete table with SQLite.swift ?
Asked
Active
Viewed 6,768 times
2
-
Are you using [this](https://github.com/stephencelis/SQLite.swift/blob/master/Documentation/Index.md)? There should be methods for that in any case. – area28 Sep 14 '15 at 20:13
-
Yes that one. I checked carefully the documentation but couldn't find anything about deleting or updating table. In the documentation they describe deleting rows and etc. – letitbefornow Sep 14 '15 at 20:18
-
In particular there is [this section](https://github.com/stephencelis/SQLite.swift/blob/master/Documentation/Index.md#user-content-dropping-tables) if you search for dropping tables. – area28 Sep 14 '15 at 20:20
-
I also just notice that.. Thank you. – letitbefornow Sep 14 '15 at 20:20
2 Answers
4
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)

area28
- 1,425
- 1
- 9
- 11
0
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)

Muffin Man
- 41
- 3