0

From past 2 days I am searching over the internet for the link between swift and sqlite but i am unable to get the code for it and for the database connection, as i want retrive the data from the citylist.db file into an auto complete textfield in swift.

Please guys if anybody knows the code or suggest anything about the topic they most welcome..!!

One more thing I went through this link https://github.com/stephencelis/SQLite.swift/blob/master/Documentation/Index.md but stucked on the sqlcipher part and now I dont know what should I do ?

stephencelis
  • 4,954
  • 2
  • 29
  • 22
Irshad Qureshi
  • 807
  • 18
  • 41
  • To access a SQLite database in Swift, you should use Core Data. To use a predefined Database, you can use NSPersistentStoreCoordinator to indicate the database's URL. Please note that includding a pre-loaded database can cause your app to be rejected, if it is too big. Try to create a Master / Detail iOS app, and take a look into the AppDelegate class. You can copy the database to the Sandbox Document URL. – cleuton Apr 16 '15 at 17:58
  • Thanks Brother for the reply but I am still unable to do that as I searched over the keyword and found this http://www.cimgf.com/2014/06/08/the-core-data-stack-in-swift/ but again my query is I have the citylist.db file already , So where do i place my file inside the project folder so that it could read it from there. – Irshad Qureshi Apr 18 '15 at 05:25
  • Would love to help you get SQLite.swift up and running, but you'll need to go into more detail as to what you've already done (step-by-step would be great) and what issues you're encountering: error messages, etc. – stephencelis May 13 '15 at 16:18

1 Answers1

0

here is sample code to display data using sqlite in table view

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

//@IBOutlet var tabe: UITableView!
override func viewDidLoad() {
    super.viewDidLoad()
    createdb()
}



internal func createdb()
{
    let path = NSBundle.mainBundle().pathForResource("db", ofType: "sqlite")
    println(path)
    db = Database(path, readonly: true)
    user = db["ZFOOD"]
    println(db)
    stmt = user.select(distinct : cat)

}


func numberOfSectionsInTableView(tableView: UITableView) -> Int {
    return 1
}

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    var data = Array(stmt)
    return data.count

}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    var data = Array(stmt)
    let cell: AnyObject = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath)
    cell.textLabel.text = data[indexPath.row][cat]
    return cell as UITableViewCell
}
Memon Irshad
  • 972
  • 1
  • 8
  • 17