python-noob - which container is appropriate for later exporting into mySql + matplotlib ?
someone
newsboost at gmail.com
Fri Apr 12 10:03:49 EDT 2013
On 2013-04-11 19:58, Cousin Stanley wrote:
> someone wrote:
>
>> ....
>> I want to put this table into an appropriate container
>> such that afterwards I want to:
>>
>> 1) Put the data into a mySql-table
>> ....
>
> You might consider using sqlite3 as a database manager
> since it is "batteries included" with python ....
>
> The stand-alone sqlite interpreter can first be used
> to create an empty database named some.sql3
> and create a table named xdata in that data base ....
>
> sqlite3 some.sql3 '.read xdata_create.sql'
>
> where the file xdata_create.sql contains ....
>
> create table xdata
> (
> xdate integer ,
> xtime integer ,
> col1 real ,
> col2 integer ,
> col3 integer ,
> col4 real ,
> col5 integer ,
> col6 integer
> ) ;
Oh, thank you very much! Now I understand this (I haven't really worked
much with sql before, so this was/is new to me, thanks!).
> The csv data file can then be inserted into the xdata table
> in the some.sql3 database via python ....
........ and .......
> # python data selection example
> # for column 4 between 8 and 9
I combined both code snippets into:
==============================
#!/usr/bin/python
import sqlite3 as DBM
import ipdb
fs = open( 'some.csv' )
ls = [ ]
dbc = DBM.connect( 'some.sql3' )
cur = dbc.cursor()
if 0:
sql = 'insert into xdata values( ? , ? , ? , ? , ? , ? , ? , ? )'
for row in fs :
dt, col1, col2, col3, col4,col5, col6 = row.strip().split(',' )
xdate , xtime = dt.split( 'T' )
xtuple = ( xdate, xtime, col1, col2, col3, col4, col5, col6 )
cur.execute( sql , xtuple )
dbc.commit()
else:
list_sql = [
'select xtime , col4' ,
'from xdata' ,
'where xtime >= 80000 and xtime <= 90000 ; ' ]
str_sql = '\n'.join( list_sql )
cur.execute( str_sql )
for row in cur :
#ipdb.set_trace()
# I get: TypeError: "tuple indices must be integers, not str"
# "ipdb> row" says: "(80000, 46120.0)"
#print row[ 'xtime' ] , row[ 'col4' ]
print row[0] , row[1]
fs.close()
dbc.close()
==============================
I don't fully understand it yet, but it's nice to see that it works!
Thank you very much for that! Now I'll have to concentrate on figuring
out how/why it works :-)
> You can be creative with the data selections
> and pass them off to be plotted as needed ....
Yes, I understand. Thank you very much. As you can see, on my system I
had to use:
print row[0] , row[1]
instead of:
print row[ 'xtime' ] , row[ 'col4' ]
I'm not sure exactly why - is it because you have another version of
sqlite3 ? This is a bit strange, but anyway I can live with that - at
least for now...
> If mysql is used instead of sqlite3
> you should only have to monkey with
> the data type declarations in xdata_create.sql
> and the dbc.connect strings in the python code ....
Actually I didn't knew anything about this sqlite3 before now. This is
the first time I try it out, so I don't really know what's the
difference between sqlite3 and mysql...
But thank you very much for providing some code I can now study and
learn from !!! Much appreciated....
More information about the Python-list
mailing list