File: queries.html

package info (click to toggle)
qdjango 0.2.5-2
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 2,144 kB
  • sloc: cpp: 6,085; perl: 104; makefile: 6
file content (162 lines) | stat: -rw-r--r-- 12,971 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=9"/>
<title>QDjango: Making queries</title>

<link href="tabs.css" rel="stylesheet" type="text/css"/>
<link href="doxygen.css" rel="stylesheet" type="text/css" />

<link href="search/search.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript" src="search/search.js"></script>
<script type="text/javascript">
  $(document).ready(function() { searchBox.OnSelectItem(0); });
</script>

</head>
<body>
<div id="top"><!-- do not remove this div! -->


<div id="titlearea">
<table cellspacing="0" cellpadding="0">
 <tbody>
 <tr style="height: 56px;">
  
  
  <td style="padding-left: 0.5em;">
   <div id="projectname">QDjango
   
   </div>
   
  </td>
  
  
  
 </tr>
 </tbody>
</table>
</div>

<!-- Generated by Doxygen 1.7.6.1 -->
<script type="text/javascript">
var searchBox = new SearchBox("searchBox", "search",false,'Search');
</script>
  <div id="navrow1" class="tabs">
    <ul class="tablist">
      <li><a href="index.html"><span>Main&#160;Page</span></a></li>
      <li class="current"><a href="pages.html"><span>Related&#160;Pages</span></a></li>
      <li><a href="modules.html"><span>Modules</span></a></li>
      <li><a href="annotated.html"><span>Classes</span></a></li>
      <li><a href="files.html"><span>Files</span></a></li>
      <li>
        <div id="MSearchBox" class="MSearchBoxInactive">
        <span class="left">
          <img id="MSearchSelect" src="search/mag_sel.png"
               onmouseover="return searchBox.OnSearchSelectShow()"
               onmouseout="return searchBox.OnSearchSelectHide()"
               alt=""/>
          <input type="text" id="MSearchField" value="Search" accesskey="S"
               onfocus="searchBox.OnSearchFieldFocus(true)" 
               onblur="searchBox.OnSearchFieldFocus(false)" 
               onkeyup="searchBox.OnSearchFieldChange(event)"/>
          </span><span class="right">
            <a id="MSearchClose" href="javascript:searchBox.CloseResultsWindow()"><img id="MSearchCloseImg" border="0" src="search/close.png" alt=""/></a>
          </span>
        </div>
      </li>
    </ul>
  </div>
</div>
<div class="header">
  <div class="headertitle">
<div class="title">Making queries </div>  </div>
</div><!--header-->
<div class="contents">
<div class="textblock"><p>The <a class="el" href="classQDjango.html" title="The QDjango class provides a set of static functions.">QDjango</a> object relational mapper (ORM) supports the concept of querysets, borrowed from django's ORM.</p>
<p>A queryset is a collection of database objects which match a certain number of user-specified conditions.</p>
<p>You can learn more about querysets by reading the <a class="el" href="classQDjangoQuerySet.html" title="The QDjangoQuerySet class is a template class for performing database queries.">QDjangoQuerySet</a> template class documentation.</p>
<h2><a class="anchor" id="creating-queries"></a>
Creating and filtering querysets</h2>
<p>Before you can start using querysets, you need to declare your database models as described in <a class="el" href="models.html">Database models</a>.</p>
<p>The most basic queryset matches all the objects for a given model.</p>
<div class="fragment"><pre class="fragment"><span class="comment">// all users</span>
<a class="code" href="classQDjangoQuerySet.html" title="The QDjangoQuerySet class is a template class for performing database queries.">QDjangoQuerySet&lt;User&gt;</a> users;
</pre></div><p>You can use the <a class="el" href="classQDjangoQuerySet.html#a5fc458cda1b34cad3d9f4f99224feae6">QDjangoQuerySet::filter()</a> and <a class="el" href="classQDjangoQuerySet.html#af27220e6645f081348c99ff55a5d5ed6">QDjangoQuerySet::exclude()</a> methods to add filtering conditions to a querset:</p>
<div class="fragment"><pre class="fragment"><span class="comment">// find all users whose password is &quot;foo&quot; and whose username is not &quot;bar&quot;</span>
<a class="code" href="classQDjangoQuerySet.html" title="The QDjangoQuerySet class is a template class for performing database queries.">QDjangoQuerySet&lt;User&gt;</a> someUsers;
someUsers = users.<a class="code" href="classQDjangoQuerySet.html#a5fc458cda1b34cad3d9f4f99224feae6">filter</a>(<a class="code" href="classQDjangoWhere.html" title="The QDjangoWhere class expresses an SQL constraint.">QDjangoWhere</a>(<span class="stringliteral">&quot;password&quot;</span>, <a class="code" href="classQDjangoWhere.html#acd1b7a7d3e2367c6f4846dd9f0ebbd61a7fe62ab85b26467aca41c4b4307a6a45">QDjangoWhere::Equals</a>, <span class="stringliteral">&quot;foo&quot;</span>) &amp;&amp;
                         <a class="code" href="classQDjangoWhere.html" title="The QDjangoWhere class expresses an SQL constraint.">QDjangoWhere</a>(<span class="stringliteral">&quot;username&quot;</span>, <a class="code" href="classQDjangoWhere.html#acd1b7a7d3e2367c6f4846dd9f0ebbd61a1a733f4d536e1a12ca5cfb791e00ed8c">QDjangoWhere::NotEquals</a>, <span class="stringliteral">&quot;bar&quot;</span>));

<span class="comment">// find all users whose username is &quot;foo&quot; or &quot;bar&quot;</span>
someUsers = users.<a class="code" href="classQDjangoQuerySet.html#a5fc458cda1b34cad3d9f4f99224feae6">filter</a>(<a class="code" href="classQDjangoWhere.html" title="The QDjangoWhere class expresses an SQL constraint.">QDjangoWhere</a>(<span class="stringliteral">&quot;username&quot;</span>, <a class="code" href="classQDjangoWhere.html#acd1b7a7d3e2367c6f4846dd9f0ebbd61a7fe62ab85b26467aca41c4b4307a6a45">QDjangoWhere::Equals</a>, <span class="stringliteral">&quot;foo&quot;</span>) ||
                         <a class="code" href="classQDjangoWhere.html" title="The QDjangoWhere class expresses an SQL constraint.">QDjangoWhere</a>(<span class="stringliteral">&quot;username&quot;</span>, <a class="code" href="classQDjangoWhere.html#acd1b7a7d3e2367c6f4846dd9f0ebbd61a7fe62ab85b26467aca41c4b4307a6a45">QDjangoWhere::Equals</a>, <span class="stringliteral">&quot;bar&quot;</span>));

<span class="comment">// find all users whose username starts with &quot;f&quot;:</span>
someUsers = users.<a class="code" href="classQDjangoQuerySet.html#a5fc458cda1b34cad3d9f4f99224feae6">filter</a>(<a class="code" href="classQDjangoWhere.html" title="The QDjangoWhere class expresses an SQL constraint.">QDjangoWhere</a>(<span class="stringliteral">&quot;username&quot;</span>, <a class="code" href="classQDjangoWhere.html#acd1b7a7d3e2367c6f4846dd9f0ebbd61a2c59d066559ebd6ebd37d65f9ac33622">QDjangoWhere::StartsWith</a>, <span class="stringliteral">&quot;f&quot;</span>));
</pre></div><p>You can also use the <a class="el" href="classQDjangoQuerySet.html#af353175373ce8a2087ed42840be58f4c">QDjangoQuerySet::limit()</a> method to limit the number of returned rows:</p>
<div class="fragment"><pre class="fragment"><span class="comment">// limit number of results</span>
someUsers = users.<a class="code" href="classQDjangoQuerySet.html#af353175373ce8a2087ed42840be58f4c">limit</a>(0, 100);
</pre></div><h2><a class="anchor" id="iterating-queries"></a>
Iterating over results</h2>
<p>The easiest way to iterate over results is to use Qt's <a href="http://doc.qt.nokia.com/latest/containers.html#the-foreach-keyword">foreach</a> keyword:</p>
<div class="fragment"><pre class="fragment"><span class="comment">// iterate over matching users</span>
<span class="keywordflow">foreach</span> (<span class="keyword">const</span> User &amp;user, someUsers) {
  qDebug() &lt;&lt; <span class="stringliteral">&quot;found user&quot;</span> &lt;&lt; user.username;   
}
</pre></div><p>Another way of iterating over results is to run over model instances using the <a class="el" href="classQDjangoQuerySet.html#a90a5cd47b75e0cee952461ab574b3f46">QDjangoQuerySet::size()</a> and <a class="el" href="classQDjangoQuerySet.html#a1bcfbffb6676f4ec19f9278c3f0adf4f">QDjangoQuerySet::at()</a> methods:</p>
<div class="fragment"><pre class="fragment"><span class="comment">// iterate over matching users</span>
User user;
<span class="keywordflow">for</span> (<span class="keywordtype">int</span> i = 0; i &lt; someUsers.<a class="code" href="classQDjangoQuerySet.html#a90a5cd47b75e0cee952461ab574b3f46">size</a>(); ++i) {
  <span class="keywordflow">if</span> (someUsers.<a class="code" href="classQDjangoQuerySet.html#a1bcfbffb6676f4ec19f9278c3f0adf4f">at</a>(i, &amp;user)) {
    qDebug() &lt;&lt; <span class="stringliteral">&quot;found user&quot;</span> &lt;&lt; user.username;   
  }
}
</pre></div><p>It is also possible to retrieve field data without creating model instances using the <a class="el" href="classQDjangoQuerySet.html#a9e558d61d83f171a3e627295d544ccb1">QDjangoQuerySet::values()</a> and <a class="el" href="classQDjangoQuerySet.html#a7125a4bf5e722c8af322c1b6e4be05b0">QDjangoQuerySet::valuesList()</a> methods:</p>
<div class="fragment"><pre class="fragment"><span class="comment">// retrieve usernames and passwords for matching users as maps</span>
QList&lt;QVariantMap&gt; propertyMaps = someUsers.<a class="code" href="classQDjangoQuerySet.html#a9e558d61d83f171a3e627295d544ccb1">values</a>(QStringList() &lt;&lt; <span class="stringliteral">&quot;username&quot;</span> &lt;&lt; <span class="stringliteral">&quot;password&quot;</span>);
<span class="keywordflow">foreach</span> (<span class="keyword">const</span> QVariantMap &amp;propertyMap, propertyMaps) {
  qDebug() &lt;&lt; <span class="stringliteral">&quot;username&quot;</span> &lt;&lt; propertyList[<span class="stringliteral">&quot;username&quot;</span>];
  qDebug() &lt;&lt; <span class="stringliteral">&quot;password&quot;</span> &lt;&lt; propertyList[<span class="stringliteral">&quot;password&quot;</span>];
}

<span class="comment">// retrieve usernames and passwords for matching users as lists</span>
QList&lt;QVariantList&gt; propertyLists = someUsers.<a class="code" href="classQDjangoQuerySet.html#a7125a4bf5e722c8af322c1b6e4be05b0">valuesList</a>(QStringList() &lt;&lt; <span class="stringliteral">&quot;username&quot;</span> &lt;&lt; <span class="stringliteral">&quot;password&quot;</span>);
<span class="keywordflow">foreach</span> (<span class="keyword">const</span> QVariantList &amp;propertyList, propertyLists) {
  qDebug() &lt;&lt; <span class="stringliteral">&quot;username&quot;</span> &lt;&lt; propertyList[0];
  qDebug() &lt;&lt; <span class="stringliteral">&quot;password&quot;</span> &lt;&lt; propertyList[1];
}
</pre></div><h2><a class="anchor" id="other-queries"></a>
Other operations</h2>
<div class="fragment"><pre class="fragment"><span class="comment">// count matching users without retrieving their data</span>
<span class="keywordtype">int</span> numberOfUsers = someUsers.<a class="code" href="classQDjangoQuerySet.html#a90bfd4aecf07372b2d68c56b345e12a8">count</a>();

<span class="comment">// delete all the users in the queryset</span>
someUsers.<a class="code" href="classQDjangoQuerySet.html#ad174f57f5b4091aeba43482ac3b0635f">remove</a>();
</pre></div> </div></div><!-- contents -->
<!-- window showing the filter options -->
<div id="MSearchSelectWindow"
     onmouseover="return searchBox.OnSearchSelectShow()"
     onmouseout="return searchBox.OnSearchSelectHide()"
     onkeydown="return searchBox.OnSearchSelectKey(event)">
<a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(0)"><span class="SelectionMark">&#160;</span>All</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(1)"><span class="SelectionMark">&#160;</span>Classes</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(2)"><span class="SelectionMark">&#160;</span>Functions</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(3)"><span class="SelectionMark">&#160;</span>Typedefs</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(4)"><span class="SelectionMark">&#160;</span>Enumerations</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(5)"><span class="SelectionMark">&#160;</span>Enumerator</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(6)"><span class="SelectionMark">&#160;</span>Properties</a></div>

<!-- iframe showing the search results (closed by default) -->
<div id="MSearchResultsWindow">
<iframe src="javascript:void(0)" frameborder="0" 
        name="MSearchResults" id="MSearchResults">
</iframe>
</div>



<hr class="footer"/><address class="footer"><small>
Generated on Wed May 16 2012 18:11:23 for QDjango by &#160;<a href="http://www.doxygen.org/index.html">
<img class="footer" src="doxygen.png" alt="doxygen"/>
</a> 1.7.6.1
</small></address>

</body>
</html>