使用sqlite.swift在数据库中搜索特定行

时间:2016-04-23 22:27:50

标签: database sqlite.swift

我在数据库中读取了一个特定的行。我使用过滤查询,但我得到的是以下内容:

if __name__ == "__main__":
    import json

    #'output_info.json is http://pastebin.com/ckKAtP5y
    with open('output_info.json') as data_file:
        data = json.load(data_file)

    temp_data = data
    greater = []
    len1 = len(data['courses'])
    len2 = len1

    for i in range(0,len1):
        for j in range(0, len2):
            if i==j:
                continue
            if data['courses'][i]['course_name'][0] == temp_data['courses'][j]['course_name'][0]:
                if data['courses'][i]['course_name'][1] == temp_data['courses'][j]['course_name'][1]:
                    if data['courses'][i]['course_name'][2] == temp_data['courses'][j]['course_name'][2]:
                        if data['courses'][i]['course_mentioned_times']> temp_data['courses'][j]['course_mentioned_times']:
                            greater.append(i)
                        else:
                            greater.append(j)


    final = []
    for i in greater:
      if i not in final:
        final.append(i)

    list_order = []

    for i in range(0,len(data['courses'])):
        list_order.append(i)

    new_final = []
    for i in list_order:
        if i not in final:
            new_final.append(i)

    for i in new_final:
        if i!=new_final[0]:
            i=i-1
        data['courses'].pop(i)

    # Writing the new json data back to data.json file.
    with open('data.json', 'w') as f:
        json.dump(data, f)

这是我的疑问:

Table(clauses: SQLite.QueryClauses(select: (false, [SQLite.Expression<()>(template: "*", bindings: [])]), from: ("users", nil, nil), join: [], filters: Optional(SQLite.Expression<Swift.Optional<Swift.Bool>>(template: "(\"id\" = ?)", bindings: [Optional(6523)])), group: nil, order: [], limit: nil))

我的错误在哪里?

1 个答案:

答案 0 :(得分:6)

好的,我自己整理了这个问题。我用了prepare函数。见下面的代码

let query = users.filter(id == 6523)
for user in try db.prepare(query) { 
   print("id: \(user[name])") 
}