python-noob - which container is appropriate for later exporting into mySql + matplotlib ?

Cousin Stanley cousinstanley at gmail.com
Thu Apr 11 13:58:26 EDT 2013


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 
    ) ;


# -----------------------------------------------------------

  The csv data file can then be inserted into the xdata table
  in the  some.sql3  database via python .... 

import sqlite3 as DBM

fs = open( 'some.csv' ) 

ls = [ ]

dbc = DBN.connect( 'some.sql3' )

cur = dbc.cursor()

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 )

fs.close()
    
dbc.commit()

dbc.close()


# ----------------------------------------------------------------

# python data selection example
# for column 4 between 8 and 9


import sqlite3 as DBM

fs = open( 'some.csv' ) 

ls = [ ]

dbc = DBM.connect( 'some.sql3' )

dbc.row_factory = DBM.Row

cur = dbc.cursor()

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 : 

    print row[ 'xtime' ] , row[ 'col4' ]

fs.close()

dbc.close()


# ----------------------------------------------------

  You can be creative with the data selections
  and pass them off to be plotted as needed ....

  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 ....


-- 
Stanley C. Kitching
Human Being
Phoenix, Arizona




More information about the Python-list mailing list