0


I am attempting to populate a database with latlon weather data for future consultation. I am doing this via the sqlite3 python module.

At the moment, I am able to create the table (a resulting .db file is visible after program execution). I am also able to consult rows after insertion, so I'm sure my cursor.execute lines are working.

Unfortunately, after program execution, upon consulting the database with a "select * from table" statement, nothing appears (even though the same command worked beforehand, while the program executed). In fact, when dumping the sqlite3 file, I only get the create statement.

What could be wrong? I get no errors or warnings.

The code itself:

#!/usr/bin/python
import sys
import numpy
from netCDF4 import Dataset
import sqlite3

# Config variables.
N = 0
E = -30
S = -30
W = -60

ncfile = sys.argv[1];
ncdata = Dataset(ncfile,'r',format='NETCDF4')

sst = ncdata.variables['sea_surface_temperature']
lat = ncdata.variables['lat']
lon = ncdata.variables['lon']

# Data reshaping.
lat = lat[:]
lon = lon[:]
sst = sst[:]

c_lat = numpy.argwhere((lat <= N) & (lat >= S))
c_lon = numpy.argwhere((lon <= E) & (lon >= W))

conn = sqlite3.connect('sst.db')
c = conn.cursor()

# Create the database table.
c.execute("""CREATE TABLE current(lat real, lon real, sst real)""")

sqlite3.register_adapter(numpy.float64, float)
sqlite3.register_adapter(numpy.float32, float)
for lati in c_lat:
    for loni in c_lon:
        latn = lati[0]
        lonn = loni[0]
        lat_db = lat[latn]
        lon_db = lon[lonn]
        sst_db = sst[0,lonn][latn]

        values = (lat_db,lon_db,sst_db)
        if type(sst_db) != numpy.float64:
            continue

        c.execute("INSERT INTO current VALUES(?,?,?)",values)

# Test if data was inserted. (it is!)
c.execute("SELECT * FROM current")
print c.fetchall()
2
  • 1
    possible duplicate of SQLite python does not update table Commented Apr 14, 2014 at 17:07
  • Perfect! Apperantly I did not search the correct terms. Commented Apr 14, 2014 at 17:09

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.