1

I need to connect to a SQLite/SpatiaLite database to copy data there. I have done the same with PostgreSQL/PostGIS like this:

connection_string = "dbname={} user={} password={} host={} port={}".format(
                self.dbname,
                config.get_config_value(dbsettings, "user"),
                config.get_config_value(dbsettings, "password"),
                config.get_config_value(dbsettings, "host"),
                config.get_config_value(dbsettings, "port")
            )
ogr.Open("PG:" + connection_string)

I followed instructions here where it is specified how to connect to PostgreSQL. But in the SQLite section, there isn't any information on connecting to the database. I suppose there also will be a connection string with a path to the database file instead of port and host but I need the exact syntax.

I am using Python 3.5.

EDIT:

I need to use ogr.open (not sqlite3.connect) because I store data to the database using CopyLayer (a method of ogr.open) as can be seen here (this is for PsotgreSQL):

    dsc_in = ogr.Open(file_name)
    dsc_out = ogr.Open("PG:" + self.target)

    layer = dsc_out.CopyLayer(dsc_in.GetLayer(), identifier,
                              ['OVERWRITE=YES'])
    if layer is None:
        raise Exception("Writing output data to the database failed.")
1

As far as I know, I don't think you need to build a connection string (as opposed to db services) for sqlite connections. This works fine for me:

from osgeo import ogr
ds = ogr.Open('spatialite.db')

for i in range(ds.GetLayerCount()):
    print(df.GetLayerByIndex(i).GetName())
  • side note: please check if your gdal/ogr was compiled with spatialite support (run ogrinfo --formats and spatialite should be in the list) – nickves Jun 4 '18 at 9:45
  • When I do is like this, ds is None. Any idea what might be causing it or where to look? I am using Linux Bash Shell on Win 10. – Jan Pisl Jun 7 '18 at 16:55
  • I run the "ogrinfo --formats" command and spatialite is on the list ( SQLite -vector- (rw+v): SQLite / Spatialite) – Jan Pisl Jun 7 '18 at 16:56
  • 1
    the usual suspect in these case if that the file can't be seen. try os.path.isfile('mydirectory/sqlite.db'). It should return True. If it does do ogrinfo on your sqllite file, if the program relevant information its not a program with gdal/ogr – nickves Jun 7 '18 at 17:28
  • os.path.isfile('mydirectory/sqlite.db') returns True. When I run ogrinfo testdb.sqlite3, it returns "FAILURE: Unable to open datasource `testdb.sqlite3' with the following drivers." (and then a list of drivers). SQLite is on the list. – Jan Pisl Jun 7 '18 at 17:41
1

Python has a native library for working with sqlite, you might be better off using that.

import sqlite3 sqlite3.connect("path/to/db")

  • I need to use ogr.open. I updated the question so it is clear why. But maybe I am missing something.. – Jan Pisl Jun 3 '18 at 14:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.