jeudi 4 février 2016

sqlite query in python writes only last row to file using objects

First off... I'm new to Python in the last couple of months and up till now it's been going pretty well. But I'm stumped here and I'm very close to smashing my head through my monitor.

When I run this script I only get the last line of data written to a file while what I need is all lines of data to be written in the format that is in the class section.

VOL_HOURS.db has at multiple rows but only the last one being written. Here is my script:

    from vlist import VLIST

import sqlite3
import decimal
import os
import sys
import uuid

VERSION = "oops"
DEVICE_SOFTWARE_VERSION = "OOPS"
FILENAME = "VHOURS.nmf"
conn = sqlite3.connect("VOL_HOURS.db")






def fromVolunteers():
    conn = sqlite3.connect("VOL_HOURS.db")


with conn:

    cur = conn.cursor()
    cur.execute("SELECT BOB, DAVE, CAROL, ANDY, CARL, DANNY, CHERYL, CYNTHIA, TARA, SCOTT, ASHLEY, CRYSTAL FROM VOLUNTEERS")
    rows = cur.fetchall()

with open(FILENAME,"w+") as output:


for row in rows:
    bob = row[0] 
    dave = row[1]
    carol = row[2]
    andy= row[3]
    carl = row[4]
    danny = row [5]
    cheryl = row [6]
    cynthia = row [7]
    tara = row [8]
    scott = row [9]
    ashley = row [10]
    crystal = row [11]



def main():
    fromVolunteers()
    testOutput = open(FILENAME, "w+")

    vlist = VLIST()
    vlist.setBob(bob)
    vlist.setDave(dave)
    vlist.setCarol(carol)
    vlist.setAndy(andy)
    vlist.setCarl(carl)
    vlist.setDanny(danny)
    vlist.setCheryl(cheryl)
    vlist.setCynthia(cynthia)
    vlist.setTara(tara)
    vlist.setScott(scott)
    vlist.setAshley(ashley)
    vlist.setCrystal(crystal)

    vlist.dump_object(testOutput)

    testOutput.flush()
    testOutput.close()
cur.close()
conn.close()

if __name__ == "__main__":
    sys.exit(main())

The class file that it's pulling from is:

class VLIST:

    def __init__(self):
        self.bob = ""
        self.dave = ""
        self.carol = ""
        self.andy = ""
        self.carl = ""
        self.danny = ""
        self.cheryl = ""
        self.cynthia = ""
        self.tara = ""
        self.scott = ""
        self.ashley = ""
        self.crystal = ""

    def setBob(self,bob):
        self.bob = bob
        return

    def setDave(self,dave):
        self.dave = dave
        return

    def setCarol(self,carol):
        self.carol = carol
        return

    def setAndy(self,andy):
        self.andy = andy
        return

    def setCarl(self,carl):
        self.carl = carl
        return

    def setDanny(self,danny):
        self.danny = danny
        return

    def setCheryl(self,cheryl):
        self.cheryl = cheryl
        return

    def setCynthia(self,cynthia):
        self.cynthia = cynthia
        return

    def setTara(self,tara):
        self.tara = tara
        return

    def setScott(self,scott):
        self.scott = scott
        return

    def setAshley(self,ashley):
        self.ashley = ashley
        return

    def setCrystal(self,crystal):
        self.crystal = crystal
        return

    def dump_object(self,testOutput):
        testOutput.write("VLIST,"+self.bob+","+self.dave+","+self.carol+","+self.andy+","+self.carl+","+self.danny+","+self.cheryl+","+self.cynthia+","+self.tara+","+self.scott+","+self.ashley+","+self.crystal+","+"\n")

I apologize in advance if this is confusing, like I said I'm new to this whole this. Thank you for your time.

Aucun commentaire:

Enregistrer un commentaire