2016-08-24 77 views
1

獲取數據在我的應用程序,我希望獲取從FMDB數據庫和數據我想發送觀看數據。但問題是,當數據被提取時,它不會顯示控制檯中的實際數據,如ticket_type = Movie,time = 11:42 pm。相反,它只顯示內存地址結果。從FMDB數據庫

我TicketData源代碼如下

class TicketData: NSObject { 
var field1: String? 
var field2: String? 
var field3: String? 
var field4: String? 

override init() { 
    super.init() 
} 

convenience init(field1: String, field2: String, field3: String, field4: String) { 
    self.init() 

     self.field1 = field1 
     self.field2 = field2 
     self.field3 = field3 
     self.field4 = field4 

} 

}

我的應用程序的截圖是 截圖我的應用程序 image

我的源代碼如下

class TicketDetailViewController: UIViewController, WCSessionDelegate { 

var databasePath = NSString() 
var holding_Ticket_category: String = "" 
var holding_Image: UIImage? 
var hold_ticketName: String = "" 
var hold_ticketDate: String = "" 
var hold_ticketTime: String = "" 
var session: WCSession! 
var ticketDataArray:[TicketData] = [] 


@IBOutlet weak var ticket_grey: UIImageView! 
@IBOutlet weak var cropped_frame: UIImageView! 
@IBOutlet weak var display_image: UIImageView! 
@IBOutlet weak var ticket_type_name: UILabel! 
@IBOutlet weak var ticket_date: UILabel! 
@IBOutlet weak var ticket_time: UILabel! 
@IBOutlet weak var ticket_category: UILabel! 


override func viewDidLoad() 
{ 
    super.viewDidLoad() 

    if WCSession.isSupported(){ 
     self.session = WCSession.defaultSession() 
     self.session.delegate = self 
     self.session.activateSession() 
    } 

    display_image.image = holding_Image 
    ticket_type_name.text = hold_ticketName 
    ticket_date.text = hold_ticketDate 
    ticket_time.text = hold_ticketTime 
    ticket_category.text = holding_Ticket_category 

    let filemgr = NSFileManager.defaultManager() 
    let dirPaths = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true) 
    let docsDir = dirPaths[0] 
    var ticketDB: FMDatabase 

    databasePath = (docsDir as NSString).stringByAppendingPathComponent("ticket_Pass.sqlite") 


    if !filemgr.fileExistsAtPath(databasePath as String) 
    { 

     ticketDB = FMDatabase(path: databasePath as String) 
     if ticketDB.open() 
     { 

      let sql_stmt = "CREATE TABLE IF NOT EXISTS TICKET (ID INTEGER PRIMARY KEY AUTOINCREMENT, IMAGE TEXT, TICKET_CATEGORY TEXT, TICKET_TYPE TEXT, DATE TEXT, TIME TEXT)" 
      if !ticketDB.executeStatements(sql_stmt) 
      { 
       print("Error: \(ticketDB.lastErrorMessage())") 
      } 
      ticketDB.close() 
     } else 
     { 

      print("Error: \(ticketDB.lastErrorMessage())") 
     } 
    } 
} 

個@IBAction FUNC addTickets(發件人:UIButton的) {

let ticketDB = FMDatabase(path: databasePath as String) 
    if ticketDB.open() 
    { 

      let insertSQL = "INSERT INTO TICKET (image, ticket_category, ticket_type, date, time) VALUES ('\(display_image.image!)', '\(ticket_category.text!)', '\(ticket_type_name.text!)', '\(ticket_date.text!)', '\(ticket_time.text!)')" 

      let result = ticketDB.executeUpdate(insertSQL,withArgumentsInArray: nil) 

      if !result 
      { 

       print("Error: \(ticketDB.lastErrorMessage())") 

      } else 
      { 

       let alt = PMAlertController(title: "Success!", description: "Your data is saved to Database!", image: UIImage(named: ""), style: .Alert) 

       alt.addAction(PMAlertAction(title: "OK!", style: .Default, action: { (ACTION) -> Void in 

         self.navigationController?.popToRootViewControllerAnimated(true) 

       })) 

         self.presentViewController(alt, animated: true, completion: nil) 
         print(databasePath) 


      } 
    } 
} 


@IBAction func deleteTickets(sender: UIButton) { 
    let alt = PMAlertController(title: "Delete Ticket Details!", description: "Are you sure?", image: UIImage(named: ""), style: .Alert) 

    alt.addAction(PMAlertAction(title: "Cancel", style: PMAlertActionStyle.Default, action: { (ACTION) -> Void in 

     self.dismissViewControllerAnimated(true, completion: nil) 

    })) 
    alt.addAction(PMAlertAction(title: "OK", style: PMAlertActionStyle.Default, action: { (ACTION) -> Void in 

     self.navigationController?.popToRootViewControllerAnimated(true) 
    })) 
    self.presentViewController(alt, animated: true, completion: nil) 
} 


@IBAction func sendToWatch(sender: AnyObject) { 


    let ticketDB = FMDatabase(path: databasePath as String) 
    if ticketDB.open() 
    { 

     let insertSQL = "INSERT INTO TICKET (image, ticket_category, ticket_type, date, time) VALUES ('\(display_image.image!)', '\(ticket_category.text!)', '\(ticket_type_name.text!)', '\(ticket_date.text!)', '\(ticket_time.text!)')" 

     let result = ticketDB.executeUpdate(insertSQL,withArgumentsInArray: nil) 

     if !result 
     { 

      print("Error: \(ticketDB.lastErrorMessage())") 

     } else 
     { 

      let alt = PMAlertController(title: "Success!", description: "Your data is saved to Database!", image: UIImage(named: ""), style: .Alert) 

      alt.addAction(PMAlertAction(title: "OK!", style: .Default, action: 
       { (ACTION) -> Void in 

        let ticketDB = FMDatabase(path: self.databasePath as String) 
        if ticketDB.open() 
        { 

         let querySQL = "SELECT * FROM TICKET" 

         let result: FMResultSet? = ticketDB.executeQuery(querySQL, withArgumentsInArray: nil) 

         if let result = result 
         { 

          var ticketData = TicketData() 
          while (result.next() == true) 
          { 
           let image = result.stringForColumn("image") 
           let ticket_category = result.stringForColumn("ticket_category") 
           let ticket_type = result.stringForColumn("ticket_type") 
           let date = result.stringForColumn("date") 
           let time = result.stringForColumn("time") 
           ticketData = TicketData(field1: ticket_category!, field2: ticket_type!, field3: date!, field4: time!, field5: image!) 
           self.ticketDataArray.append(ticketData) 


          } 
         print("Data: \(result.resultDictionary())") 


          } 


         }else 
         { 
          print("Error: \(ticketDB.lastErrorMessage())") 

         } 

        ticketDB.close() 


        } 

      })) 

      self.presentViewController(alt, animated: true, completion: nil) 
      print(databasePath) 

     } 
    } 
+0

使用result.resultDictionary並通過鍵從該字典中提取值。我認爲這將有助於 –

+0

我在控制檯中得到的結果如下: 2016-08-24 10:23:07.294 WearWatch [807:32340] CUICatalog:提供的資產名稱無效: /Users/apple/Library/Developer/CoreSimulator/Devices/CE972FAB-70AD-47EF-9805-A2B94EDFD07D/data/Containers/Data/Application/7F7397DA-CBDC-4973-A8F5-E1EE3712E45F/Documents/ticket_Pass.sqlite Data: Data: 數據: 數據: 數據:

+0

我如何做到這一點,因爲我是新來的iOS開發。你可以寫源 –

回答

1

的ticketData是一個對象,你想打印使用對象,

print("Data: \(ticketData)") 

每當我們嘗試打印對象,總是會打印對象的內存地址,而不是對象的屬性。如果要打印對象屬性,請逐個打印,如

print("Data: \(ticketData.ticket_type)") 

等等。如果你想一次打印所有東西,你可以做的是你可以從resultSet中獲得一個字典對象並打印出來。

print("Data:\(resultSet.resultDictionary())") 

編輯:如何獲取結果集的最後一個值。

if let result = result { 

var ticketData = TicketData() 
var latestTicketDict = [String: AnyObject]() 

while (result.next() == true) { 
    let image = result.stringForColumn("image") 
    let ticket_category = result.stringForColumn("ticket_category") 
    let ticket_type = result.stringForColumn("ticket_type") 
    let date = result.stringForColumn("date") 
    let time = result.stringForColumn("time") 
    ticketData = TicketData(field1: ticket_category!, field2: ticket_type!, field3: date!, field4: time!, field5: image!) 
    self.ticketDataArray.append(ticketData) 
    latestTicketDict = result.resultDictionary() 
} 

print("Data: \(latestTicketDict)") 

} 

更新:另外我建議你去通過他們的GitHub頁面上的FMDB文檔和探討FMDB一些教程編寫更有效的DB操作。也不要關閉數據庫並打開數據庫爲每一個數據庫操作的FMDB的創造者說When to close SQLite database (using FMDB) -

"Keep it open unless you change your schema. That's the only reason to close it, and constantly re-opening it is a little hit on performance /battery life."

此外,我建議你看一看FMDatabaseQueue。

希望這有助於。

+0

感謝它幫助,但它打印所有條目。如果我只想要最新的條目,請打印 –

+0

你的意思是最後一本詞典是最新的詞典嗎? – iamyogish

+0

如果要打印最新或最後一行,則需要在while(result.next()== true)之前聲明ticketData變量,並且需要將該值分配給while循環內的ticketData。然後,在while循環之外,您需要打印ticketData所需的任何屬性。 – iamyogish