2

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

例子:

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

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

4

2 回答 2

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/stephenencelis/SQLite.swift/blob/master/Documentation/Index.md http://masteringswift.blogspot.ch/2015/09/create-data-access- layer-with.html

这些应该可以帮助您继续前进。

于 2016-02-23T04:02:19.710 回答
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() 创建一个子字符串,它总是给出相同的表达式

于 2016-07-05T21:10:27.090 回答