Skip to content Skip to sidebar Skip to footer

Using An Sqlite3 Database With An Ios8 App Using Xcode 6 And Swift

i'm fairly new to programming. I am working with Xcode 6 and swift to build an iOS 8 app. my app has quite lot of data which i have organised into sqlite3 files using the firefox

Solution 1:

you can use sqlite.swift framework to implement database connection with swift here is link https://github.com/stephencelis/SQLite.swift/blob/master/Documentation/Index.md

Here is example to connect sqlite database

var db : Database!
var cat =Expression<String>("ZCATEGORY")
var name =Expression<String>("ZNAME")
var user : Query!
var stmt : Query!
var data : Array<Row>!

@IBOutletvar tabe: UITableView!
overridefuncviewDidLoad() {
    super.viewDidLoad()


    createdb()
}



 internalfunccreatedb()
{
  // bundle database  within your applet path =NSBundle.mainBundle().pathForResource("db", ofType: "sqlite")
    println(path)
    db =Database(path, readonly: true)
    // ZFOOD is table name
    user = db["ZFOOD"]
    println(db)
    // select distinct(cat) from ZFOOD
    stmt = user.select(distinct : cat)
    // Stored query result in array
     data =Array(stmt)

   }

funcnumberOfSectionsInTableView(tableView: UITableView) -> Int {
    return1
}

functableView(tableView: UITableView, numberOfRowsInSectionsection: Int) -> Int {

    return data.count

}
functableView(tableView: UITableView, cellForRowAtIndexPathindexPath: NSIndexPath) -> UITableViewCell {


    let cell: AnyObject= tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath)
     cell.textLabel.text = data[indexPath.row][cat]
    return cell asUITableViewCell
}

I hope this helpful for you

Post a Comment for "Using An Sqlite3 Database With An Ios8 App Using Xcode 6 And Swift"