Created
June 26, 2014 17:32
-
-
Save zgchurch/3bac5443f6b3f407fe65 to your computer and use it in GitHub Desktop.
Using SQLite3 from Swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 1. Create this file in your Xcode project | |
// 2. Go to "Build Settings" and find "Objective-C Bridging Header." Use the search bar to find it quickly. | |
// 3. Double-click and type "BridgingHeader.c" | |
// If you get "Could not import Objective-C Header," try "my-project-name/BridgingHeader.h" | |
// 4. Go to "Build Phases," "Link Binary With Libraries," and add libsqlite3.0.dylib | |
#include <sqlite3.h> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Foundation | |
var db: COpaquePointer = nil; | |
if sqlite3_open("/Users/zchurch/Code/testapp/db/development.sqlite3", &db) != SQLITE_OK { | |
println("Failed to open file") | |
exit(1) | |
} | |
var sql = "SELECT * FROM schema_migrations" | |
var statement:COpaquePointer = nil | |
var tail: CString = "" | |
if sqlite3_prepare_v2(db, sql.bridgeToObjectiveC().UTF8String, sql.lengthOfBytesUsingEncoding(NSUTF8StringEncoding).bridgeToObjectiveC().intValue, &statement, &tail) != SQLITE_OK { | |
println("Failed to prepare statement") | |
exit(1) | |
} | |
func print_row(s: COpaquePointer) { | |
for i in 0..sqlite3_column_count(s) { | |
switch sqlite3_column_type(s, i) { | |
case SQLITE_INTEGER: | |
var r = sqlite3_column_int(s, i) | |
case SQLITE_TEXT: | |
var r = CString(sqlite3_column_text(s, i)) | |
println("String: \(r)") | |
default: | |
println("Other column type") | |
} | |
} | |
} | |
var complete = false | |
while complete == false { | |
switch sqlite3_step(statement){ | |
case SQLITE_DONE: | |
println("Done executing statement") | |
case SQLITE_ROW: | |
println("Row") | |
print_row(statement) | |
default: | |
println("Other") | |
complete = true | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hello Zach,
thanks for your code ideas. I am using them in listing the backup files of iOS devices, see https://github.com/ekuester/List-ManifestDB-From-iOSBackup. Regards, Erich Kuester