New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to use existing database #4

Closed
conxsys opened this Issue Sep 21, 2014 · 2 comments

Comments

Projects
None yet
2 participants
@conxsys

conxsys commented Sep 21, 2014

I cannot get SwiftData to use an already existing database that contains data. I have renamed it to SwiftData.sqlite and placed it in the Supporting Files folder in my project (I have tried a few other locations as well). When I try to query the Players table:

let (resultSet, err) = SD.executeQuery("SELECT * FROM PLAYERS")

I get the error:

SwiftData Error -> During: SQL Prepare
-> Code: 1 - SQL error or missing database
-> Details: no such table: PLAYERS

@conxsys

This comment has been minimized.

Show comment
Hide comment
@conxsys

conxsys Sep 22, 2014

Nevermind, I figured out the problem. This should be handled much more elegantly, but for now I just brute forced it by modifying the createPath() func to this:

        let docsPath = NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.DocumentDirectory, NSSearchPathDomainMask.UserDomainMask, true)[0] as String
        let databaseStr = "SwiftData.sqlite"
        let dbPath = docsPath.stringByAppendingPathComponent(databaseStr)

        // BEGING MODIFICATION
        let fileMan = NSFileManager.defaultManager()
        if !(fileMan.fileExistsAtPath(dbPath)) {    // The database does not already exist in Documents directory
            if let source = NSBundle.mainBundle().resourcePath?.stringByAppendingPathComponent(databaseStr) {
                if !(fileMan.fileExistsAtPath(source)) {
                    println("SQLiteDB - file \(databaseStr) not found in bundle")
                } else {
                    var error:NSError?

                    if !(fileMan.copyItemAtPath(source, toPath: dbPath, error: &error)) {
                        println("SQLiteDB - failed to copy writable version of DB!")
                        println("Error - \(error!.localizedDescription)")
                    }
                }
            }
        }
        // END MODIFICATION

        return dbPath

conxsys commented Sep 22, 2014

Nevermind, I figured out the problem. This should be handled much more elegantly, but for now I just brute forced it by modifying the createPath() func to this:

        let docsPath = NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.DocumentDirectory, NSSearchPathDomainMask.UserDomainMask, true)[0] as String
        let databaseStr = "SwiftData.sqlite"
        let dbPath = docsPath.stringByAppendingPathComponent(databaseStr)

        // BEGING MODIFICATION
        let fileMan = NSFileManager.defaultManager()
        if !(fileMan.fileExistsAtPath(dbPath)) {    // The database does not already exist in Documents directory
            if let source = NSBundle.mainBundle().resourcePath?.stringByAppendingPathComponent(databaseStr) {
                if !(fileMan.fileExistsAtPath(source)) {
                    println("SQLiteDB - file \(databaseStr) not found in bundle")
                } else {
                    var error:NSError?

                    if !(fileMan.copyItemAtPath(source, toPath: dbPath, error: &error)) {
                        println("SQLiteDB - failed to copy writable version of DB!")
                        println("Error - \(error!.localizedDescription)")
                    }
                }
            }
        }
        // END MODIFICATION

        return dbPath

@conxsys conxsys closed this Sep 22, 2014

@Lopdo

This comment has been minimized.

Show comment
Hide comment
@Lopdo

Lopdo Jan 27, 2015

I think this should be official part of the (otherwise awesome) project, along with option to specify name of file.

Lopdo commented Jan 27, 2015

I think this should be official part of the (otherwise awesome) project, along with option to specify name of file.

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