Re: [pysqlite] Exporting Data to .CSV file
"Nuno Ribeiro" <[email protected]> Fri, 28 Nov 2008 18:45:21 +0000
| Newsgroups | gmane.comp.python.db.pysqlite.user |
|---|---|
| Message-ID | <[email protected]> |
Hi Dennis! >> Did you read the help files for the csv module in Python. No, I actually didn't! I just read pysqlite examples/tutorials and I guess I misunderstood some of them. I realized I could "operate" on tables using SQL commands such as SELECT, UPDATE, etc... what I have missed - and confused - was what Roger said after, about mixing concepts, ie, SQLite C Library and pysqlite module. Thanks a lot for the advice! Nuno On Fri, Nov 28, 2008 at 5:21 AM, Dennis Lee Bieber <[email protected]>wrote: > On Fri, 28 Nov 2008 03:38:34 +0000, "Nuno Ribeiro" > <[email protected]> declaimed the > following in gmane.comp.python.db.pysqlite.user: > > > But, I want also to save Data to a file. > > I tried to import pysqlite and dump a table in CSV format but it didn't > > work! > > > > c.execute ( ''' .output file.csv ''') > > c.execute ( ''' .separator , ''' ) > > > > I don't know if that is possible, i.e., to execute all SQLite commands > using > > pysqlite. > > I didn't want to used Bash commands. Of course I can do it in a simple > way > > with cat command... > > b = os.system('sqlite3 /Path/To/Database/contacts.db "select * from > > contacts" | cat > file.csv') > > > > But, I want to do all work from Python. Is it possible? > > > Did you read the help files for the csv module in Python. > > You basically will have to perform a SELECT on the data, and then > write that data using Python's csv module. > > -=-=-=-=-=-=- > >>> import csv > >>> csvout = open("sample.csv", "wb") > >>> writer = csv.writer(csvout) > >>> from pysqlite2 import dbapi2 as db > >>> con = db.connect("test.db") > >>> cur = con.cursor() > >>> cur.execute("select * from stats") > <pysqlite2._sqlite.Cursor object at 0x0120B6B0> > >>> cur.description > (('id', None, None, None, None, None, None), ('name', None, None, None, > None, None, None), ('num', None, None, None, None, None, None)) > >>> columns = [ f[0] for f in cur.description ] > >>> columns > ['id', 'name', 'num'] > >>> writer.writerow(columns) > >>> for r in cur: > ... writer.writerow(r) > ... > >>> cur.close() > >>> con.close() > >>> csvout.close() > >>> > -=-=-=-=-=-=-=- > id,name,num > 1,integer range,123 > 2,single range,3.14159265 > 3,single range int,31415926500 > 4,double range,5.1234567689 > 5,double range int?,5.1234567689e+020 > -- > Wulfraed Dennis Lee Bieber KD6MOG > [email protected] [email protected] > HTTP://wlfraed.home.netcom.com/ > (Bestiaria Support Staff: [email protected]) > HTTP://www.bestiaria.com/ > > _______________________________________________ > list-pysqlite mailing list > list-pysqlite-FR6EJeJVuqdwc357pe9rcyQmJico6nz3epZhswDD4dQ@public.gmane.org > http://itsystementwicklung.de/cgi-bin/mailman/listinfo/list-pysqlite > _______________________________________________ list-pysqlite mailing list list-pysqlite-FR6EJeJVuqdwc357pe9rcyQmJico6nz3epZhswDD4dQ@public.gmane.org http://itsystementwicklung.de/cgi-bin/mailman/listinfo/list-pysqlite