2015-04-05 55 views
0

我是相當新的編程。我正在使用Xcode 6並快速構建iOS 8應用程序。我的應用程序有很多數據,我使用Firefox添加到sqlite3文件中。我已將這些文件放入我的項目文件夾中。他們的擴展名是.sql,現在我需要在我的應用程序中訪問這些數據。我已經通過在構建階段選項卡中添加libsqlite3.dylib鏈接了sqlite3庫。現在我想編寫訪問數據庫的代碼。我已經在網上找到了很多關於iOS 7和更早版本的代碼和例子的使用Objective-C的信息,但是沒有與iOS 8和Swift一起使用。如果有人能幫助我或帶領我走向正確的方向,我將不勝感激。謝謝。使用sqlite3數據庫與ios8應用程序使用Xcode 6和swift

回答

1

可以使用sqlite.swift框架實現與SWIFT 這裏的數據庫連接是鏈接https://github.com/stephencelis/SQLite.swift/blob/master/Documentation/Index.md

這裏是例如連接SQLite數據庫

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

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


    createdb() 
} 



internal func createdb() 
{ 
    // bundle database within your app 
    let 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) 

    } 

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

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 

    return data.count 

} 
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 


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

我希望這有助於您

+0

喜。感謝您的回覆。我真的只是在不使用任何包裝的情況下自己調用sql庫。我看過其他職位,但他們似乎都創建了表格和輸入值。我已經擁有了一個我不想修改的數據庫。我只想訪問表中的值。例如,表格有兩列。我只想把第一列作爲關鍵字,第二列作爲值。我一直在這個很長一段時間沒有運氣... – alionthego 2015-04-06 10:26:42

+0

如果你使用sqlite.swift包裝,然後我會幫你,因爲我從來沒有使用SQL庫 – 2015-04-06 10:31:54

+0

代碼更新檢查它 – 2015-04-06 10:37:51

相關問題