我的iOS
应用程序上有一个sqlite表,我想总结一列的所有字符串编号。列名是:“count”
我写了这段代码来获取数字:
let kiwi = DB.getInstance()
let records = kiwi.executeQuery("SELECT * FROM zekrlist")
for record in records {
let structContact = StructContact()
structContact.id = record.column["id"]?.asInt()
structContact.count = record.column["count"]?.asString()
print(structContact.count)
打印结果如下:
4
2
我希望将这些数字加在一起:
6
任何帮助将不胜感激。
答案 0 :(得分:1)
let kiwi = DB.getInstance()
let count:Int = 0
let records = kiwi.executeQuery("SELECT * FROM zekrlist")
for record in records {
let structContact = StructContact()
structContact.id = record.column["id"]?.asInt()
structContact.count = record.column["count"]?.asString()
count = count + structContact.count.asInt() // Int(structContact.count)
}
print(count)
答案 1 :(得分:1)
试试这个: -
let records = kiwi.executeQuery("SELECT SUM(columnName) AS resultName FROM tableName")
var sum = 0
for record in records
{
sum += record.column["resultName"]?.asInt() ?? 0
}