Re: Reading column names.

[email protected]
Newsgroups gmane.comp.python.db.pysqlite.user
Message-ID <OF0E054FAF.1D756838-ON85257274.004502CC-85257274.004542B3@bankofny.com>
Conlin,

You can also use cursor.description, this returns a list of tuples of
which the first field contains the column name. It has been a while
since I have done this, but has worked for me.

Here is quick and dirty example: (Stolen from here:
http://initd.org/pub/software/pysqlite/doc/usage-guide.html)

from pysqlite2 import dbapi2 as sqlite

FIELD_MAX_WIDTH = 20
TABLE_NAME = 'people'
SELECT = 'select * from %s order by age, name_last' % TABLE_NAME

con = sqlite.connect("mydb")

cur = con.cursor()
cur.execute(SELECT)

# Print a header.
for fieldDesc in cur.description:    print fieldDesc[0].ljust(FIELD_MAX_WIDTH) ,

print # Finish the header with a newline.
print '-' * 78

# For each row, print the value of each field left-justified within
# the maximum possible width of that field.
fieldIndices = range(len(cur.description))
for row in cur:    for fieldIndex in fieldIndices:        fieldValue = str(row[fieldIndex])        print fieldValue.ljust(FIELD_MAX_WIDTH) ,

    print # Finish the row with a newline.


Sample output:


name_last            age
------------------------------------------------------------------------------

Putin                51
Yeltsin              72



Example 4


Let's insert more people into the people table:


from pysqlite2 import dbapi2 as sqlite

con = sqlite.connect("mydb")

cur = con.cursor()

newPeople = (    ('Lebed'       , 53),    ('Zhirinovsky' , 57),  )

for person in newPeople:    cur.execute("insert into people (name_last, age) values (?, ?)", person)


# The changes will not be saved unless the transaction is committed explicitly:

con.commit()



Thanks,

Greg Givler
Information Technology
Lockwood®
10 Valley Stream Parkway
Malvern, PA 19355
Phone: (484) 605-4826
Email: [email protected]


                                                                                                                                           
                      "Colin Barnette"                                                                                                     
                      <colin.barnette@gmail        To:       [email protected]                                                      
                      .com>                        cc:                                                                                     
                      Sent by:                     Subject:  [pysqlite] Reading column names.                                              
                      pysqlite-bounces@list                                                                                                
                      s.initd.org                                                                                                          
                                                                                                                                           
                                                                                                                                           
                      01/30/2007 11:51 PM                                                                                                  
                      Please respond to                                                                                                    
                      "For users of the                                                                                                    
                      pysqlite database                                                                                                    
                      module."                                                                                                             
                                                                                                                                           
                                                                                                                                           




Hi, I'm trying to find out how to read the column names of a given
table.

I spent about an hour googling but to no avail. Any help is appreciated!

Thanks,

Colin Barnette
_______________________________________________
pysqlite mailing list
pysqlite-IAPFreCvJWPBWskQ1e/[email protected]
http://lists.initd.org/mailman/listinfo/pysqlite

______________________________________________________________________
This email has been scanned by the MessageLabs Email Security System.
For more information please call the help desk at ext 4850
______________________________________________________________________




______________________________________________________________________
This email has been scanned by the MessageLabs Email Security System.
For more information please visit http://www.messagelabs.com/email 
______________________________________________________________________
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.