Как получить таблицу и массив в использовании базы данных FMDB
struct Inspectionopationdata
{
let inspectionStatus: String
let vehInspDetails: Array<Any>
let startDate: String
let signedOff: Bool
let inspectionDescription: String
let performedBy: String
let performedByEmployeeNumber: String
let registration: String
let inspectionDate: String
let vehicleUniqueId: Int
let noteHistory: Array<Any>
let inspectionId: Int
let faultsFound: String
let type: String
let damageFound: String
let templateId : Int
}
var shareInstancethree = InspectionopationDatabaseManager()
class InspectionopationDatabaseManager: NSObject{
var pathToDatabase: String!
var databasethree:FMDatabase? = nil
class func getInstance() -> InspectionopationDatabaseManager{
if shareInstancethree.databasethree == nil{
shareInstancethree.databasethree = FMDatabase(path: Util3.getPaththree("Userselect.db"))
}
return shareInstancethree
}
func saveDatadaily(_ modelInfo:Inspectionopationdata) -> Bool{
shareInstancethree.databasethree?.open()
let isSave = shareInstancethree.databasethree?.executeUpdate("INSERT INTO Userselect (startDate,registration,vehInspDetails,noteHistory,faultsFound,vehicleUniqueId,performedBy,signedOff,damageFound,inspectionDate,performedByEmployeeNumber,inspectionDescription,inspectionStatus,type,inspectionId,templateId) VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)", withArgumentsIn: [modelInfo.startDate,modelInfo.registration,modelInfo.vehInspDetails,modelInfo.noteHistory,modelInfo.faultsFound,modelInfo.vehicleUniqueId,modelInfo.performedBy,modelInfo.signedOff,modelInfo.damageFound,modelInfo.inspectionDate,modelInfo.performedByEmployeeNumber,modelInfo.inspectionDescription,modelInfo.inspectionStatus,modelInfo.type,modelInfo.inspectionId,modelInfo.templateId])
// shareInstancethree.databasethree?.close()
return isSave!
}
func openDatabase() -> Bool {
if databasethree == nil {
if FileManager.default.fileExists(atPath: pathToDatabase) {
databasethree = FMDatabase(path: pathToDatabase)
}
}
if databasethree != nil {
if (shareInstancethree.databasethree?.open())! {
return true
}
}
return false
}
func getdatadailyvehicle() -> [Inspectionopationdata]! {
var list: [Inspectionopationdata]!
if openDatabase() {
let query = "select * from Userselect"
do {
let results = try databasethree!.executeQuery(query, values: nil)
while results.next() {
let vehicle = Inspectionopationdata(inspectionStatus: results.string(forColumn:"inspectionStatus")!, vehInspDetails: results.mutableArrayValue(forKey: "vehInspDetails") as! Array<Any>,startDate: results.string(forColumn:"startDate")!, signedOff: results.bool(forColumn: "signedOff"), inspectionDescription: results.string(forColumn:"inspectionDescription")!, performedBy: results.string(forColumn:"performedBy")!, performedByEmployeeNumber: results.string(forColumn:"performedByEmployeeNumber")!, registration: results.string(forColumn:"registration")!, inspectionDate: results.string(forColumn:"inspectionDate")!, vehicleUniqueId: Int(results.int(forColumn: "vehicleUniqueId")), noteHistory: results.mutableArrayValue(forKey: "noteHistory") as! Array<Any>, inspectionId: Int(results.int(forColumn:"inspectionId")), faultsFound: results.string(forColumn:"faultsFound")!, type: results.string(forColumn:"type")!, damageFound: results.string(forColumn:"damageFound")!, templateId: Int(results.int(forColumn:"templateId"))
)
if list == nil {
list = [Inspectionopationdata]()
}
list.append(vehicle)
}
}
catch {
print(error.localizedDescription)
}
// databasethree!.close()
}
return list
}
}
//save table
let modelinfo = Inspectionopationdata(
inspectionStatus: responseDic["inspectionStatus"] as! String,
vehInspDetails: responseDic["vehInspDetails"] as! Array,
startDate: responseDic["startDate"] as! String,
signedOff: responseDic["signedOff"] as! Bool,
inspectionDescription: responseDic["inspectionDescription"] as! String,
performedBy: responseDic["performedBy"] as! String,
performedByEmployeeNumber: responseDic["performedByEmployeeNumber"] as! String,
registration: responseDic["registration"] as! String,
inspectionDate: responseDic["inspectionDate"] as! String,
vehicleUniqueId: responseDic["vehicleUniqueId"] as! Int,
noteHistory: responseDic["noteHistory"] as! Array,
inspectionId: responseDic["inspectionId"] as! Int,
faultsFound: responseDic["faultsFound"] as! String,
type: responseDic["type"] as! String,
damageFound: responseDic["damageFound"] as! String,
templateId: self.templateId)
let isSave1 = InspectionopationDatabaseManager.getInstance().saveDatadaily(modelinfo)
print("isDailyVehicleinspectionSave::->\(isSave1)")
Опциональное значение