I'm working on a group project for school and I am also trying to learn Swift for the project, so I am very new to Xcode and Swift and though I know SQL I have not worked with SQLite before. I am at the point where I have verified that I am able to open a reuse.db file that was created by my group mate, but I am unable to execute insert or select statements that don't return nil. Alternatively after following a tutorial, I am able to create a database.db file and then execute statements within that create a table, insert data into the table, and then query results, however I need to figure out how to utilize the database created by my group mate. Once I get the results I will have to figure out how to parse and reformat them so they correctly populate the table views, however I am just stuck on figuring out how to get results in the first place. Here is my view controller code. After I open the database I am unable to do an insert, and if I comment out my insert attempt, I am unable to do a select query. Thank you in advance for any help or suggestions!! Also, I believe the syntax for my SQL statements are correct because I have verified they are correct with the group mate that created the reuse.db file
//
// ViewController.swift
// EmmasTest
//
// Created by Emma Paul on 5/14/15.
// Copyright (c) 2015 Emma Paul. All rights reserved.
//
import UIKit
class CategoriesViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
@IBOutlet var tableView: UITableView!
//initialize categories to be a blank string
var categories: [String] = []
//this stores a reference to the database path
var databasePath = NSString()
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
//sets the page title to be "Categories"
title = "Categories"
//the .self gets the class to use the class name
self.tableView.registerClass(UITableViewCell.self, forCellReuseIdentifier: "cell")
let myDatabase = FMDatabase(path: "/Users/emmamaypaul/Downloads/reuse.db")
if myDatabase == nil
{
categories = ["couldn't open database file"]
}
else
{
if myDatabase.open()
{
categories = ["opened file"]
let insertSQL = "INSERT INTO categories (name) VALUES ('Emmas appliance');"
let result = myDatabase.executeUpdate(insertSQL, withArgumentsInArray: nil)
if !result
{
categories = ["couldn't insert"]
}
else
{
categories = ["insert happened"]
} /*
let categoryQuery = "SELECT name FROM categories"
let results:FMResultSet? = myDatabase.executeQuery(categoryQuery, withArgumentsInArray: nil)
if (results?.stringForColumn("name") != nil)
{
// var string_result: String? = results?.stringForColumn("name")
// categories = ["\(string_result)"]
categories = ["query happened"]
}else{
categories = ["query didn't happened"]
} */
myDatabase.close()
}
}
//this sorts the categories
// categories.sort(<)
}
func tableView(tableView: UITableView, numberOfRowsInSection: Int) -> Int
{
//this will be the count of the array
return self.categories.count;
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
{
var cell:UITableViewCell = self.tableView.dequeueReusableCellWithIdentifier("cell") as! UITableViewCell
cell.textLabel?.text = self.categories[indexPath.row]
return cell
}
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath)
{
//I believe this allows you to select a row, just prints a message for now if selected
//actually nothing gets printed
println("You selected #\(indexPath.row) category")
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
Aucun commentaire:
Enregistrer un commentaire