本文介绍了Python将数据库记录放在Namedtuple中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在python(2.7)中编写一些代码来执行此操作:

I'm trying to write some code in python (2.7) doing this:


  1. 在sqlite中打开数据库

  2. 查询数据库,获得一些结果。数据库有多个表,我需要来自不同表的记录:
    数据库是这样的:

    • data.db ---> [table1 [col1] ,col2,col3],table2 [col1,col2,col3]]

从现在开始,我已经实现了第1,2,3和4部分,但我无法弄清楚如何将结果存储在一个namedtuple中。假设在每次迭代时我都将我在namedtuple中需要的数据存储在临时变量中:

Since now i have achieved part 1, 2, 3 and 4, but i can't figure out how to store the results in a namedtuple. Assume that on every iteration i store the data that i need in the namedtuple in temporary variables:

结果为:

var1 = table1.col1

var2 = table2.col1

var3 = table3.col1

(现在我想做点什么变量,但那不是问题,并将3个变量存储在一个namedtuple中)

(now i want to do something with the variable, but thats' not the problem, and store the 3 variables in a namedtuple)

contacts = namedtuple('contacts', 'Z_PK ZFULLNAME ZPHONE ZTEXT ZDATE')

    if has_contacts_sqlite:

        # reading contacts from database
        # 1st step: ZWAPHONE table
        query = "SELECT * FROM ZWAPHONE"
        self.tempcur.execute(query)
        tempcontacts = self.tempcur.fetchall()

        for contact in tempcontacts:
            id = contact.Z_PK
            contact_key = contact.ZCONTACT
            favorite_key = contact.ZFAVORITE
            status_key = contact.ZSTATUS
            phonenum = contact.ZPHONE

            # 2nd step: name from ZWACONTACT table
            query = "SELECT * FROM ZWACONTACT WHERE Z_PK=?;"
            self.tempcur.execute(query, [contact_key])
            contact_entry = self.tempcur.fetchone()
            if contact_entry == None:
                name = "N/A"
            else:
                name = contact_entry.ZFULLNAME

            # 3rd step: status from ZWASTATUS table
            query = "SELECT * FROM ZWASTATUS WHERE Z_PK=?;"
            self.tempcur.execute(query, [status_key])
            status_entry = self.tempcur.fetchone()
            if status_entry == None:
                text = "N/A"
                date = "N/A"
            else:
                text = status_entry.ZTEXT
                date = self.formatDate(status_entry.ZDATE)

            #print ("%s" %(id))
            #print ("%s" %(name.encode(sys.stdout.encoding, errors='replace')))
            #print ("%s" %(phonenum.encode(sys.stdout.encoding, errors='replace')))
            #print ("%s" %(text.encode(sys.stdout.encoding, errors='replace')))
            #print ("%s" %(date))

            contact = contacts(id, name, phonenum, text, date)


print contacts
for line in contacts:
    print line


推荐答案

namedtuple()只不过是一个用工厂功能:

A namedtuple() is nothing but a class generated with a factory function:

SomeRowResult = namedtuple('SomeRowResult', 'var1 var2 var3')

这里 SomeRowResult 是一个类对象(元组的子类),并调用它将创建该类的实例:

Here SomeRowResult is a class object (a subclass of tuple), and calling it will create instances of the class:

for result in results:
    result = SomeRowResult(table1.col1, table2.col1, table3.col1)

如果你想要要获得这些结果的列表,您需要显式构建该列表:

If you wanted to have a list of these results, you need to explicitly build that list:

all_results = []
for result in results:
    result = SomeRowResult(table1.col1, table2.col1, table3.col1)
    all_results.append(result)

这篇关于Python将数据库记录放在Namedtuple中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-17 03:29