7

Sample data for testing available here (the model described below is embedded in the QGIS project file)

In the context of a QGIS model, I am struggling to pass input parameters as part of a SQL query to be used in the Execute SQL tool.

I have this model:

enter image description here

And I use a pre-calculated expression as follows:

select 
    row_number() over() as id, 
    min('||@Fieldcontainingthecentralityscores||') as min, 
    ('||@FieldcontainingtheHierarchylevel||') as hierarchy
from '||@MatrixofFunctions||'
group by hierarchy'

The screenshot:

enter image description here

As you can see, what I do is to try to grab model-specific variables denoted by the @

However when I run the model I get this message:

Prepare algorithm: gdal:executesql_1
Running Execute SQL [1/1]
Input Parameters:
{ DIALECT: 0, INPUT: 'C:/Users/DaSilvaA/Desktop/oromia_MoF/Oromia_(incl_Addis)_MoF_final.gpkg|layername=Oromia_(incl_Addis)_MoF_final', OPTIONS: '', OUTPUT: 'TEMPORARY_OUTPUT', SQL: 'select \r\n\trow_number() over() as id, \r\n\tmin(centrality_score) as min, \r\n\t(Oromia_MoF_oct_2022_c_Hierarchy Level) as hierarchy\r\nfrom \r\ngroup by hierarchy' }
GDAL command:
ogr2ogr C:/Users/DaSilvaA/AppData/Local/Temp/processing_YDUAJo/3d541238b96f4b6cb5d51a547ba2f4ac/OUTPUT.gpkg "C:/Users/DaSilvaA/Desktop/oromia_MoF/Oromia_(incl_Addis)_MoF_final.gpkg" -sql "select 
row_number() over() as id, 
min(centrality_score) as min, 
(Oromia_MoF_oct_2022_c_Hierarchy Level) as hierarchy 
from 
group by hierarchy" -f "GPKG"
GDAL command output:
ERROR 1: In ExecuteSQL(): sqlite3_prepare_v2(select ):

incomplete input

I have tried many variations to the expression syntax formulation but in the end the model parameters do not pass into the query.

Does anyone know how to have input parameters interpreted in a SQL query inside a QGIS model?

9
  • Can you share ""Execute SQL" dialog window with all selected parameters? Commented Dec 19, 2022 at 0:02
  • I can't see the model in the qgis file. Can you jus save it as in the same folder you've shared ? Commented Dec 19, 2022 at 9:21
  • @CésarArquero Can the model be saved in the qgis file? If so, I didn't know that. Commented Dec 19, 2022 at 9:24
  • I think they are stored in a user folder, not within the qgis file... Just save as Commented Dec 19, 2022 at 9:35
  • @CésarArquero the zip file now includes the model. Thanks for taking the time. Commented Dec 19, 2022 at 16:29

3 Answers 3

5
+100

This seems to be some bug with how the GDAL execute SQL algorithm interprets the layer input.
You could replace it with the QGIS native Execute SQL tool, which has a slightly different way of understanding input names (you can just write 'input1', 'input2' and so on, basically just 'inputN'.

For example, I replaced the tool with your sample data and the query runs fine as long as I define the geometry type as no geometry:

This is the expression:
concat('select row_number() over() as id, min(','"',@Fieldcontainingthecentralityscores,'"',') as min, (','"',@FieldcontainingtheHierarchylevel,'"',') as hierarchy from input1 group by hierarchy')

side note: if you are already wrapping your text in concat, you don't really need all the || signs, you can just use commas. It makes the expression easier to read.

The definition of the model stays pretty much the same.

enter image description here

enter image description here

The output is a point layer, but without any geometry.

enter image description here

2

I found some kind of solution. I've checked plenty of places with different work around to get the layer name as parameter. Something is wrong with the capture of the layer name. Can't even addes as project parameter.

Here you have two of the tools that others came out with:

I hope this solution can be also useful for them. Let's go:

  1. Don't use input "vectorial layer", use "vectorial objects" instead (hope you can find it easily in the english layout of QGIS). enter image description here
  2. Add the name of the input using the function "layer_property" like here: enter image description here

Here is the SQL code:

concat('select row_number() over() as id, min(',
'"',@Fieldcontainingthecentralityscores,'"',') as min, (',
'"',@FieldcontainingtheHierarchylevel,'"',') as hierarchy from ', 
layer_property(@input, 'name') ,' group by hierarchy')

This is what worked for me. But there is always a but. The filename must be the same as the name of the layer in the QGIS canvas. If you prefer to rely on the name of the file instead, you have other functions to get the name of the file, like file_name.

enter image description here

Here you have the project with the updated model

1
  • 1
    Thank you so much Cesar for your answer, but I ha d no time to test your solution and I had to award the bounty points as they expire in a few yours. Commented Dec 26, 2022 at 19:33
0

Wherever MatrixofFunctions is coming from is apparently undefined, as its substituting a blank for the FROM table name, thus the error. Check the definition and make sure it outputs the desired table name.

3
  • 1
    Thanks, @dwhitemv. That is the problem - the input variable (a geopackage layer) is interpreted as NULL. My question is how avoid that. Commented Dec 6, 2022 at 9:16
  • Can you post the definition? Is the value present in the attributes table for that layer? Commented Dec 6, 2022 at 15:21
  • 1
    The query is correct and the attributes are present. Again, the issue here is that the variables representing the table and the attributes are evaluated as NULL. Commented Dec 7, 2022 at 8:50

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.