5

I'm brand new to QGIS but 10+ years ago I was adept with ArcGIS. I installed 3.2.0 just a few days ago and all went well at first. I located/downloaded/imported the data I want and added a field to the table in each layer. I'm using Windows 10 Home v.1803, 64-bit. Now I want to create new layers using that new field. Screeching halt.

I can't create a new layer. I can't clip the layer. I can't filter the layer. I can't create anything else, apparently, because I keep getting some version of this error:

"Export to vector file failed. Error: Creation of data source failed (OGR error: sqlite3_open(NC GeoRegions.gpkg) failed: unable to open database file)"

I'm not sure what database it's trying to open, or I would check out its read-write permission. And anyway I can't believe the message is accurate. If QGIS cannot "open" a database, how in the world can it read that database to populate attribute tables? It makes no sense on its face.

What am I doing wrong?

7
  • 1
    In QGIS 3.x the default data format is GeoPackage (which is a sqlite database with spatial extension in the background). A suppose you have not opened so far GeoPackage database but ESRI shape files, so there is a problem with the sqlite/spatialite installation. You should check weather sqlite is available. Several programs (even Windows) use sqlite, it happens that you have several version of sqlite.dll on your machine, and QGIS finds an incompatible version. Reordering the folders in PATH may help. Move QGIS folders to the beginning.
    – Zoltan
    Commented Jul 23, 2018 at 6:55
  • Thank you, Zoltan. It does seem like you have the right idea. Strange, that I have read as far as I have in the user manual and did not see, that it is possible to open and work with a shapefile and not have any geopackage open. I would think that's kind of an important concept... Anyway, may I ask you to clarify the last part? What PATH are you referring to? If QGIS doesn't create a geopackage with every new project, that must mean I have to create one myself, or import someone else's. How do I do that?
    – John A.
    Commented Jul 23, 2018 at 8:32
  • I see there was a folder added to the one I created to hold my data for the project. I did not put it there. It is called DEFAULT/PERMANENT/SQLITE. The PERMANENT folder has in it files called DEFAULT WIND, MYNAME, PROJ_EPSG, PROJ_INFO, PROJ_UNITS, VAR, and WIND. I have no idea what these files are - they have no file type extensions. Is one of these the database I'm trying to write to? Does PROJ mean "project" or "projection"?
    – John A.
    Commented Jul 23, 2018 at 8:53
  • I also see that every file in that directory has a size of only 1 kb, so I suppose none of them are databases. Also, the SQLITE folder is empty. I do not understand the purpose of this file structure...
    – John A.
    Commented Jul 23, 2018 at 9:31
  • 1
    For me (on mac) I was trying to write to a directory that I didn't have permission to write to. Once I changed the output location to my Desktop, I was able to export / save a temporary layer as a GPKG. Commented Mar 3, 2020 at 13:08

4 Answers 4

6

The easy solution to your issue is to export using the shapefile format instead of the geopackage format.

See below if you want to know why you're having this issue.


Geopackage is a database format. In one geopackage, you can have many layers. This is different from the shapefile format. Each shapefile layer is a separate file (actually 3-6 files, but you probably already know that).

You must have an existing geopackage before you can add a layer to it. When you try to export as a geopackage layer, QGIS can't find a geopackage to add the layer to. That's why you're getting the error message "unable to open database."

You must create a geopackage before you can add layers to it.

To create a geopackage database, go to the Layer menu > Create Layer > New Geopackage Layer. In the window that pops up, next to click the "..." button next to Database. Choose the folder where you want to put your database and type in a name. Click Save, then click OK.

Now you have a geopackage with one empty table. You can now export layers to this geopackage.

4
  • Thank you so much for the explanation, csk. Yes, I absolutely wanted to know why I was running into the problem. Your explanation is very clear, and I understand what I need to do now. Much appreciated!
    – John A.
    Commented Jul 31, 2018 at 22:39
  • Happy to help. FYI, the geopackage format is supposedly much better than the shapefile format. Eg, no character limit on field names, one single file as opposed to multiple files, etc. That said, I still use shapefiles for pretty much everything.
    – csk
    Commented Aug 1, 2018 at 17:21
  • I had the same error and your answer helped me realise that I had to click the … button to select a folder. Just giving a file name in the main screen was trying to save on the root of my drive (which it obviously couldn't). So thanks. Commented Aug 8, 2019 at 12:55
  • Glad it helped. That's a pretty common mistake, and the error message doesn't really help.
    – csk
    Commented Aug 9, 2019 at 14:50
2

In case anyone else is also still encountering this error: I've come to realize that, quite often in PyQGIS, errors are actually caused by something that happens earlier. I had this exact problem when trying to call the v.split function, and in my case the issue was that for some reason, it defaulted to format="". Manually setting it to format="GPKG" via the 'GRASS_VECTOR_LCO' parameter solved the issue for me. (Naturally, only do this if you know the input will actually be a GeoPackage.)

Of course, this is a quite specific example. A more general tip for troubleshooting an issue like that, and how I figured this one out, was by taking a very close look at the logs, not just the actual error messages but also any easy-to-miss warnings in the grey text within.

1

Well... this isn't an answer exactly, but I found a workaround that seems to do the trick for now. I still wish I knew why doing what I'm supposed to do doesn't yield the result it's supposed to as far as almost any function that involves writing to some mystery database. BUT --

After trying every other tool, I found one that works. And in the end it's just as effective. Not elegant, not proper, not efficient. But effective.

One of the Data Management tools in the Vector menu is "Split Vector Layer." I've done this now several dozen times. Honestly I don't know what the difference is between "Split Vector Layer" and "Create New Shapefile Layer" because if I could do the latter, the result I'd expect is exactly what I get with the Split tool.

I've been wanting to create a subset of the primary layer, so I select the features I want and split them. The source layer remains intact, the new layer contains just the selected items. (The tool is more versatile than that, but that's what I need it do do.) Imagine starting with a State layer but you want a separate layer for each county. This can do that, and each new layer contains all the fields of the parent layer.

In order to avoid confusion because of the default naming convention for the new layer, I go to the output folder from Windows Explorer and rename the new shapefile and its associated files.

1

I recently produced this exact same error when trying to create an gpkg output in a folder that doesn't exist yet. I added a routine that first creates the folder and that solved the issue.

# create output folder if it doesn't exist
if not os.path.exists(os.path.dirname(dataset_output)):
  os.makedirs(os.path.dirname(dataset_output))

Your Answer

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

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