读取表的名称()

时间:2016-02-15 20:48:00

标签: sqlite.swift

创建一个宽度表(名称)后如何阅读表格的名称?

示例:

let tab = Table("myTable")
let name = ??

实际上,这个例子没有意义,但我的用例是变量" tab"是一个类属性,该类不会将表名保存在字符串中。

2 个答案:

答案 0 :(得分:0)

我不完全理解你的例子。基本上当你在一个类中创建表时,你会说我会收集它看起来像这样:

class tab {

func Table(vString:String)
    static let TABLE_NAME = vString     //setting Name of Table in the class    
    static let table = Table(TABLE_NAME)// 

    static var kundeID = Expression<Int64>("kundeID")   //Setting Fields of the Table
    static let lastname = Expression<String>("lastname")
    static let firstname = Expression<String>("firstname")
    //.... until you got all the fields

//Creating the table if it does not yet exist.
static func createTable() throws {
    guard let DB = SQLiteDataStore.sharedInstance.BBDB else {
        throw DataAccessError.Datastore_Connection_Error
    }
    do {
        let _ = try DB.run( table.create(ifNotExists: true) {t in
            t.column(kundeID, primaryKey: true) //primary key
            t.column(lastname) //define same columns as above!
            t.column(firstname)
            // and so on
            })

    } catch _ {
        // Error throw if table already exists
    }

}

因此,您知道表名实际上是什么,因为您通过调用函数表来定义它。我认为你不能以任何其他方式做到这一点。看看那些链接: https://github.com/stephencelis/SQLite.swift/blob/master/Documentation/Index.md http://masteringswift.blogspot.ch/2015/09/create-data-access-layer-with.html

这些应该可以帮助你开始。

答案 1 :(得分:0)

我使用这些扩展程序..

extension Table{
  var getName: String{
    let cadena:NSString = self.asSQL()  //"SELECT * FROM \"NombreDeTabla\""
    return cadena.substringWithRange(NSRange (location: 15, length: cadena.length-16))
  }
}

与列相同:

extension Expression{
  var getName: String{
    let cadena:NSString = self.asSQL() //\"NombreDeColumna\"
    return cadena.substringWithRange(NSRange (location: 1, length: cadena.length-2))
  }
}

它只是从.asSQL()创建一个子串,它总是给出相同的表达式