index.htm 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  2. <html xmlns="http://www.w3.org/1999/xhtml">
  3. <head>
  4. <meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/>
  5. <meta http-equiv="X-UA-Compatible" content="IE=9"/>
  6. <meta name="generator" content="Doxygen 1.8.14"/>
  7. <meta name="viewport" content="width=device-width, initial-scale=1"/>
  8. <title>EasyQtSql: EasyQtSql User Manual</title>
  9. <link href="tabs.css" rel="stylesheet" type="text/css"/>
  10. <script type="text/javascript" src="jquery.js"></script>
  11. <script type="text/javascript" src="dynsections.js"></script>
  12. <link href="navtree.css" rel="stylesheet" type="text/css"/>
  13. <script type="text/javascript" src="resize.js"></script>
  14. <script type="text/javascript" src="navtreedata.js"></script>
  15. <script type="text/javascript" src="navtree.js"></script>
  16. <script type="text/javascript">
  17. /* @license magnet:?xt=urn:btih:cf05388f2679ee054f2beb29a391d25f4e673ac3&amp;dn=gpl-2.0.txt GPL-v2 */
  18. $(document).ready(initResizable);
  19. /* @license-end */</script>
  20. <link href="search/search.css" rel="stylesheet" type="text/css"/>
  21. <script type="text/javascript" src="search/searchdata.js"></script>
  22. <script type="text/javascript" src="search/search.js"></script>
  23. <link href="doxygen.css" rel="stylesheet" type="text/css" />
  24. </head>
  25. <body>
  26. <div id="top"><!-- do not remove this div, it is closed by doxygen! -->
  27. <div id="titlearea">
  28. <table cellspacing="0" cellpadding="0">
  29. <tbody>
  30. <tr style="height: 56px;">
  31. <td id="projectalign" style="padding-left: 0.5em;">
  32. <div id="projectname">EasyQtSql
  33. </div>
  34. <div id="projectbrief">Easy SQL data access helper for QtSql</div>
  35. </td>
  36. </tr>
  37. </tbody>
  38. </table>
  39. </div>
  40. <!-- end header part -->
  41. <!-- Generated by Doxygen 1.8.14 -->
  42. <script type="text/javascript">
  43. /* @license magnet:?xt=urn:btih:cf05388f2679ee054f2beb29a391d25f4e673ac3&amp;dn=gpl-2.0.txt GPL-v2 */
  44. var searchBox = new SearchBox("searchBox", "search",false,'Search');
  45. /* @license-end */
  46. </script>
  47. <script type="text/javascript" src="menudata.js"></script>
  48. <script type="text/javascript" src="menu.js"></script>
  49. <script type="text/javascript">
  50. /* @license magnet:?xt=urn:btih:cf05388f2679ee054f2beb29a391d25f4e673ac3&amp;dn=gpl-2.0.txt GPL-v2 */
  51. $(function() {
  52. initMenu('',true,false,'search.php','Search');
  53. $(document).ready(function() { init_search(); });
  54. });
  55. /* @license-end */</script>
  56. <div id="main-nav"></div>
  57. </div><!-- top -->
  58. <div id="side-nav" class="ui-resizable side-nav-resizable">
  59. <div id="nav-tree">
  60. <div id="nav-tree-contents">
  61. <div id="nav-sync" class="sync"></div>
  62. </div>
  63. </div>
  64. <div id="splitbar" style="-moz-user-select:none;"
  65. class="ui-resizable-handle">
  66. </div>
  67. </div>
  68. <script type="text/javascript">
  69. /* @license magnet:?xt=urn:btih:cf05388f2679ee054f2beb29a391d25f4e673ac3&amp;dn=gpl-2.0.txt GPL-v2 */
  70. $(document).ready(function(){initNavTree('index.htm','');});
  71. /* @license-end */
  72. </script>
  73. <div id="doc-content">
  74. <!-- window showing the filter options -->
  75. <div id="MSearchSelectWindow"
  76. onmouseover="return searchBox.OnSearchSelectShow()"
  77. onmouseout="return searchBox.OnSearchSelectHide()"
  78. onkeydown="return searchBox.OnSearchSelectKey(event)">
  79. </div>
  80. <!-- iframe showing the search results (closed by default) -->
  81. <div id="MSearchResultsWindow">
  82. <iframe src="javascript:void(0)" frameborder="0"
  83. name="MSearchResults" id="MSearchResults">
  84. </iframe>
  85. </div>
  86. <div class="header">
  87. <div class="headertitle">
  88. <div class="title"><a class="el" href="namespace_easy_qt_sql.htm" title="Easy SQL data access helper for QtSql. ">EasyQtSql</a> User Manual </div> </div>
  89. </div><!--header-->
  90. <div class="contents">
  91. <div class="textblock"><h1><a class="anchor" id="intro_sec"></a>
  92. Introduction</h1>
  93. <p><a class="el" href="namespace_easy_qt_sql.htm" title="Easy SQL data access helper for QtSql. ">EasyQtSql</a> is a lightweight header-only library for quick and easy SQL querying with QtSql classes.</p>
  94. <h1><a class="anchor" id="install_sec"></a>
  95. Quick Start guide</h1>
  96. <h2><a class="anchor" id="step1"></a>
  97. Step 1: Create Qt application project</h2>
  98. <p>You can use QtCreator to create a simple minimal console application with a single main.cpp file.</p>
  99. <p>Or use a ready-made application where you want to add database support.</p>
  100. <h2><a class="anchor" id="step2"></a>
  101. Step 2: Add QtSql support</h2>
  102. <p>Open *.pro file and add QtSql module (QT += sql):</p>
  103. <div class="fragment"><div class="line">QT += sql</div><div class="line">QT -= gui</div><div class="line"></div><div class="line">CONFIG += c++11</div><div class="line"></div><div class="line">TARGET = App</div><div class="line">CONFIG += console</div><div class="line">CONFIG -= app_bundle</div><div class="line"></div><div class="line">TEMPLATE = app</div></div><!-- fragment --><h2><a class="anchor" id="step3"></a>
  104. Step 3: Include EasyQtSql library</h2>
  105. <p>To use my library you need to include single header file (<a class="el" href="_easy_qt_sql_8h.htm" title="Main EasyQtSql header file. ">EasyQtSql.h</a>) in your source code. So you need to put the library header files (<a class="el" href="namespace_easy_qt_sql.htm" title="Easy SQL data access helper for QtSql. ">EasyQtSql</a> folder) in an accessible place next to your project and to specify path to the <a class="el" href="namespace_easy_qt_sql.htm" title="Easy SQL data access helper for QtSql. ">EasyQtSql</a> folder with INCLUDEPATH directive. You can just include EasyQtSql.pri inside your *.pro file:</p>
  106. <div class="fragment"><div class="line">QT += sql</div><div class="line">QT -= gui</div><div class="line"></div><div class="line">include(../<a class="code" href="namespace_easy_qt_sql.htm">EasyQtSql</a>/<a class="code" href="namespace_easy_qt_sql.htm">EasyQtSql</a>.pri)</div><div class="line"></div><div class="line">CONFIG += c++11</div><div class="line"></div><div class="line">TARGET = App</div><div class="line">CONFIG += console</div><div class="line">CONFIG -= app_bundle</div><div class="line"></div><div class="line">TEMPLATE = app</div><div class="line"></div><div class="line"><span class="preprocessor">#INCLUDEPATH += ../EasyQtSql</span></div><div class="line"></div><div class="line">SOURCES += <a class="code" href="dummy_8cpp.htm#a0ddf1224851353fc92bfbff6f499fa97">main</a>.cpp \
  107. TestObject.cpp</div><div class="line"></div><div class="line">HEADERS += \</div><div class="line"> TestObject.h </div></div><!-- fragment --><h2><a class="anchor" id="step4"></a>
  108. Step 4: Include the library header file and start using it</h2>
  109. <p>Include <a class="el" href="_easy_qt_sql_8h.htm" title="Main EasyQtSql header file. ">EasyQtSql.h</a> header file and start using the library.</p>
  110. <div class="fragment"><div class="line"><span class="preprocessor">#include &lt;QCoreApplication&gt;</span></div><div class="line"><span class="preprocessor">#include &quot;<a class="code" href="_easy_qt_sql_8h.htm">EasyQtSql.h</a>&quot;</span></div><div class="line"><span class="preprocessor">#include &quot;TestObject.h&quot;</span></div><div class="line"></div><div class="line"><span class="keyword">using namespace </span><a class="code" href="namespace_easy_qt_sql.htm">EasyQtSql</a>;</div><div class="line"></div><div class="line"><span class="keywordtype">int</span> <a class="code" href="dummy_8cpp.htm#a0ddf1224851353fc92bfbff6f499fa97">main</a>(<span class="keywordtype">int</span> argc, <span class="keywordtype">char</span> *argv[])</div><div class="line">{</div><div class="line"> QCoreApplication a(argc, argv);</div><div class="line"></div><div class="line"> qDebug() &lt;&lt; <span class="stringliteral">&quot;begin&quot;</span>;</div><div class="line"></div><div class="line"> QSqlDatabase sdb = QSqlDatabase::addDatabase(<span class="stringliteral">&quot;QSQLITE&quot;</span>);</div><div class="line"> sdb.setDatabaseName(<span class="stringliteral">&quot;:memory:&quot;</span>);</div><div class="line"></div><div class="line"> <span class="keywordflow">try</span></div><div class="line"> {</div><div class="line"> <a class="code" href="class_easy_qt_sql_1_1_transaction.htm">Transaction</a> t(sdb);</div><div class="line"></div><div class="line"> t.<a class="code" href="class_database.htm#aae9f7e53275334c91655eeb7a2bde030">execNonQuery</a>(<span class="stringliteral">&quot;CREATE TEMP TABLE IF NOT EXISTS table1 (a int, b int, c int, d text)&quot;</span>);</div><div class="line"></div><div class="line"> <span class="comment">//multi insert</span></div><div class="line"> <a class="code" href="class_easy_qt_sql_1_1_non_query_result.htm">NonQueryResult</a> res1 = t.<a class="code" href="class_database.htm#a8388689590c1f3adeabd2972b221bcb2">insertInto</a>(<span class="stringliteral">&quot;table1 (a, b, c, d)&quot;</span>)</div><div class="line"> .<a class="code" href="class_easy_qt_sql_1_1_insert_query.htm#af7502896bedf2a771aa8732282f1de72">values</a>(1, 2, 3, 7)</div><div class="line"> .<a class="code" href="class_easy_qt_sql_1_1_insert_query.htm#af7502896bedf2a771aa8732282f1de72">values</a>(2, 3, 4, <span class="stringliteral">&quot;test&quot;</span>)</div><div class="line"> .<a class="code" href="class_easy_qt_sql_1_1_insert_query.htm#af7502896bedf2a771aa8732282f1de72">values</a>(2, 3, 4, 8)</div><div class="line"> .<a class="code" href="class_easy_qt_sql_1_1_insert_query.htm#a273196e89e6fa6607cba428bd0a91a71">exec</a>();</div><div class="line"></div><div class="line"> qDebug() &lt;&lt; res1.<a class="code" href="class_easy_qt_sql_1_1_non_query_result.htm#accc70f0c2199629ce062feb8de0c2fe7">lastQuery</a>();</div><div class="line"></div><div class="line"> <span class="comment">//delete with condition</span></div><div class="line"> <span class="comment">//t.deleteFrom(&quot;table1&quot;).where(&quot;a = ? AND b = ?&quot;, 1, 2);</span></div><div class="line"></div><div class="line"> <span class="comment">//delete without condition</span></div><div class="line"> <span class="comment">//t.deleteFrom(&quot;table1&quot;).exec();</span></div><div class="line"></div><div class="line"> <span class="comment">//delete with constant condition</span></div><div class="line"> <span class="comment">//t.deleteFrom(&quot;table1&quot;).where(&quot;1=0&quot;);</span></div><div class="line"> <a class="code" href="class_easy_qt_sql_1_1_prepared_query.htm">PreparedQuery</a> query = t.<a class="code" href="class_database.htm#a03e2c9113452bb52bb7018ef2366bce1">prepare</a>(<span class="stringliteral">&quot;SELECT a, b, c, d FROM table1 WHERE a = ? AND b = ?&quot;</span>);</div><div class="line"></div><div class="line"> <a class="code" href="class_easy_qt_sql_1_1_query_result.htm">QueryResult</a> res = query.<a class="code" href="class_easy_qt_sql_1_1_prepared_query.htm#a8b37cea455273109305cb8e621b169bf">exec</a>(1, 2);</div><div class="line"></div><div class="line"> qDebug() &lt;&lt; res.<a class="code" href="class_easy_qt_sql_1_1_query_result.htm#abe5f540e13e57385fc57c8826b61816e">executedQuery</a>() &lt;&lt; res.<a class="code" href="class_easy_qt_sql_1_1_query_result.htm#a10971ce872fed637dad73c9ffe4681cf">boundValue</a>(0) &lt;&lt; res.<a class="code" href="class_easy_qt_sql_1_1_query_result.htm#a10971ce872fed637dad73c9ffe4681cf">boundValue</a>(1);</div><div class="line"></div><div class="line"> <span class="keywordflow">while</span>(res.<a class="code" href="class_easy_qt_sql_1_1_query_result.htm#a3e1b0790cdb82a848f2b48ddc698001b">next</a>())</div><div class="line"> {</div><div class="line"> QVariantMap map = res.<a class="code" href="class_easy_qt_sql_1_1_query_result.htm#a27809ada6725d479f31b66ebcbc7c95c">toMap</a>();</div><div class="line"></div><div class="line"> qDebug() &lt;&lt; map;</div><div class="line"> }</div><div class="line"></div><div class="line"> res = query.<a class="code" href="class_easy_qt_sql_1_1_prepared_query.htm#a8b37cea455273109305cb8e621b169bf">exec</a>(2, 3);</div><div class="line"></div><div class="line"> qDebug() &lt;&lt; res.<a class="code" href="class_easy_qt_sql_1_1_query_result.htm#abe5f540e13e57385fc57c8826b61816e">executedQuery</a>() &lt;&lt; res.<a class="code" href="class_easy_qt_sql_1_1_query_result.htm#a10971ce872fed637dad73c9ffe4681cf">boundValue</a>(0) &lt;&lt; res.<a class="code" href="class_easy_qt_sql_1_1_query_result.htm#a10971ce872fed637dad73c9ffe4681cf">boundValue</a>(1);</div><div class="line"></div><div class="line"> <span class="keywordflow">while</span>(res.<a class="code" href="class_easy_qt_sql_1_1_query_result.htm#a3e1b0790cdb82a848f2b48ddc698001b">next</a>())</div><div class="line"> {</div><div class="line"> QStringList list = res.<a class="code" href="class_easy_qt_sql_1_1_query_result.htm#a152e0d75c2c149896955c3257ca08e7d">toStringList</a>();</div><div class="line"> qDebug() &lt;&lt; list;</div><div class="line"></div><div class="line"> TestObject obj;</div><div class="line"></div><div class="line"> <span class="keywordtype">int</span> a;</div><div class="line"> <span class="keywordtype">bool</span> b;</div><div class="line"> <span class="keywordtype">double</span> c;</div><div class="line"> QVariant d;</div><div class="line"> QByteArray arr;</div><div class="line"></div><div class="line"> res.<a class="code" href="class_easy_qt_sql_1_1_query_result.htm#ab0dac0809b8eab8f066106391346dfb4">fetchVars</a>(a, b, c, d, arr);</div><div class="line"></div><div class="line"> res.<a class="code" href="class_easy_qt_sql_1_1_query_result.htm#a043b284c72b54c4a2871867196a67560">fetchObject</a>(obj);</div><div class="line"></div><div class="line"> qDebug() &lt;&lt; a &lt;&lt; b &lt;&lt; c &lt;&lt; d;</div><div class="line"> }</div><div class="line"></div><div class="line"> <a class="code" href="class_easy_qt_sql_1_1_non_query_result.htm">NonQueryResult</a> res2 = t.<a class="code" href="class_database.htm#a2d8dad3df4134306749582216bd437e0">update</a>(<span class="stringliteral">&quot;table1&quot;</span>)</div><div class="line"> .<a class="code" href="class_easy_qt_sql_1_1_update_query.htm#a682f1733cdd85e56672a9d200c4bb7b4">set</a>(<span class="stringliteral">&quot;a&quot;</span>, 123)</div><div class="line"> .<a class="code" href="class_easy_qt_sql_1_1_update_query.htm#a682f1733cdd85e56672a9d200c4bb7b4">set</a>(<span class="stringliteral">&quot;b&quot;</span>, 333)</div><div class="line"> .<a class="code" href="class_easy_qt_sql_1_1_update_query.htm#a682f1733cdd85e56672a9d200c4bb7b4">set</a>(QVariantMap{{<span class="stringliteral">&quot;a&quot;</span>, 1}, {<span class="stringliteral">&quot;b&quot;</span>, 2}})</div><div class="line"> .where(<span class="stringliteral">&quot;c=? AND d=?&quot;</span>, 3, 4);</div><div class="line"></div><div class="line"> qDebug() &lt;&lt; res2.<a class="code" href="class_easy_qt_sql_1_1_non_query_result.htm#accc70f0c2199629ce062feb8de0c2fe7">lastQuery</a>() &lt;&lt; res2.<a class="code" href="class_easy_qt_sql_1_1_non_query_result.htm#a4e82dd968f0b371514243e825a5b7f86">executedQuery</a>();</div><div class="line"></div><div class="line"> t.<a class="code" href="class_transaction.htm#a7388031cba0416d48e03e3f7acd5ca6a">commit</a>();</div><div class="line"></div><div class="line"> qDebug() &lt;&lt; <span class="stringliteral">&quot;end&quot;</span>;</div><div class="line"> }</div><div class="line"> <span class="keywordflow">catch</span> (<span class="keyword">const</span> <a class="code" href="class_easy_qt_sql_1_1_d_b_exception.htm">DBException</a> &amp;e)</div><div class="line"> {</div><div class="line"> qDebug() &lt;&lt; e.<a class="code" href="class_easy_qt_sql_1_1_d_b_exception.htm#acae3cc68633e4be58440ec745d51cedb">lastError</a> &lt;&lt; e.<a class="code" href="class_easy_qt_sql_1_1_d_b_exception.htm#afc48e67740faefa6825adf04908d5924">lastQuery</a>;</div><div class="line"> }</div><div class="line"></div><div class="line"> qDebug() &lt;&lt; <span class="stringliteral">&quot;done&quot;</span>;</div><div class="line"></div><div class="line"> <span class="keywordflow">return</span> a.exec();</div><div class="line">}</div></div><!-- fragment --><h1><a class="anchor" id="whatsnext"></a>
  111. What's next?</h1>
  112. <p>You can:</p><ul>
  113. <li>Read the documentation</li>
  114. <li>Explore and run Qt tests</li>
  115. <li>Explore the source code of the library</li>
  116. <li>Use the library in your project</li>
  117. <li>Give me feedback</li>
  118. </ul>
  119. <h1><a class="anchor" id="license"></a>
  120. License</h1>
  121. <pre class="fragment">
  122. The MIT License (MIT)
  123. Copyright 2018 Alexey Kramin
  124. Permission is hereby granted, free of charge, to any person obtaining
  125. a copy of this software and associated documentation files (the
  126. "Software"), to deal in the Software without restriction, including
  127. without limitation the rights to use, copy, modify, merge, publish,
  128. distribute, sublicense, and/or sell copies of the Software, and to
  129. permit persons to whom the Software is furnished to do so, subject to
  130. the following conditions:
  131. The above copyright notice and this permission notice shall be
  132. included in all copies or substantial portions of the Software.
  133. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  134. EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  135. MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  136. NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  137. LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  138. OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  139. WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  140. </pre> </div></div><!-- contents -->
  141. </div><!-- doc-content -->
  142. <!-- start footer part -->
  143. <div id="nav-path" class="navpath"><!-- id is needed for treeview function! -->
  144. <ul>
  145. <li class="footer">Generated by
  146. <a href="http://www.doxygen.org/index.html">
  147. <img class="footer" src="doxygen.png" alt="doxygen"/></a> 1.8.14 </li>
  148. </ul>
  149. </div>
  150. </body>
  151. </html>