﻿{"id":8449,"date":"2019-04-02T02:06:36","date_gmt":"2019-04-02T00:06:36","guid":{"rendered":"http:\/\/www.sigterritoires.fr\/?p=8449"},"modified":"2019-04-05T09:25:46","modified_gmt":"2019-04-05T07:25:46","slug":"requests-and-views-of-a-spatialite-database-with-qgis-2-8","status":"publish","type":"post","link":"https:\/\/www.sigterritoires.fr\/index.php\/en\/requests-and-views-of-a-spatialite-database-with-qgis-2-8\/","title":{"rendered":"Requests and views of a SpatiaLite database with QGis 2.8"},"content":{"rendered":"\n<p>One of the main differences between data management with shapefiles and\na database is the ability to create different \u00ab\u00a0views\u00a0\u00bb for the same\ndata. In this article, the last in the series devoted to SpatiaLite with QGis,\nwe&rsquo;ll discuss how to build SQL queries to generate SpatiaLite views. <br>\nDB Manager has a SQL window that allows the construction and execution of SQL\nqueries on a database. This article shows how to use the SQL window to query a\ntable and create a SpatiaLite spatial view. <\/p>\n\n\n\n<!--more-->\n\n\n\n<p><strong>How to create a SQL query<\/strong> <\/p>\n\n\n\n<p>The different databases support all (or some) SQL commands. SQLite supports\nmuch of the ANSI SQL92 standard, but not all. For a complete list of supported\nSQL operations, visit <a href=\"https:\/\/translate.google.com\/translate?hl=fr&amp;prev=_t&amp;sl=fr&amp;tl=en&amp;u=https:\/\/sqlite.org\/lang.html\">https:\/\/sqlite.org\/lang.html<\/a> \n\nTo create an SQL query, do the following: <br>\n1. Open the Database <strong><em>Manager<\/em><\/strong> by clicking Database <strong><em>Manager<\/em><\/strong>\nunder <strong><em>Database<\/em><\/strong> . <br>\n2. In the tree view, locate and select the database on which you want to run an\nSQL query. <br>\n3. Click <strong><em>Database |<\/em><\/strong><strong><em>SQL window<\/em><\/strong> , or press <strong><em>F2<\/em><\/strong>\non your keyboard, to open the SQL window. <br>\n4. Enter an SQL query in the text box at the top. Click on the <strong><em>execute<\/em><\/strong>\nbutton or <strong><em>F5<\/em><\/strong> on your keyboard to execute the SQL query on the\ndatabase. The results of the query will be displayed in the results area at the\nbottom, and the number of lines processed and the run time will be displayed\nnext to the button. &nbsp; <strong><em>Run<\/em><\/strong> . An example of a successful\nexecution request is shown in the following screenshot: \n\n\n\n<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"630\" height=\"467\" data-attachment-id=\"8450\" data-permalink=\"https:\/\/www.sigterritoires.fr\/index.php\/en\/requests-and-views-of-a-spatialite-database-with-qgis-2-8\/21-2\/\" data-orig-file=\"https:\/\/i0.wp.com\/www.sigterritoires.fr\/wp-content\/uploads\/2019\/04\/21.png?fit=630%2C467&amp;ssl=1\" data-orig-size=\"630,467\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"21\" data-image-description=\"\" data-image-caption=\"\" data-large-file=\"https:\/\/i0.wp.com\/www.sigterritoires.fr\/wp-content\/uploads\/2019\/04\/21.png?fit=630%2C467&amp;ssl=1\" src=\"https:\/\/i0.wp.com\/www.sigterritoires.fr\/wp-content\/uploads\/2019\/04\/21.png?resize=630%2C467&#038;ssl=1\" alt=\"\" class=\"wp-image-8450\" srcset=\"https:\/\/i0.wp.com\/www.sigterritoires.fr\/wp-content\/uploads\/2019\/04\/21.png?w=630&amp;ssl=1 630w, https:\/\/i0.wp.com\/www.sigterritoires.fr\/wp-content\/uploads\/2019\/04\/21.png?resize=300%2C222&amp;ssl=1 300w\" sizes=\"auto, (max-width: 630px) 100vw, 630px\" \/><\/figure>\n\n\n\n<p>You can store any query by entering a name in the text box at the top,\nand then clicking the <strong><em>Store<\/em><\/strong> button . To load and execute the\nstored query, select the query name from the drop-down menu at the top. To\ndelete a stored query, select the query from the drop-down menu and then click\nthe <strong><em>Clear<\/em><\/strong> button (next to <strong><em>Stocker<\/em><\/strong> ). <\/p>\n\n\n\n<p><strong>How to create a spatial view<\/strong> <\/p>\n\n\n\n<p>Creating a spatial view on a SpatiaLite database, with the SQL window in\nthe database manager, is a two-step process. <\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>The first step is to create a\nview containing a field with unique identifiers and the <em>geometry<\/em> column <\/li><li>The second step is to insert a\nnew record in the <em>views_geometry_columns<\/em> table to save the view as a spatial view <\/li><\/ul>\n\n\n\n<p>\n\n\n\nWe will create a spatial view on the Hotels\ntable to show all hotels in Brest and Quimper; To this end, proceed as follows\n: <br>\n1. Open the database manager. <br>\n2. In the tree view, <strong><em>locate<\/em><\/strong> and select the <strong><em>BaseTest.sqlite<\/em><\/strong>\ndatabase <strong><em>.<\/em><\/strong><br>\n3. Click <strong><em>Database |<\/em><\/strong><strong><em>SQL window<\/em><\/strong> , or press <strong><em>F2<\/em><\/strong>\non your keyboard, to open the SQL window. <br>\n4. Enter the following query: <br><strong><em>CREATE VIEW the_hotels_brest_quimper as<\/em><\/strong><br><strong><em>SELECT h.pk as ROWID, h.Name, h.PostalCode, h.Commune, h.geom from hotels\nas h<\/em><\/strong><br><strong><em>WHERE h.Commune in (&lsquo;Brest&rsquo;, &lsquo;Quimper&rsquo;);<\/em><\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"630\" height=\"462\" data-attachment-id=\"8451\" data-permalink=\"https:\/\/www.sigterritoires.fr\/index.php\/en\/requests-and-views-of-a-spatialite-database-with-qgis-2-8\/22-2\/\" data-orig-file=\"https:\/\/i0.wp.com\/www.sigterritoires.fr\/wp-content\/uploads\/2019\/04\/22.png?fit=630%2C462&amp;ssl=1\" data-orig-size=\"630,462\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"22\" data-image-description=\"\" data-image-caption=\"\" data-large-file=\"https:\/\/i0.wp.com\/www.sigterritoires.fr\/wp-content\/uploads\/2019\/04\/22.png?fit=630%2C462&amp;ssl=1\" src=\"https:\/\/i0.wp.com\/www.sigterritoires.fr\/wp-content\/uploads\/2019\/04\/22.png?resize=630%2C462&#038;ssl=1\" alt=\"\" class=\"wp-image-8451\" srcset=\"https:\/\/i0.wp.com\/www.sigterritoires.fr\/wp-content\/uploads\/2019\/04\/22.png?w=630&amp;ssl=1 630w, https:\/\/i0.wp.com\/www.sigterritoires.fr\/wp-content\/uploads\/2019\/04\/22.png?resize=300%2C220&amp;ssl=1 300w\" sizes=\"auto, (max-width: 630px) 100vw, 630px\" \/><\/figure>\n\n\n\n<p><strong><em>h<\/em><\/strong> or any letter is suitable as a table prefix for the\nfield names. <\/p>\n\n\n\n<p>The <strong><em>SELECT<\/em><\/strong> clause sets the fields that will be visible in\nthe view and the <strong><em>WHERE<\/em><\/strong> clause defines the selection criteria for\nthe records that will be visible in the view. <\/p>\n\n\n\n<p>In the <strong><em>CREATE VIEW<\/em><\/strong> query , &nbsp; two fields are required in\nthe <strong><em>SELECT statement<\/em><\/strong> : the unique identifier field that must be\nrenamed to <strong><em>ROWID<\/em><\/strong> and the field &nbsp; geometry. You must\nrename the unique identifier in &nbsp; ROWID or you will not be\nable to save the view as a spatial view. \n\n5. Click the <strong><em>execute<\/em><\/strong> button to\ncreate the view. <br>\nNow that the view is created, we need to save it as a spatial view by inserting\na new line into the <em>views_geometry_columns<\/em> table . This table links the\ngeometry of the view to the geometry of the table in which the selection takes\nplace. <br>\n6. In the SQL window, click the <strong><em>clear<\/em><\/strong> button to clear the text\nbox of the SQL query. <br>\n7. Enter the following query: <br><strong><em>INSERT INTO views_geometry_columns (view_name, view_geometry, view_rowid,\nf_table_name, f_geometry_column, read_only)<\/em><\/strong><br><strong><em>VALUES (&lsquo;les_hotels_brest_quimper&rsquo;, &lsquo;geom&rsquo;, &lsquo;rowid&rsquo;, &lsquo;hotels&rsquo;, &lsquo;geom&rsquo;,\n1);<\/em><\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"630\" height=\"466\" data-attachment-id=\"8452\" data-permalink=\"https:\/\/www.sigterritoires.fr\/index.php\/en\/requests-and-views-of-a-spatialite-database-with-qgis-2-8\/23-3\/\" data-orig-file=\"https:\/\/i0.wp.com\/www.sigterritoires.fr\/wp-content\/uploads\/2019\/04\/23.png?fit=630%2C466&amp;ssl=1\" data-orig-size=\"630,466\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"23\" data-image-description=\"\" data-image-caption=\"\" data-large-file=\"https:\/\/i0.wp.com\/www.sigterritoires.fr\/wp-content\/uploads\/2019\/04\/23.png?fit=630%2C466&amp;ssl=1\" src=\"https:\/\/i0.wp.com\/www.sigterritoires.fr\/wp-content\/uploads\/2019\/04\/23.png?resize=630%2C466&#038;ssl=1\" alt=\"\" class=\"wp-image-8452\" srcset=\"https:\/\/i0.wp.com\/www.sigterritoires.fr\/wp-content\/uploads\/2019\/04\/23.png?w=630&amp;ssl=1 630w, https:\/\/i0.wp.com\/www.sigterritoires.fr\/wp-content\/uploads\/2019\/04\/23.png?resize=300%2C222&amp;ssl=1 300w\" sizes=\"auto, (max-width: 630px) 100vw, 630px\" \/><\/figure>\n\n\n\n<p>The <strong><em>INSERT<\/em><\/strong> query has values for six fields: <\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><em>view_name<\/em> : This contains the name of the view that you want\nto register as a spatial view. <\/li><li><em>view_geometry<\/em> : It contains the name of the geometry field of the\nview. <\/li><li><em>view_rowid<\/em> : It contains the name of the rowid field. Note that he must be rowid. If the rowid field is named otherwise, you will\nneed to recreate the view with a named field &nbsp; rowid <\/li><li><em>f_table_name<\/em> : the name of the table whose view is a selection. <\/li><li><em>f_geometry_column<\/em> : the name of the geometry field in the table whose\nview is a selection <\/li><li><em>read_only<\/em> : in this field, enter 1 so that the spatial view\nis &nbsp; read-only, or &nbsp; enter 0 so that the spatial view\nis &nbsp; in read \/ write. Note that since QGIS version 2.6.0, read \/ write views can not be edited\nin QGIS Desktop. However, views can be editable in\nsome plugins or with SQL queries. <\/li><\/ul>\n\n\n\n<p>8. Click the <strong><em>execute<\/em><\/strong> button to create the view. <br>\nThe view is now saved as a spatial view and can be added as a layer in the QGis\nmap window, just like any other SpatiaLite spatial table. <\/p>\n\n\n\n<p><strong>How to remove<\/strong> <strong>&nbsp;<\/strong> <strong>a spatial view<\/strong> <\/p>\n\n\n\n<p>To remove a spatial view requires that you delete the spatial view\ntable, but also the relevant entry in the <strong><em>view_geometry_columns<\/em><\/strong>\ntable . <br>\nTo delete the table &nbsp; spatial view, use the <strong><em>DROP VIEW SQL<\/em><\/strong>\ncommand . For example, to remove the view from <em>the_hotels_brest_quimper<\/em>\n, you will need to execute the following SQL command: <\/p>\n\n\n\n<p><strong><em>DROP VIEW<\/em><\/strong>  <strong><em>les_hotels_brest_quimper<\/em><\/strong>\n<\/p>\n\n\n\n<p>With the view deleted, the last step is to delete the corresponding\nentry in the <em>view_geometry_columns<\/em> table using the <strong><em>SQL DELETE<\/em><\/strong>\ncommand . <br>\nFor example, to remove the writing about hotels in Brest and Quimper, you will\nneed to run the &nbsp; following SQL command: <\/p>\n\n\n\n<p><strong><em>DELETE FROM views_geometry_columns<\/em><\/strong> <br>\n<strong><em>WHERE<\/em><\/strong>  <strong><em>view_name =\n&lsquo;les_hotes_brest_quimper&rsquo;<\/em><\/strong>  <strong><em>;<\/em><\/strong> <\/p>\n","protected":false},"excerpt":{"rendered":"<p>One of the main differences between data management with shapefiles and a database is the ability to create different \u00ab\u00a0views\u00a0\u00bb for the same data. In this article, the last in the series devoted to SpatiaLite with&hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"give_campaign_id":0,"_bbp_topic_count":0,"_bbp_reply_count":0,"_bbp_total_topic_count":0,"_bbp_total_reply_count":0,"_bbp_voice_count":0,"_bbp_anonymous_reply_count":0,"_bbp_topic_count_hidden":0,"_bbp_reply_count_hidden":0,"_bbp_forum_subforum_count":0,"sfsi_plus_gutenberg_text_before_share":"","sfsi_plus_gutenberg_show_text_before_share":"","sfsi_plus_gutenberg_icon_type":"","sfsi_plus_gutenberg_icon_alignemt":"","sfsi_plus_gutenburg_max_per_row":"","_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_post_was_ever_published":false},"categories":[1260],"tags":[],"class_list":["post-8449","post","type-post","status-publish","format-standard","hentry","category-non-classe-en"],"aioseo_notices":[],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/p6XU0A-2ch","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/www.sigterritoires.fr\/index.php\/wp-json\/wp\/v2\/posts\/8449","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.sigterritoires.fr\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.sigterritoires.fr\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.sigterritoires.fr\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.sigterritoires.fr\/index.php\/wp-json\/wp\/v2\/comments?post=8449"}],"version-history":[{"count":0,"href":"https:\/\/www.sigterritoires.fr\/index.php\/wp-json\/wp\/v2\/posts\/8449\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.sigterritoires.fr\/index.php\/wp-json\/wp\/v2\/media?parent=8449"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.sigterritoires.fr\/index.php\/wp-json\/wp\/v2\/categories?post=8449"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.sigterritoires.fr\/index.php\/wp-json\/wp\/v2\/tags?post=8449"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}