How to import sqlite in swift application

Hi,

Can any one point me to how to add sqlite to swift project?

I followed below staeps but still getting error "Use of undeclared type 'sqlite3'"

  1. Add a file Bridging-Header.h
  2. Go to the build settings tab for your project
  3. Find "Objective-C Bridging Header".
  4. Enter the name and path for the file you created in step one.


Anything I am missing here?


Thanks in advance

Replies

What do you write in the Bridging Header?

At least you need a single line like this:

#import "sqlite3.h"

You can confirm if it's working or not by writing:

        print(SQLITE_VERSION)//->3.8.10.2


I followed below staeps but still getting error "Use of undeclared type 'sqlite3'"

What code generates this error message?

In the C-interface of SQLite3, type sqlite3 is a hidden struct and Swift cannot import it as struct. Just the pointer type sqlite3* is imported as COpaquePointer.

You need to use COpaquePointer in Swift, everywhere you need to use sqlite3* in C.

        let filename: String = "..."
        typealias sqlite3ptr = COpaquePointer
        var db: sqlite3ptr = nil
        let result = sqlite3_open(filename, &db)

Below import statement is added in Bridging header. Using xcode 7.1

#import <sqlite3.h>

Thanks. its working now in application project.

Same steps are not working when adding sqlite in framework project. Is there anything I need to do additional to make it work for framework project?

You can find some threads discussing about importing non-module-d headers (like "sqlite3.h") in framework project.

In short, it's hardly possible. There are no standard ways and no easy ways. Dig it for a few minutes in the dev forums.


ADDITION: Added links to recently updated threds. It seems you know one of them.

Still a pain to use sqlite in a framework?

Swift frameworks and unmapped SDK libraries?

Include of non-modular header inside framework module