我在IOS App中使用FMDatabase
来访问SQLITE
数据库。
我创建了一个名为ModelManager
的类,一切正常。我可以选择,更新......
但我不确定我所做的是否正确所以请帮助我理解这一点。
模型管理器类中的:
let sharedInstance = ModelManager()
class ModelManager: NSObject {
var database: FMDatabase? = nil
class func getInstance() -> ModelManager
{
if(sharedInstance.database == nil)
{
sharedInstance.database = FMDatabase(path: Util.getPath(GlobalVariables.DatabaseName))
sharedInstance.database?.logsErrors()
sharedInstance.database?.crashOnErrors()
}
return sharedInstance
}
func ExecQuery(Query: String, MethodCalling: String) -> Bool
{
sharedInstance.database!.open()
let isExecuted = sharedInstance.database!.executeUpdate(Query, withArgumentsInArray: nil)
//let isExecuted = ModelManager.getInstance().database?.executeUpdate(Query, withArgumentsInArray: nil)
sharedInstance.database!.close()
if(isExecuted == false)
{
//error
}
return isExecuted
}
}
当我想访问数据库时,我会执行以下操作:
用于选择:
sharedInstance.database!.open()
let resultSet: FMResultSet! = ModelManager.getInstance().database?.executeQuery(Query, withArgumentsInArray: nil)
if (resultSet != nil)
{
while resultSet.next()
{
print("here: \(lastLoggedInUser)")
lastLoggedInUser = resultSet.stringForColumn("FTLOGIN")
}
}
sharedInstance.database!.close()
用于执行查询(更新,插入,删除...)
ModelManager.getInstance().ExecQuery(Query, MethodCalling: "LoginService")
现在我的问题是
如果我必须执行选择查询并在获取数据时,我想执行查询,我该怎么做?
sharedInstance.database!.open()
let resultSet: FMResultSet! = ModelManager.getInstance().database?.executeQuery(Query, withArgumentsInArray: nil)
if (resultSet != nil)
{
while resultSet.next()
{
ModelManager.getInstance().ExecQuery(SomethingRetreived, MethodCalling: "LoginService")
}
}
sharedInstance.database!.close()