Skip to content

Instantly share code, notes, and snippets.

@zgchurch
Created June 26, 2014 17:32
Show Gist options
  • Star 11 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save zgchurch/3bac5443f6b3f407fe65 to your computer and use it in GitHub Desktop.
Save zgchurch/3bac5443f6b3f407fe65 to your computer and use it in GitHub Desktop.
Using SQLite3 from Swift
// 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>
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
}
}
@Ngasuplo
Copy link

Hi zgchurch! so in this way where and how i can setting file bridgingheader.h in my swift project

@zgchurch
Copy link
Author

zgchurch commented Jul 1, 2014

See the comment at the top of BridgingHeader.h - get to build settings by clicking on your project in the left project drawer with the "Project Navigator" open (Command + 1)

@0xhacking
Copy link

// 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

Hi, zgchurch , i want to know about this line [ // 3. Double-click and type "BridgingHeader.c" ] , "BridgingHeader.c" or "BridgingHeader.h" .
where is BridgingHeader.c?

@ekuester
Copy link

ekuester commented Jun 9, 2017

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment