我有一个字典,它与该字典有不同的数据类型值我为数据库创建插入查询现在我的问题是如果我传递double值然后它也进入Int条件如果我写Int条件up和Int条件down。我也尝试使用 isKind(of :) 但同样的事情发生了。
以下是为关键字
的代码
//MARK: INSERT RECORDS
func insertRecord(_ dictRecord : NSDictionary, tableName : String)->Bool
{
//Example:- INSERT INTO subject(sub_color,sub_name,sub_priority)values(125.562000,'Maths','15')
let strStart = "INSERT INTO " + tableName
var column : String = "("
var values : String = "("
var i = 0
for key in dictRecord.allKeys
{
column = (column as String) + (key as! String) + ((dictRecord.allKeys.count-1 == i) ? "" : ",")
// column = "\(column) \(key) \(condition)" as NSString
if let strValue = dictRecord.value(forKey: (key as! String)) as? String
{
values = (values as String) + "\"" + strValue + "\"" + ((dictRecord.allKeys.count-1 == i) ? "" : ",")
// values = "\(values)'\(strValue)' \(condition) " as NSString
}else if let doubleValue = dictRecord.value(forKey: (key as! String)) as? Double
{
values = (values as String) + String (format: "%f",doubleValue) + ((dictRecord.allKeys.count-1 == i) ? "" : ",")
// let strDoubleValue = String(format: "%f", doubleValue)
// values = "\(values) \(strDoubleValue) \(condition) " as NSString
}else if let numValue = dictRecord.value(forKey: (key as! String)) as? Int
{
values = (values as String) + String (format: "%d",numValue) + ((dictRecord.allKeys.count-1 == i) ? "" : ",")
// let strNumValue = String(format: "%d", numValue)
// values = "\(values) \(strNumValue) \(condition) " as NSString
}
i = i + 1
}
// column = (column as String) + ")"
// values = (values as String) + ")"
column = "\(column))"
values = "\(values))"
let strQuery = String(format:"%@%@values%@",strStart,column,values)
return self.executeQuery(strQuery)
}
我的代码是代码
func insertRecord(_ dictRecord : NSDictionary, tableName : String)->Bool
{
//Example:- INSERT INTO subject(sub_color,sub_name,sub_priority)values(125.562000,'Maths','15')
let strStart = "INSERT INTO " + tableName
var column : String = "("
var values : String = "("
var i = 0
for key in dictRecord.allKeys
{
column = (column as String) + (key as! String) + ((dictRecord.allKeys.count-1 == i) ? "" : ",")
// column = "\(column) \(key) \(condition)" as NSString
if let strValue = dictRecord.value(forKey: (key as! String)) as? String
{
values = (values as String) + "\"" + strValue + "\"" + ((dictRecord.allKeys.count-1 == i) ? "" : ",")
// values = "\(values)'\(strValue)' \(condition) " as NSString
}else if dictRecord.value(forKey: (key as! String)) is Int
{
let numValue = dictRecord.value(forKey: (key as! String)) as? Int
values = (values as String) + String (format: "%d",numValue!) + ((dictRecord.allKeys.count-1 == i) ? "" : ",")
// let strNumValue = String(format: "%d", numValue)
// values = "\(values) \(strNumValue) \(condition) " as NSString
}else if dictRecord.value(forKey: (key as! String)) is Double
{
let doubleValue = dictRecord.value(forKey: (key as! String)) as? Double
values = (values as String) + String (format: "%f",doubleValue!) + ((dictRecord.allKeys.count-1 == i) ? "" : ",")
// let strDoubleValue = String(format: "%f", doubleValue)
// values = "\(values) \(strDoubleValue) \(condition) " as NSString
}else if dictRecord.value(forKey: (key as! String)) is Float
{
let floatValue = dictRecord.value(forKey: (key as! String)) as? Float
values = (values as String) + String (format: "%f",floatValue!) + ((dictRecord.allKeys.count-1 == i) ? "" : ",")
// let strDoubleValue = String(format: "%f", doubleValue)
// values = "\(values) \(strDoubleValue) \(condition) " as NSString
}
i = i + 1
}
// column = (column as String) + ")"
column = "\(column))"
values = "\(values))"
let strQuery = String(format:"%@%@values%@",strStart,column,values)
return self.executeQuery(strQuery)
}
我尝试存储的内容
{
"country_Lat" = "112.00";
"country_Long" = 112122.000; // if always store in database like 112122
"country_Name" = London;
"country_Status" = 1;
"country_id" = 2;
createddate = "12:00";
updateddate = "1:00";
}
答案 0 :(得分:0)
我尽可能避免使用Any
类型的词典,但如果您的密钥是String
而且您使用的是常用值类型,那么如果您遵循Eric Aya的建议,您的代码可以大大简化。这是等效的insertRecord
函数。
func insertRecord(_ dictionary: [String: Any], tableName: String) -> Bool {
let columns = dictionary.keys.joined(separator: ",")
let values = dictionary.values.map { "\($0)" }.joined(separator: ",")
return self.executeQuery("INSERT INTO \(tableName)(\(columns))values(\(values))")
}
由于遗留代码限制,可能您被迫使用Objective C类型。但是,无论你在哪里,都要接受Swift类型和编程习惯。