OK! That makes sense now. I have one last standing issue about this. It seems that the second layer I created using &#39;v.db.addtable&#39; (after setting the dbf to SQL and importing a centroid –vector– map) has a column for CAT but it does not have a number. I checked the book to make sure and it says it should have a number and it does not have to be continuous from the first layer. <div>
<br></div><div>I tried assigning a CAT# using &#39;v.category&#39; and &#39;v.db.connect&#39; but I still do not see a number in CAT column. I think I should have a number there so that my &quot;...WHERE cat = ....&quot; argument makes sense. Any ideas how to correct this?</div>
<div><br></div><div>Thank you,</div><div><br></div><div>Bulent   <br><br><div class="gmail_quote">On Thu, Jan 5, 2012 at 12:27 PM, Micha Silver <span dir="ltr">&lt;<a href="mailto:micha@arava.co.il">micha@arava.co.il</a>&gt;</span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><u></u>

  
    
    
    
  
  <div style="direction:ltr" text="#000000" bgcolor="#ffffff"><div class="im">
    On 01/05/2012 10:49 AM, Bulent Arikan wrote:
    <blockquote type="cite">Hi,
      <div><br>
      </div>
      <div>I really would like to learn more about the SQL command-based
        operations in GRASS. To summarize: I have created a new mapset
        where the driver is set to SQL. I have created second layers for
        all of my centroids (the tables in the second layers have a
        different name like &quot;Db_runivar&quot; ). I used &#39;v.db.addtable&#39; to
        have GRASS create columns to upload the results from extended
        statistics of r.univar text files (e.g., Db_runivar.txt) for
        each centroid. </div>
      <div><br>
      </div>
      <div>Now, I have been trying to get &#39;db.execute&#39; to work. One
        suggestion was that I use a loop for automated updating (<span style="border-collapse:collapse;color:rgb(34,34,34);font-family:arial,sans-serif;font-size:13px">I am assuming
          that I have to type this in the Terminal</span>):</div>
      <div><br>
      </div>
      <div><span style="border-collapse:collapse;color:rgb(34,34,34);font-family:arial,sans-serif;font-size:13px">&gt;
          while read l; do \<br>
              col=`echo $l | cut -d= -f1`; val=`echo $l | cut -d= -f2`;
          \<br>
              sqlite3 sqlite.db &quot;UPDATE centroid SET ${col}=${val} ;&quot;; \<br>
          &gt; done &lt; univar.txt</span></div>
      <div><span style="border-collapse:collapse;color:rgb(34,34,34);font-family:arial,sans-serif;font-size:13px"><br>
        </span></div>
      <div><span style="border-collapse:collapse;color:rgb(34,34,34);font-family:arial,sans-serif;font-size:13px"><br>
        </span></div>
      <div><span style="border-collapse:collapse;color:rgb(34,34,34);font-family:arial,sans-serif;font-size:13px">I came
          up with a statement (below) by looking at the manual in
          &#39;r.univar&#39; to create an SQL command file:</span></div>
      <div><span style="border-collapse:collapse;color:rgb(34,34,34);font-family:arial,sans-serif;font-size:13px"><br>
        </span></div>
      <div><span style="border-collapse:collapse;color:rgb(34,34,34);font-family:arial,sans-serif;font-size:13px">
          <p style="margin:0px;font:13px &#39;Lucida Grande&#39;">
          </p>
          <p style="margin:0px;font:13px &#39;Lucida Grande&#39;">sed -e
            &#39;1d&#39; Dogubayazit_runivar.txt | awk -F&#39;l&#39; &#39;{print &quot;UPDATE
            Dogubayazit_runivar SET non_null_cells = &quot;$2&quot;, null_cells =
            &quot;$3&quot;, min = &quot;$4&quot;, max = &quot;$5&quot;, range = &quot;$6&quot;, mean = &quot;$7&quot;,
            mean_of_abs = &quot;$8&quot;, stddev = &quot;$9&quot;, variance = &quot;$10&quot;,
            coeff_var = &quot;$11&quot;, sum = &quot;$12&quot;, sum_abs = &quot;$13&quot;, first_quart
            = &quot;$14&quot;, median = &quot;$15&quot;, third_quart = &quot;$16&quot;, perc_90  =
            &quot;$17&quot; WHERE cat = &quot;$1&quot;;&quot;}&#39;  </p>
        </span></div>
      <div><span style="border-collapse:collapse;color:rgb(34,34,34);font-family:arial,sans-serif;font-size:13px"><br>
        </span></div>
    </blockquote>
    <br></div>
    The above will create a list of update statements, one for each row
    in the runivar.txt file. You can&#39;t feed db.execute a list of
    statements, only one at a time. So you&#39;ll have to dump the output of
    the above command into an intermediary file, then use the db.execute
    &quot;input&quot; parameter to specify that file of SQL commands. <br>
    <br>
    It might be worth noting that if you have many UPDATE&#39;s, then doing
    them one by one with an individual call to the database each time
    might take a bit of time. In this case, going back to sqlite, you
    could wrap all the UPDATE statements between a BEGIN...COMMIT
    clause, then feed that file to db.execute (or directly to sqlite),
    and it will open one connection, and push all the updates at once.
    For many 1000&#39;s of rows this would probably be much faster.<div><div class="h5"><br>
    <br>
    <blockquote type="cite">
      <div><span style="border-collapse:collapse;color:rgb(34,34,34);font-family:arial,sans-serif;font-size:13px">that can
          be used in &#39;db.execute&#39; but I am getting a syntax error:</span></div>
      <div><span style="border-collapse:collapse;color:rgb(34,34,34);font-family:arial,sans-serif;font-size:13px"><br>
        </span></div>
      <div>DBMI-SQLite driver error:</div>
      <div>Error in sqlite3_prepare():</div>
      <div>near &quot;sed&quot;: syntax error</div>
      <div>ERROR: Error while executing: &#39;sed -e &#39;1d&#39; ............</div>
      <div><span style="border-collapse:collapse;color:rgb(34,34,34);font-family:arial,sans-serif;font-size:13px"><br>
        </span></div>
      <div><span style="border-collapse:collapse;color:rgb(34,34,34);font-family:arial,sans-serif;font-size:13px">   </span></div>
      <div>What seems to be the problem?   </div>
      <div>Thank you for your time.</div>
      <div><br>
      </div>
      <div>Bulent<br>
        <br>
        <div class="gmail_quote">On Wed, Jan 4, 2012 at 9:16 AM, Micha
          Silver <span dir="ltr">&lt;<a href="mailto:micha@arava.co.il" target="_blank">micha@arava.co.il</a>&gt;</span>
          wrote:<br>
          <blockquote class="gmail_quote" style="margin:0pt 0pt 0pt 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">
            <div style="direction:ltr" bgcolor="#ffffff" text="#000000">
              <div> On 01/03/2012 10:44 PM, Bulent Arikan wrote:
                <blockquote type="cite">Thank you! This seems to be an
                  interesting solution to the problem especially because
                  it involves changing the driver from DBF to SQLITE. I
                  ended up using &#39;v.rast.stats&#39;, which adds columns for
                  extended statistics and uploads values all at once.
                  The only thing is: data are recorded as part of the
                  same layer (i.e., Layer 1). So, I cheated!
                  <div> <br>
                  </div>
                </blockquote>
                <br>
                <br>
              </div>
              Yes, v.rast.stats is definitely the way to go if you want
              raster univariate statistics pushed into a polygon vector.
              <div><br>
                <br>
                <br>
                <blockquote type="cite">
                  <div>However, I started trying your method. I created
                    a new mapset to use sqlite and copied some vector
                    maps from a mapset where default driver is DBF. I
                    defined SQLITE as the new driver in the new mapset
                    using &#39;db.connect&#39; </div>
                  <div>(driver=sqlite,
                    database=&#39;$GISDBASE/$LOCATION_NAME/$MAPSET/sqlite.db&#39;,
                    no flags checked). Then I wanted to use
                    &#39;v.db.connect&#39; for a vector map but I cannot select
                    table name etc. I also tried just using
                    &#39;v.db.connect&#39; but I still cannot see anything under
                    Table. So. I am confused about how to define a new
                    driver in a mapset.</div>
                  <div><br>
                  </div>
                </blockquote>
                <br>
              </div>
              I do as follows:<br>
              eval `g.gisenv`<br>
              (This creates the environment variables for GISDBASE,
              etc.)<br>
              Now:<br>
              db.connect driv=sqlite
              database=$GISDBASE/$LOCATION_NAME/$MAPSET/sqlite.db<br>
              <br>
              Next:<br>
              g.copy vect=old,new<br>
              to create a new copy of the original vector. The original
              will still have its attrib table as dbf. THe new copy will
              have an sqlite based attribute table.<br>
              Now you can do:<br>
              <br>
              v.db.addcol &lt;new_vector&gt; col=&quot;..., ...&quot; .<br>
              <br>
              BTW, I think that v.rast.stats will automatically create
              the needed columns .<br>
              <br>
              Cheers,<br>
              Micha
              <div>
                <div><br>
                  <br>
                  <blockquote type="cite">
                    <div>Thank you again,</div>
                    <div><br>
                    </div>
                    <div>Bulent<br>
                      <br>
                      <div class="gmail_quote">On Tue, Jan 3, 2012 at
                        9:13 PM, Micha Silver <span dir="ltr">&lt;<a href="mailto:micha@arava.co.il" target="_blank">micha@arava.co.il</a>&gt;</span>
                        wrote:<br>
                        <blockquote class="gmail_quote" style="margin:0pt 0pt 0pt 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">
                          <div style="direction:ltr" bgcolor="#ffffff" text="#000000">
                            <div> On 01/03/2012 04:50 PM, Bulent Arikan
                              wrote:
                              <blockquote type="cite">Dear List,
                                <div><br>
                                </div>
                                <div>I rasterized a centroid and ran
                                  &#39;r.univar&#39;, whose extended stats are
                                  saved as a text file. Then, I ran
                                  &#39;v.db.addtable&#39; to create a second
                                  layer in the attribute table of the
                                  centroid and I had columns added in
                                  this second layer using &#39;v.db.addcol&#39;.
                                  I want GRASS to upload values from the
                                  text file but I am not sure how to
                                  perform this task. </div>
                                <div><br>
                                </div>
                                <div>Is there a shorter way of creating
                                  a second layer and uploading values
                                  from the text file or what should I do
                                  next so that the columns in the second
                                  layer will be populated using the text
                                  file I have?</div>
                                <div><br>
                                </div>
                              </blockquote>
                              <br>
                            </div>
                            I&#39;m not sure about a shorter way, but here&#39;s
                            an option:<br>
                            First setup your mapset to save attributes
                            to sqlite. <br>
                            &gt; v.db.connect centroid driver=sqlite
                            database=.... <br>
                            <br>
                            Now add the columns which will accept
                            r.univar values to the sqlite table:<br>
                            &gt; v.db.addcol centroid col=&quot;n double,
                            null_cells double, cells double, min double,
                            max double, range double, mean double,
                            mean_of_abs double, stddev double, variance
                            double, coeff_var double, sum double&quot;<br>
                            <br>
                            Run r.univar on your raster, putting results
                            into a text file<br>
                            &gt; r.univar your_rast -g  &gt; univar.txt<br>
                            <br>
                            Now do this loop to update values for the
                            centroid:<br>
                            &gt; while read l; do \<br>
                                col=`echo $l | cut -d= -f1`; val=`echo
                            $l | cut -d= -f2`; \<br>
                                sqlite3 sqlite.db &quot;UPDATE centroid SET
                            ${col}=${val} ;&quot;; \<br>
                            &gt; done &lt; univar.txt<br>
                            <br>
                            Assuming you want to run this for several
                            centroids/areas, you&#39;ll probably want to add
                            a WHERE clause to the UPDATE statement so as
                            to put values for only one certain row
                            (centroid).<br>
                            <br>
                            HTH, Micha<br>
                            <br>
                            <br>
                            <blockquote type="cite">
                              <div>
                                <div>Thank you for your time.<br clear="all">
                                  <div><br>
                                  </div>
                                  <div>GRASS 6.5 svn on Snow Leopard</div>
                                  <div><br>
                                  </div>
                                  <div><br>
                                  </div>
                                  -- <br>
                                  BÜLENT<br>
                                </div>
                                <br>
                              </div>
                              This mail was received via Mail-SeCure
                              System.<br>
                              <pre><fieldset></fieldset>
_______________________________________________
grass-user mailing list
<a href="mailto:grass-user@lists.osgeo.org" target="_blank">grass-user@lists.osgeo.org</a>
<a href="http://lists.osgeo.org/mailman/listinfo/grass-user" target="_blank">http://lists.osgeo.org/mailman/listinfo/grass-user</a>

This mail was received via Mail-SeCure System.


</pre>
                              <span><font color="#888888"> </font></span></blockquote>
                            <span><font color="#888888">
                                <p><br>
                                </p>
                                <br>
                                <pre cols="72">-- 
Micha Silver
GIS Consultant, Arava Development Co.
<a href="http://www.surfaces.co.il" target="_blank">http://www.surfaces.co.il</a></pre>
                              </font></span></div>
                        </blockquote>
                      </div>
                      <br>
                      <br clear="all">
                      <div><br>
                      </div>
                      -- <br>
                      BÜLENT ARIKAN, PhD
                      <div>Senior Research Fellow</div>
                      <div>Research Center for Anatolian Civilizations<br>
                        Koç University<br>
                        İstiklal Caddesi No: 181 Merkez Han</div>
                      <div>Beyoğlu - ISTANBUL</div>
                      <div>TURKEY<br>
                        34433</div>
                      <div><a href="tel:%28%2B%2090%29%20212-393-6036" value="+902123936036" target="_blank">(+ 90)
                          212-393-6036</a></div>
                      <br>
                    </div>
                    <br>
                    This mail was received via Mail-SeCure System.<br>
                  </blockquote>
                  <p><br>
                  </p>
                </div>
              </div>
            </div>
          </blockquote>
        </div>
        <br>
        <br clear="all">
        <div><br>
        </div>
        <br>
        <br>
      </div>
      <br>
      This mail was received via Mail-SeCure System.<br>
    </blockquote>
    <p><br>
    </p>
  </div></div></div>

</blockquote></div><br><br clear="all"><div><br></div>-- <br>BÜLENT ARIKAN, PhD<div>Senior Research Fellow</div><div>Research Center for Anatolian Civilizations<br>Koç University<br>İstiklal Caddesi No: 181 Merkez Han</div>
<div>Beyoğlu - ISTANBUL</div><div>TURKEY<br>34433</div><div>(+ 90) 212-393-6036</div><br>
</div>