<html><head><meta http-equiv="Content-Type" content="text/html; charset=us-ascii"><meta name="Generator" content="Microsoft Word 14 (filtered medium)"><style><!--
/* Font Definitions */
@font-face
{font-family:Calibri;
panose-1:2 15 5 2 2 2 4 3 2 4;}
@font-face
{font-family:Tahoma;
panose-1:2 11 6 4 3 5 4 4 2 4;}
@font-face
{font-family:Consolas;
panose-1:2 11 6 9 2 2 4 3 2 4;}
/* Style Definitions */
p.MsoNormal, li.MsoNormal, div.MsoNormal
{margin:0cm;
margin-bottom:.0001pt;
font-size:12.0pt;
font-family:"Times New Roman","serif";}
a:link, span.MsoHyperlink
{mso-style-priority:99;
color:blue;
text-decoration:underline;}
a:visited, span.MsoHyperlinkFollowed
{mso-style-priority:99;
color:purple;
text-decoration:underline;}
code
{mso-style-priority:99;
font-family:"Courier New";}
pre
{mso-style-priority:99;
mso-style-link:"HTML Preformatted Char";
margin:0cm;
margin-bottom:.0001pt;
font-size:10.0pt;
font-family:"Courier New";}
span.HTMLPreformattedChar
{mso-style-name:"HTML Preformatted Char";
mso-style-priority:99;
mso-style-link:"HTML Preformatted";
font-family:Consolas;
mso-fareast-language:EN-CA;}
span.pln
{mso-style-name:pln;}
span.pun
{mso-style-name:pun;}
span.str
{mso-style-name:str;}
span.EmailStyle23
{mso-style-type:personal-reply;
font-family:"Calibri","sans-serif";
color:#1F497D;}
.MsoChpDefault
{mso-style-type:export-only;
font-family:"Calibri","sans-serif";
mso-fareast-language:EN-US;}
@page WordSection1
{size:612.0pt 792.0pt;
margin:72.0pt 72.0pt 72.0pt 72.0pt;}
div.WordSection1
{page:WordSection1;}
--></style></head><body lang="EN-CA" link="blue" vlink="purple"><div class="WordSection1"><p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Courier New";color:#1f497d">while(query.next()):</span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Courier New";color:#1f497d"> idString=str(query.value(0).toString()) </span></p><p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Courier New";color:#1f497d"> passwordString=str(query.value(1).toString())</span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Courier New";color:#1f497d"> </span></p><p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1f497d">query.value() returns a QVariant, so you use .toString() to convert it to a QString, then use str to convert the QString to a python string.</span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1f497d"> </span></p><p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1f497d">Hope this helps.</span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1f497d"> </span></p><p class="MsoNormal"><b><span lang="EN-US" style="font-size:10.0pt;font-family:"Tahoma","sans-serif"">From:</span></b><span lang="EN-US" style="font-size:10.0pt;font-family:"Tahoma","sans-serif""> <a href="mailto:pyqt-bounces@riverbankcomputing.com">pyqt-bounces@riverbankcomputing.com</a> [mailto:<a href="mailto:pyqt-bounces@riverbankcomputing.com">pyqt-bounces@riverbankcomputing.com</a>] <b>On Behalf Of </b>Panupat Chongstitwattana<br>
<b>Sent:</b> May-03-12 1:14 AM<br><b>To:</b> <a href="mailto:PyQt@riverbankcomputing.com">PyQt@riverbankcomputing.com</a><br><b>Subject:</b> [PyQt] returning SQL result as ordinary python string?</span></p><p class="MsoNormal">
</p><p class="MsoNormal" style="margin-bottom:12.0pt">Most example I was able to find were putting sql data into PyQt list/table widgets. But I wasn't able to find example to do basic query. Can anyone help give me some basic please?<br>
<br>For example, if I query selecting from a table with 3 records</p><pre><span class="pln"> query </span><span class="pun">=</span><span class="pln"> QtSql</span><span class="pun">.</span><span class="pln">QSqlQuery</span><span class="pun">(</span><span class="pln">db</span><span class="pun">)</span><br>
<br><span class="pln"></span></pre><pre><span class="pln"> query</span><span class="pun">.</span><span class="pln">exec_</span><span class="pun">(</span><span class="str">"SELECT id, password FROM user"</span><span class="pun">)</span></pre>
<p class="MsoNormal">How do I access each record and return them as ordinary Python strings and not QString?</p></div></body></html>