I would love to see a new helper method that would return a BOOL if the table name I pass it exists in the database or not.
@bassgzm Can I ask why? Usually you don't need to check for a table to exist. For migrations you can use PRAGMA user_version to track your database version and create the table in a specific migration. You could also use an IF NOT EXISTS clause (in a CREATE TABLE statement).
If your particular app really needs this functionality, you could write your own, _e.g._:
extension Database {
func tableExists(tableName: String) -> Bool {
return db.scalar(
"SELECT EXISTS(SELECT name FROM sqlite_master WHERE name = ?)", tableName
) as Bool
}
}
if db.tableExists("users") {
// ...
}
Going to close this for now, but if you could explain a compelling reason for this to belong in SQLite.swift, please let me know and I'll consider it.
If I run the db.execute(create table script goes here) I get an exception saying the table already exists. I am pretty new to iOS development and Swift, thanks for the sample code it looks like it will work out fine.
@bassgzm If you're trying to avoid the exception, what you actually want is CREATE TABLE IF NOT EXISTS. See:
@stephencelis
The above code didn't work
Please check the below.
extension Database {
func tableExists(tableName: String) -> Bool {
let count:Int64 = self.scalar(
"SELECT EXISTS(SELECT name FROM sqlite_master WHERE name = ?)", tableName
) as! Int64
if count>0{
return true
}
else{
return false
}
}
}
@vineethvijayan Need more context/information. Do you have an error message, etc.?
Yes, @stephencelis when I tried your code it was trying ur code and it was trying to convert int64 to bool which was throwing an error, so just converted it in the above way. Hope it is correct(It worked for me).
@vineethvijayan Gotcha. You should be able to remove DataManager.db. to ensure scalar gets called on self (the database instance).
@stephencelis Sorry about that I have a scalar variable DataManager.db which I use as a singleton object. actually you don't need it. Will update code. Thanks :)
I have a use case for this. In my app I'm building the database dynamically in code and keeping track of my own version in the database itself. In this way I don't always have the tables created up front and would need to know if an object exists prior to attempting to use it.
With the absense of error handling in Swift, for now at least, if I try to access the table I get an exception "no such table"
@blkbam Have you looked into querying against the sqlite_master table for table data? Something like the following should work:
func tableExists(tableName: String) {
return db.scalar(
"SELECT EXISTS (SELECT * FROM sqlite_master WHERE type = 'table' AND name = ?)",
tableName
) as! Int64 > 0
}
@stephencelis I'm using something like that now to get me by. I just thought I would throw in my 2 cents on having it be a part of the library.
Most helpful comment
@blkbam Have you looked into querying against the
sqlite_mastertable for table data? Something like the following should work: