Hide last authors
gru 1.1 1 {{content/}}
2
XIMA Admin 71.1 3 Database queries are similar to [[data sources>>doc:Sources]] (XML, CSV, JSON). The important difference is that they are not static, but read dynamically from a database.
gru 1.1 4
5 {{info}}
XIMA Admin 71.1 6 Before you can create a new database query, you must create a [[database connection >>doc:DBConnections]] if you have not done so already.
gru 1.1 7 {{/info}}
8
XIMA Admin 71.1 9 == Creating a database query ==
gru 1.1 10
nlo 9.1 11 {{id name="data_db_query"/}}
XIMA Admin 71.1 12 {{figure image="data_db_query_de.png"}}Configuration screen for creating a database query in: (1) list of existing DB queries, (2) editor for defining the SQL query, (3) settings of the DB query, (4) console for testing the DB query.{{/figure}}
gru 1.1 13
awa 67.4 14
15 {{html wiki="true"}}
XIMA Admin 71.1 16 <div class='xm-figure xm-float-right xm-clear-h2' data-alt='Configuration screen for creating a database query in: (1) list of existing DB queries, (2) editor for defining the SQL query, (3) settings of the DB query, (4) console for testing the DB query.'><div class='xm-figure-inner' style='width:310px'>{{lightbox image='data_db_query_de.png' width='300' group='$height' group='$group' title='Configuration screen for creating a database query in: (1) list of existing DB queries, (2) editor for defining the SQL query, (3) settings of the DB query, (4) console for testing the DB query.'/}}<div class='xm-figure-caption'>Configuration screen for creating a database query in: (1) list of existing DB queries, (2) editor for defining the SQL query, (3) settings of the DB query, (4) console for testing the DB query.</div></div></div>
awa 67.4 17 {{/html}}
18
19
nlo 16.1 20
XIMA Admin 71.1 21 {{html wiki="true"}}
22 <div class='xm-figure xm-float-right xm-clear-h2' data-alt='Configuration screen for creating a database query in: (1) list of existing DB queries, (2) editor for defining the SQL query, (3) settings of the DB query, (4) console for testing the DB query.'><div class='xm-figure-inner' style='width:310px'>{{lightbox image='data_db_query_de.png' width='300' group='$height' group='$group' title='Configuration screen for creating a database query in: (1) list of existing DB queries, (2) editor for defining the SQL query, (3) settings of the DB query, (4) console for testing the DB query.'/}}<div class='xm-figure-caption'>Configuration screen for creating a database query in: (1) list of existing DB queries, (2) editor for defining the SQL query, (3) settings of the DB query, (4) console for testing the DB query.</div></div></div>
23 {{/html}}
gru 1.1 24
25
XIMA Admin 71.1 26 * Open the module "Database queries" and click "New" {{ficon name="plus-circle-outline"/}} in the header of the list (see [[figure>>||anchor="fig_data_db_query"]]).
27 * The following data is needed for a Database query:
28 ** **Name**: A unique name for the database query
29 ** **Description**: An optional description for the database query
30 ** **Connection**: The [[Database connection>>doc:DBConnections]] that should be used
nlo 69.1 31
XIMA Admin 71.1 32 * Enter the **SQL statement** to be executed for the query in the editor (see [[figure>>||anchor="data_db_query"]]).
gru 1.1 33
XIMA Admin 71.1 34 The entered SQL statement is executed as a prepared statement, which prevents SQL injection attacks. You should not and need not use inverted commas (` or '). Also, you can use question marks ( ? ) as placeholders to build queries dynamically.
gru 1.1 35
XIMA Admin 71.1 36 {{version major="6" minor="4" patch="0"/}} [[Variables>>doc:Formcycle.UserInterface.Variables.WebHome]] can be used in the SQL statements.
37
38 == Using the database query ==
39
40 You can access the database query by making a HTTP request to the corresponding database servlet and providing the required parameters as GET parameters. The URL to the database query servlet is as follows.
41
gru 69.2 42 {{code language="none"}}
gru 1.1 43 http://<server>/formcycle/datenabfragedb
44 {{/code}}
45
XIMA Admin 71.1 46 The servlet URL is displayed beneath the settings (see [[figure>>||anchor="data_db_query"]]).
47 The following URL parameters are supported:
gru 1.1 48
awa 4.2 49 {{table dataTypeAlpha="0" preSort="0-asc"}}
XIMA Admin 71.1 50 |=Name of the paramter|=Description|=Required
51 |name|Must match the name of the database query.|Yes
52 |clientName|Must match the name of the client used for creating this data source.|Yes, if //projektId// is not given
53 |projektId|Must match the ID of the form. This information can be seen by accessing the {{code language="javascript"}}XFC_METADATA.currentProject.id{{/code}} object from JavaScript.|Yes, if //mandantName// is not given
54 |--sqlParameter--|Alias for //queryParameter//. Should no longer be used in {{formcycle/}} version 6 and will most likely be removed in the next major release of {{formcycle/}}.|No
55 |queryParameter|When the query contains placeholders (question marks, {{code language="none"}}?{{/code}}), a list of parameters must be supplied for each placeholder. The number of items must match the number of parameters used in the SQL query. The items are separated with the delimiter as defined by the URL parameter //delimiter//.
gru 70.1 56
XIMA Admin 71.1 57 If possible the parameter //queryParameterValues// should be used for new projects instead of //queryParameter// because //queryParameter// will not be supported in a future version of {{formcycle/}}.|No
58 |queryParameterValues|{{version major="6" minor="6" patch="3"/}}Starting with {{formcycle/}} Version 6.6.3 this parameter can be used as an alternative to the parameters //queryParameter// and //delimiter//. Like these parameters, //queryParameterValues// is only required if placeholders in the form of a question mark {{code language="none"}}?{{/code}} are used within the SQL query. If this is the case, the individual query parameters are passed one after the other as a separate parameter //queryParameterValues//, which also eliminates the use of the parameter //delimiter//.|No
59 |varName|Allows you to change name of the JSON response object. If not given, a plain JSON object with the result data is returned.|No
60 |delimiter|The delimiter for the placeholder values, see //queryParameter//. Defaults to a comma {{code language="none"}},{{/code}}
gru 70.1 61
XIMA Admin 71.1 62 If possible the parameter //queryParameterValues// should be used for new projects instead of //delimiter// and //queryParameter// because //delimiter// will not be supported in a future version of {{formcycle/}}.|No
awa 4.2 63 {{/table}}
gru 1.1 64
XIMA Admin 71.1 65 When accessing a database servlet from a form, always use the database URL contained in the global object //XFC_METADATA//, see also the [[metadata>>doc:Formcycle.FormDesigner.CodingPanel.ScriptTab.FormMetadata]]. For example: {{code language="javascript"}}XFC_METADATA.urls.datasource_db{{/code}}.
gru 1.1 66
XIMA Admin 71.1 67 Further we recommend you use the script function {{jsdoc page="xutil" name="getdataquery"/}}, so you do not have to setup the servlet request manually. The result of the database query is returned as //JSON//.
gru 1.1 68
XIMA Admin 71.1 69 == Testing the query ==
gru 1.1 70
nlo 52.1 71 {{info}}
XIMA Admin 71.1 72 For quick testing of the query the shortcut {{code language="none"}}Ctrl + Enter{{/code}} is provided.
nlo 52.1 73 {{/info}}
74
XIMA Admin 71.1 75 Database queries can be tested directly from the configuration UI. For this purpose a test console is provided below the SQL editor (see [[figure>>||anchor="fig_data_db_query"]]).
76 In the header of the console there is a row of buttons for controling the query:
nlo 34.1 77
XIMA Admin 71.1 78 * {{ficon name="database-search"/}}**Perform query**
79 Runs the database query. If //query parameters// ({{code language="none"}}?{{/code}}) are provided the user will be prompted to input values vor those parameters. Otherwise the result of the query will be displayed directly in the //table view//.{{lightbox image="data_db_query_test_en.png" title="Run the given query"/}}
80 * {{icon name="question"/}}**Query parameters**
81 Mask for inputting values for query parameters. This option is only available if query parameters ({{code language="none"}}?{{/code}}) are used in the SQL query. The individual parameters will be enumerated in the SQL query. Clicking "User parameters for query" {{ficon name="arrow-right-bold-circle-outline2"/}} executes the query with the given parameters. The result will be displayed in the //table view//.
82 {{lightbox image="data_db_query_test_param_en.png" title="Query parameters are enumerated"/}}
83 Actual Query in the SQL editor:{{lightbox image="data_db_query_example_de.png" title="Example query"/}}
84 * {{ficon name="table-large"/}}**Table view**
85 Query result in table view{{lightbox image="data_db_query_test_table_en.png" title="Query result in table view"/}}
86 * {{icon name="code"/}}**Source code view**
87 Query result in JSON format{{lightbox image="data_db_query_test_code_en.png" title="Query result in JSON format"/}}
88 * {{icon name="Terminal"/}}**Generated SQL**
89 Displays the generated SQL statement with input parameter values{{lightbox image="data_db_query_test_sql_en.png" title="Displays the generated SQL statement with input parameter values"/}}
nlo 30.1 90
XIMA Admin 71.1 91 == Selection form elements ==
gru 1.1 92
XIMA Admin 71.1 93 If you want to display the returned data as options of a [[selection element>>doc:Formcycle.FormDesigner.FormElements.Selection]], you can do so easily by opening the {{designer/}} and selecting the database query as the data source of the selection element.
gru 1.1 94
XIMA Admin 71.1 95 {{info}}
96 Queries that are used in selection elements must not have a question mark "?". Queries with a "?" are not offered as a data source for selection elements.
97 {{/info}}
98
99 {{figure image="data_db_query_designer_en.png"}}
100 Using the result of a database query as the data source a select element in the {{designer case="dat"/}}.
nlo 54.1 101 {{/figure}}
gru 1.1 102
XIMA Admin 71.1 103 The result of the database query is used to create the options of the [[selection element>>doc:Formcycle.FormDesigner.FormElements.Selection]] in the following order:
gru 1.1 104
105 {{box}}
XIMA Admin 71.1 106 Displayed value, submitted value, optional value 1, optional value 2, ...
gru 1.1 107 {{/box}}
108
XIMA Admin 71.1 109 All returned columns are added as the value of the HTML attribute //col0// (displayed value), //col1// (submitted value), //col2// (optional value 1), //col3// (optional value 2) etc. to the corresponding //option// element.
gru 1.1 110
XIMA Admin 71.1 111 The displayed value is visible to the user directly when selecting an option. The submitted value is the value of the HTML attribute //value// and it is sent when the form is submitted.
gru 1.1 112
XIMA Admin 71.1 113 Optional values returned by the database query may be access as follows with //JavaScript//.
gru 1.1 114
115 {{code language="javascript"}}
XIMA Admin 71.1 116 $('[name=sel2]').find('option:selected').attr('col2') // Selects the active option of the selection element named 'sel2' and returns the first optional value.
gru 1.1 117 {{/code}}
118
XIMA Admin 71.1 119 == Examples ==
gru 1.1 120
gru 70.10 121 {{info}}
XIMA Admin 71.1 122 {{version major="6" minor="6" patch="3"/}}Starting with {{formcycle/}} Version 6.6.3, the parameter //queryParameterValues// can be used instead of the parameter //queryParameter//. The former is recommended for new projects because the parameter //queryParameter// will not be supported in a future version of {{formcycle/}}. The following examples therefore show one servlet query with //queryParameter// and one with //queryParameterValues// for each SQL query.
123 {{/info}}
gru 70.9 124
gru 70.10 125
gru 1.1 126 {{code language="sql"}}
XIMA Admin 71.1 127 select name, first_name from table where first_name like (?)
gru 1.1 128 {{/code}}
129
XIMA Admin 71.1 130 This SQL statement returns the names of all persons with a certain first name. The first name to search for is specified via an URL parameter.
awa 4.2 131
XIMA Admin 71.1 132 URL for running the query:
133 {{code language="none"}}http://myserver/formcycle/datenquelledb?mandantName=myself&name=demo&queryParameter=Robinson{{/code}}
gru 70.11 134
XIMA Admin 71.1 135 {{version major="6" minor="6" patch="3"/}}URL that can be used starting with {{formcycle/}} version 6.6.3 for running the query:
136 {{code language="none"}}http://myserver/formcycle/datenquelledb?mandantName=myself&name=demo&queryParameterValues=Robinson{{/code}}
137 \\
gru 70.7 138
gru 1.1 139 {{code language="sql"}}
XIMA Admin 71.1 140 select name, first_name from table where id = ?
gru 1.1 141 {{/code}}
142
XIMA Admin 71.1 143 Retrieves the name of a person with a certain ID. The ID is given as an URL parameter.
awa 4.2 144
XIMA Admin 71.1 145 URL for running the query:
146 {{code language="none"}}http://myserver/formcycle/datenquelledb?mandantName=myself&name=demo&queryParameter=100{{/code}}
gru 70.11 147
XIMA Admin 71.1 148 {{version major="6" minor="6" patch="3"/}}URL that can be used starting with {{formcycle/}} version 6.6.3 for running the query:
149 {{code language="none"}}http://myserver/formcycle/datenquelledb?mandantName=myself&name=demo&queryParameterValues=100{{/code}}
150 \\
gru 70.7 151
gru 1.1 152 {{code language="sql"}}
XIMA Admin 71.1 153 select name, first_name from table where city like(?) AND zip = ?
gru 1.1 154 {{/code}}
155
XIMA Admin 71.1 156 Retrieves the names of all persons that live in certain city. The city and the ZIP code are given as URL parameters.
awa 4.2 157
XIMA Admin 71.1 158 URL for running the query:
159 {{code language="none"}}http://myserver/formcycle/datenquelledb?mandantName=myself&name=demo&queryParameter=Paris,75001{{/code}}
gru 70.11 160
XIMA Admin 71.1 161 {{version major="6" minor="6" patch="3"/}}URL that can be used starting with {{formcycle/}} version 6.6.3 for running the query:
162 {{code language="none"}}http://myserver/formcycle/datenquelledb?mandantName=myself&name=demo&queryParameterValues=Paris&queryParameterValues=75001{{/code}}
163 \\
gru 70.7 164
MKO 68.1 165 {{code language="sql"}}
166 select name, vorname from tabelle where ort like concat(?, '%')
167 {{/code}}
168
XIMA Admin 71.1 169 This SQL statement returns the names of all persons who live in a place that **starts **with the given characters. The '%' character serves as a wildcard for any number of characters. Depending on the DBMS used, the syntax may differ slightly (here: MySQL). The requested value is passed via URL parameters.
MKO 68.1 170
XIMA Admin 71.1 171 URL for running the query:
172 {{code language="none"}}http://myserver/formcycle/datenquelledb?mandantName=myself&name=demo&queryParameter=Par{{/code}}
gru 70.11 173
XIMA Admin 71.1 174 {{version major="6" minor="6" patch="3"/}}URL that can be used starting with {{formcycle/}} version 6.6.3 for running the query:
175 {{code language="none"}}http://myserver/formcycle/datenquelledb?mandantName=myself&name=demo&queryParameterValues=Par{{/code}}
176 \\
gru 70.7 177
MKO 68.1 178 {{code language="sql"}}
179 select name, vorname from tabelle where lower(ort) like concat('%', lower(?), '%')
180 {{/code}}
181
XIMA Admin 71.1 182 This SQL statement returns the names of all persons who live in a place that **contains **the given characters. Upper/lower case spelling is irrelevant becauce the statement converts both, the value column and the actual filter value to lower case (lower(...)). The requested value is passed via URL parameters.
MKO 68.1 183
XIMA Admin 71.1 184 URL for running the query:
185 {{code language="none"}}http://myserver/formcycle/datenquelledb?mandantName=myself&name=demo&queryParameter=aRi{{/code}}
gru 70.11 186
XIMA Admin 71.1 187 {{version major="6" minor="6" patch="3"/}}URL that can be used starting with {{formcycle/}} version 6.6.3 for running the query:
188 {{code language="none"}}http://myserver/formcycle/datenquelledb?mandantName=myself&name=demo&queryParameterValues=aRi{{/code}}
Copyright 2000-2024