0

I am trying to use someone else's code as a basis for what I want to do. However it appears to have been written for an older version of Swift and so I get lots of errors. I have been able to fix quite a few but now I am stumped.

Code I am correcting is:

import UIKit

class ViewController: UIViewController {

let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
var array_data: NSMutableArray = []

@IBOutlet var table_data : UITableView?
override func viewDidLoad() {
    super.viewDidLoad()
    selectFunc()

    self.navigationItem.title="Empolyee List"
    var addBtn = UIBarButtonItem(title: "Add", style: UIBarButtonItemStyle.Plain, target: self, action: Selector("insertFunc"))
    self.navigationItem.rightBarButtonItem = addBtn


}
override func viewWillAppear(animated: Bool) {
    super.viewWillAppear(animated)
    selectFunc()
}


override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

// I did some work on the block below to get rid of the use of Cstring (Deprecated) that has caused issues below

func selectFunc(){
    var selectQuery="select * from EmployeInfo"
    var result:CInt=0

    var stmt:COpaquePointer = nil
    result = sqlite3_prepare_v2(appDelegate.database, selectQuery, -1, &stmt, nil);
    if result != SQLITE_OK
    {
        println("failed  \(sqlite3_errmsg(appDelegate.database))")
    }
    else
    {
        result = sqlite3_step(stmt)
        array_data.removeAllObjects()
        while result == SQLITE_ROW {
            var Dictionary = NSMutableDictionary()

            let name = sqlite3_column_text(stmt, 0)
            let dep = sqlite3_column_text(stmt, 1)
            let sal = sqlite3_column_text(stmt, 2)

//  This is where the errors start  - next three lines


            Dictionary.setObject(String.fromCString(CString(name)), forKey:"name")
            Dictionary.setObject(String.fromCString(CString(dep)), forKey: "department")
            Dictionary.setObject(String.fromCString(CString(sal)), forKey: "salary")

//  The error (not surprisingly) is "Use of Unresolved identifier 'Cstring'"


//OK  for those who run into this issue...

//use...                    Dictionary.setObject(String.fromCString(UnsafePointer<CChar>(name))!, forKey: "name")
            Dictionary.setObject(String.fromCString(UnsafePointer<CChar>(dep))!, forKey: "department")
            Dictionary.setObject(String.fromCString(UnsafePointer<CChar>(sal))!, forKey: "salary")


            array_data .addObject(Dictionary)
            result = sqlite3_step(stmt)

        }
    }
    self.table_data!.reloadData()
}
func insertFunc(){

    let vc : UIViewController = storyboard!.instantiateViewControllerWithIdentifier("AddViewController") as! UIViewController;
    self.navigationController!.pushViewController(vc, animated: true)
}


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

func tableView(tableView: UITableView!, numberOfRowsInSection section: Int) -> Int {
    return self.array_data.count
}

func tableView(tableView: UITableView!, cellForRowAtIndexPath indexPath: NSIndexPath!) -> UITableViewCell! {
    //simple cell
    var cell = tableView.dequeueReusableCellWithIdentifier("CELL") as? UITableViewCell
    if !(cell != nil) {
        cell = UITableViewCell(style: UITableViewCellStyle.Value1, reuseIdentifier: "CELL")
    }
    var dic=array_data.objectAtIndex(indexPath.row) as! NSDictionary
    cell!.textLabel!.text = dic.valueForKey("name") as! String
    return cell
}
func tableView(tableView: UITableView!, didSelectRowAtIndexPath indexPath: NSIndexPath!){
    let vc = storyboard!.instantiateViewControllerWithIdentifier("update_deleteViewController") as update_deleteViewController;
    vc.dictionary=array_data.objectAtIndex(indexPath.row) as NSDictionary
    self.navigationController.pushViewController(vc, animated: true)
}

}

There are other issues in the project but this will give me a good start.

Eric Aya
  • 69,473
  • 35
  • 181
  • 253
IanMac
  • 1
  • 1
  • 2
    Please reduce your code to the part that is actually causing problems, and add the exact error messages. – Martin R Jul 07 '15 at 08:44
  • Perhaps this is what you are looking for http://stackoverflow.com/a/25169265/1187415 ? – Martin R Jul 07 '15 at 08:46
  • I have entered the details above – IanMac Jul 08 '15 at 09:47
  • Did you have a look at the answer from my previous comment? Did you check if that solves your problem? – Martin R Jul 08 '15 at 09:51
  • Thanks for your comments. I thought it useful to leave the rest of the code so you could get some idea of what I was trying to do. I am trying your suggestion now. – IanMac Jul 08 '15 at 09:51
  • if I use Dictionary.setObject(String.fromCString(UnsafePointer(name)) then the error comes back as adding a comma separator (which I don't think is right) – IanMac Jul 08 '15 at 09:56
  • Thanks to Martin R. You gave me the hint I needed. Now on with the development :-) - Much appreciated! – IanMac Jul 08 '15 at 10:09

1 Answers1

0

Retrieving the data from sqlite database using swift programming language

func retrivingDataFromSqliteDb(){

    var database:COpaquePointer = nil

    // This is for getting document directory path or database path
    let dbpath = appDelegate.getting_SandBox_Path().cStringUsingEncoding(NSUTF8StringEncoding)

    // open the database and checking open or not
    let error = sqlite3_open(dbpath, &database)

    if error != SQLITE_OK {

        println("Error while opening");
    }
    else{
        println("already database open");


        var selectQuery = "select * from emptab"

        var result:CInt = 0
        var stmt:COpaquePointer = nil

        result = sqlite3_prepare_v2(database, selectQuery, -1, &stmt, nil);
        if result != SQLITE_OK {

            println("failed : \(sqlite3_errmsg(database))")
        }
        else  {

            result = sqlite3_step(stmt)
             array_data.removeAllObjects()

            while result == SQLITE_ROW {
            var Dictionary = NSMutableDictionary()

            let fname = sqlite3_column_text(stmt, 0)
            let lname = sqlite3_column_text(stmt, 1)
            let phone = sqlite3_column_text(stmt, 2)
            let email = sqlite3_column_text(stmt, 3)
            let address = sqlite3_column_text(stmt, 4)

       // adding to retrived objects to dictionary
    Dictionary.setObject(String.fromCString(UnsafePointer<CChar>(fname))!, forKey: "firstName")
    Dictionary.setObject(String.fromCString(UnsafePointer<CChar>(lname))!, forKey: "LastName")
    Dictionary.setObject(String.fromCString(UnsafePointer<CChar>(phone))!, forKey: "MobileNum")
    Dictionary.setObject(String.fromCString(UnsafePointer<CChar>(email))!, forKey: "mail")
    Dictionary.setObject(String.fromCString(UnsafePointer<CChar>(address))!, forKey: "address")


                // dictionary of data adding to the Array
                array_data .addObject(Dictionary)

                result = sqlite3_step(stmt)

            }
            println(array_data)

        }

    }

}