3.8 KiB
Writing to SQLite3+¶
The requirements to get started are minimal (actually none). The cell below creates a dataframe that will be stored within SQLite 3+
#
# Writing to PostgreSQL database
#
import transport
from transport import providers
import pandas as pd
_data = pd.DataFrame({"name":['James Bond','Steve Rogers','Steve Nyemba'],'age':[55,150,44]})
sqw = transport.get.writer(provider=providers.SQLITE,database='/home/steve/demo.db3',table='friends')
sqw.write(_data,if_exists='replace') #-- default is append
print (transport.__version__)
Reading from SQLite3+¶
The cell below reads the data that has been written by the cell above and computes the average age within a PostreSQL (simple query).
- Basic read of the designated table (friends) created above
- Execute an aggregate SQL against the table
NOTE
By design read object are separated from write objects in order to avoid accidental writes to the database. Read objects are created with transport.get.reader whereas write objects are created with transport.get.writer
import transport
from transport import providers
sqr = transport.get.reader(provider=providers.SQLITE,database='/home/steve/demo.db3',table='friends')
_df = sqr.read()
_query = 'SELECT COUNT(*) _counts, AVG(age) from friends'
_sdf = sqr.read(sql=_query)
print (_df)
print ('--------- STATISTICS ------------')
print (_sdf)
An auth-file is a file that contains database parameters used to access the database. For code in shared environments, we recommend
- Having the auth-file stored on disk
- and the location of the file is set to an environment variable.
To generate a template of the auth-file open the file generator wizard found at visit https://healthcareio.the-phi.com/data-transport
{
"provider":"sqlite",
"database":"/home/steve/demo.db3","table":"friends"
}