作者热门文章
- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
我已经为我的应用程序创建了 sqlite 表,但现在我想在表中向数据库添加一个新列。 ALTER TABLE 将帮助我解决这个问题,但首先我想检查数据库版本。我正在使用 PRAGMA user_version 检查用户版本并更新 user_version 但它始终将 user_version 返回为 0。
var database: FMDatabase? = nil
class func getInstance() -> ModelManager{
if(sharedInstance.database == nil){
sharedInstance.database = FMDatabase(path: Util.getPath("XXXX.sqlite"))
}
return sharedInstance
}
func userVersion(){
sharedInstance.database!.open()
var userVer = Int()
let resultSet = sharedInstance.database?.executeQuery("pragma user_version", withArgumentsInArray: nil)
userVer = Int(resultSet!.intForColumn("user_version"))
print("user version : ",userVer)
sharedInstance.database!.close()
}
func updateUserVersion(){
sharedInstance.database!.open()
sharedInstance.database?.executeUpdate("PRAGMA user_version=1", withArgumentsInArray: nil)
sharedInstance.database!.close()
}
最佳答案
下面的代码适用于 Swift 4
import UIKit
import FMDB
class DataConnection: NSObject {
static let databaseVersion = 2
static var isDatabaseUpdated = false
static var database: FMDatabase? = nil
class func databaseSetup() {
if database == nil {
let docsDir = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)[0]
let dpPath = docsDir.appendingPathComponent("database.sqlite")
let file = FileManager.default
if(!file.fileExists(atPath: dpPath.path)) {
copyDatabase(file: file, dpPath: dpPath)
database = FMDatabase(path: dpPath.path)
do {
database!.open()
try database!.executeUpdate("PRAGMA user_version = \(databaseVersion)", values: nil)
database!.close()
isDatabaseUpdated = true
}catch {
print("Error on updating user_version")
}
}else {
database = FMDatabase(path: dpPath.path)
if !isDatabaseUpdated {
var currentVersion = 0
do {
database!.open()
let resultSet: FMResultSet! = try database!.executeQuery("pragma user_version", values: nil)
while resultSet.next() {
currentVersion = Int(resultSet.int(forColumn: "user_version"))
}
database!.close()
}catch {
print("Error on getting user_version")
}
if databaseVersion > currentVersion {
do {
try file.removeItem(at: dpPath)
}catch {
print("Error on getting user_version")
}
copyDatabase(file: file, dpPath: dpPath)
database = FMDatabase(path: dpPath.path)
do {
database!.open()
try database!.executeUpdate("PRAGMA user_version = \(databaseVersion)", values: nil)
database!.close()
isDatabaseUpdated = true
}catch {
print("Error on updating user_version")
}
}else {
isDatabaseUpdated = true
}
}
}
}
}
private class func copyDatabase(file: FileManager, dpPath: URL){
let dpPathApp = Bundle.main.path(forResource: "database", ofType: "sqlite")
print("resPath: "+String(describing: dpPathApp))
do {
try file.copyItem(atPath: dpPathApp!, toPath: dpPath.path)
print("copyItemAtPath success")
} catch {
print("copyItemAtPath fail")
}
}
}
关于ios - 如何在 sqlite 中升级数据库版本并在 swift 表中添加新列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36832983/
我是一名优秀的程序员,十分优秀!