<?xml version="1.0" encoding="UTF-8" ?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/">
    <channel>
        <title>CUBRID Python Tutorial - Part 2</title>
        <link>http://www.cubrid.org/?mid=cubrid_python_tutorial_part2</link>
        <description>CUBRID Python Tutorial - Part 2</description>
        <language>en</language>
        <pubDate>Sat, 16 Feb 2013 01:50:30 -0800</pubDate>
        <lastBuildDate>Sat, 16 Feb 2013 02:31:05 -0800</lastBuildDate>
        <generator>XpressEngine 1.4.4.1</generator>
                        										        <item>
            <title>CUBRID Python T...</title>
            <dc:creator>CUBRID</dc:creator>
            <link>http://www.cubrid.org/cubrid_python_tutorial_part2</link>
            <guid isPermaLink="true">http://www.cubrid.org/cubrid_python_tutorial_part2</guid>
                                    <description><![CDATA[<p class="MsoNormal"><b><span style="font-size:20.0pt;mso-ansi-language:EN-US">CUBRID Python Tutorial
– Part II</span></b></p>

<p class="MsoNormal"><a href="/cubrid_python_tutorial_part1" target="_self">&lt;&lt; CUBRID Python Tutorial - Part I</a></p>

<p class="MsoNormal">In the first part of this tutorial we went through the steps needed to connect
to a CUBRID database and to run some basic SQL statements from&nbsp;<a href="http://python.org/" target="_blank">Python</a>. Let’s see now how we can make
use of some more advanced features of the Python driver API:</p>

<p class="MsoNormal"><o:p></o:p></p>

<ol>
<li>Insert and query a LOB object</li>
<li>Retrieve database schema information</li>
<li>Retrieve the last insert id</li>
</ol>

<p class="MsoNormal"><span style="font-size:16.0pt;
color:#3399FF;mso-ansi-language:EN-US"><br /></p>

<p class="MsoNormal"><span style="font-size:16.0pt;
color:#3399FF;mso-ansi-language:EN-US">Handling LOB objects<o:p></o:p></p>

<p class="MsoNormal">First, we will show you how to insert and retrieve a CLOB object data. But
before, let’s create a table with a CLOB column:</p>

<p class="MsoNormal"></p>

<div editor_component="code_highlighter" code_type="Bash" title="" first_line="1" collapse="false" highlight="" nogutter="false" style="border:#666 1px dotted;border-left:#2AE 5px solid;padding:5px;background:#FAFAFA url('./modules/editor/components/code_highlighter/code.png') no-repeat top right;">csql&nbsp;-u&nbsp;dba&nbsp;-c&nbsp;"CREATE&nbsp;TABLE&nbsp;test_cubrid(content&nbsp;CLOB);"&nbsp;demodb</div>

<table class="MsoNormalTable" border="1" cellspacing="0" cellpadding="0" style="border: none;">
 <tbody><tr>
  <td width="590" valign="top" style="width:6.15in;border:solid windowtext 1.0pt;
  mso-border-alt:solid windowtext .5pt;background:#FDE9D9;padding:0in 5.4pt 0in 5.4pt">
  <p class="MsoNormal" align="center" style="text-align:center">You can read more about CUBRID LOB data types
  here: <a href="/manual/90/en/BLOB%7CCLOB">http://www.cubrid.org/manual/90/en/BLOB|CLOB</a></p>

</tr></tbody></table>

<p class="MsoNormal">Now that we have the table setup in the database, let’s see how we can perform
an INSERT. The sequence to be performed is:<o:p></o:p></p>

<ol>
<li>Connect to the database</li>
<li>Obtain a cursor from the connection object</li>
<li>Prepare an INSERT statement</li>
<li>Obtain a LOB object</li>
<li>Get some data to insert into the CLOB column (the <b>'cubrid.log'</b> file content, for example)</li>
<li>Bind data to the INSERT statement</li>
<li>Execute  the INSERT command.</li>
</ol>

<p class="MsoNormal">And here is the code:<o:p></o:p></p>

<p class="MsoNormal"><i><span style="color:#1F497D;mso-ansi-language:EN-US"></span></i></p>

<div editor_component="code_highlighter" code_type="Python" title="" first_line="1" collapse="false" highlight="" nogutter="false" style="border:#666 1px dotted;border-left:#2AE 5px solid;padding:5px;background:#FAFAFA url('./modules/editor/components/code_highlighter/code.png') no-repeat top right;"><i>import&nbsp;_cubrid<br />
from&nbsp;_cubrid&nbsp;import&nbsp;*<br />
<br />
conn&nbsp;=&nbsp;_cubrid.connect('CUBRID:localhost:30000:demodb:::',&nbsp;'dba','')<br />
cur&nbsp;=&nbsp;conn.cursor()<br />
cur.prepare('insert&nbsp;into&nbsp;test_cubrid&nbsp;values&nbsp;(?)')<br />
lob&nbsp;=&nbsp;conn.lob()<br />
lob.imports('cubrid.log',&nbsp;'C')<br />
cur.bind_lob(1,&nbsp;lob)<br />
cur.execute()<br />
<br />
lob.close()<br />
cur.close()<br />
conn.close()</i></div>

<p class="MsoNormal">Let’s see next how we retrieve CLOB
data from the table.<b> </b>The procedure
is similar to the one above. Finally, we will write the data we retrieved to
the <b>‘cubrid_out.log’ </b>file, and
compare the file with the initial one<b>:</b>&nbsp;</p>

<div editor_component="code_highlighter" code_type="Python" title="" first_line="1" collapse="false" highlight="" nogutter="false" style="border:#666 1px dotted;border-left:#2AE 5px solid;padding:5px;background:#FAFAFA url('./modules/editor/components/code_highlighter/code.png') no-repeat top right;">conn&nbsp;=&nbsp;_cubrid.connect('CUBRID:localhost:30000:demodb:::',&nbsp;'dba','')<br />
cur&nbsp;=&nbsp;conn.cursor()<br />
cur.prepare('select&nbsp;*&nbsp;from&nbsp;test_cubrid')<br />
cur.execute()<br />
lob_fetch&nbsp;=&nbsp;conn.lob()<br />
cur.fetch_lob(1,&nbsp;lob_fetch)<br />
lob_fetch.export('cubrid_out.log')<br />
<br />
lob_fetch.close()<br />
cur.close()<br />
conn.close()</div>

<p></p>

<p class="MsoNormal">Let’s run the code and compare the
two files.</p>

<p class="MsoNormal"><o:p></o:p></p>

<p class="MsoNormal"></p>

<div editor_component="code_highlighter" code_type="Bash" title="" first_line="1" collapse="false" highlight="" nogutter="false" style="border:#666 1px dotted;border-left:#2AE 5px solid;padding:5px;background:#FAFAFA url('./modules/editor/components/code_highlighter/code.png') no-repeat top right;">Insert&nbsp;done.<br />
Select&nbsp;done.<br />
ovidiu@ovidiu-mint&nbsp;~&nbsp;$&nbsp;diff&nbsp;-s&nbsp;cubrid.log&nbsp;cubrid_out.log</div>

<p class="MsoNormal">Files cubrid.log and cubrid_out.log are identical.<span style="text-indent: 0.5in; text-align: center;">&nbsp;</span></p>

<p class="MsoNormal"><span style="color: rgb(51, 153, 255); font-size: 16pt;"><br /></span></p>

<p class="MsoNormal"><span style="color: rgb(51, 153, 255); font-size: 16pt;">Retrieving schema information</span></p>

<p class="MsoNormal">In the next example, we will see how we can retrieve metadata information
about the tables in the database.</p>

<p class="MsoNormal"><o:p></o:p></p>

<p class="MsoNormal">First we will select the user defined tables from the database and then
retrieve the schema information for each of the tables:</p>

<p class="MsoNormal"></p>

<div editor_component="code_highlighter" code_type="Python" title="" first_line="1" collapse="false" highlight="" nogutter="false" style="border:#666 1px dotted;border-left:#2AE 5px solid;padding:5px;background:#FAFAFA url('./modules/editor/components/code_highlighter/code.png') no-repeat top right;">import&nbsp;_cubrid<br />
from&nbsp;_cubrid&nbsp;import&nbsp;*<br />
<br />
conn&nbsp;=&nbsp;_cubrid.connect('CUBRID:localhost:30000:demodb:::',&nbsp;'dba','')<br />
cur&nbsp;=&nbsp;conn.cursor()<br />
cur.prepare("SELECT&nbsp;class_name&nbsp;FROM&nbsp;db_class&nbsp;where&nbsp;is_system_class&nbsp;=&nbsp;'NO'")<br />
cur.execute()<br />
row&nbsp;=&nbsp;cur.fetch_row()<br />
print&nbsp;'_________________________________'<br />
print&nbsp;'Table&nbsp;name&#92;tTable&nbsp;type'<br />
print&nbsp;'_________________________________'<br />
<br />
while&nbsp;row:<br />
&nbsp;&nbsp;&nbsp;&nbsp;schema_info&nbsp;=&nbsp;conn.schema_info(CUBRID_SCH_TABLE,&nbsp;row[0])<br />
&nbsp;&nbsp;&nbsp;&nbsp;print&nbsp;schema_info[0]&nbsp;+&nbsp;'&#92;t&#92;t'&nbsp;+&nbsp;(schema_info[1]&nbsp;==&nbsp;2&nbsp;and&nbsp;‘User&nbsp;table’&nbsp;and&nbsp;‘Unknown&nbsp;type’)<br />
&nbsp;&nbsp;&nbsp;&nbsp;row&nbsp;=&nbsp;cur.fetch_row()<br />
<br />
cur.close()<br />
conn.close()<span style="line-height: 25px;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;</span></div>

<p class="MsoNormal"><o:p></o:p></p>

<p class="MsoNormal">As you can see, the script is quite simple. When we have the list of the
user tables we iterate through it and call the <i>schema_info</i>. The first parameter is the type of schema requested,
and the second one is a string used to filter the schema information that is
requested (in our case, the name of the tables are the filter). The returned
value is an array containing the schema information. <o:p></o:p></p>

<p class="MsoNormal">In particular, the result of executing the above code is:</p>

<p class="MsoNormal"></p>

<div editor_component="code_highlighter" code_type="Bash" title="" first_line="1" collapse="false" highlight="" nogutter="false" style="border:#666 1px dotted;border-left:#2AE 5px solid;padding:5px;background:#FAFAFA url('./modules/editor/components/code_highlighter/code.png') no-repeat top right;">_________________________________<br />
Table&nbsp;name&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Table&nbsp;type<br />
_________________________________<br />
stadium&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;User&nbsp;table<br />
code&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;User&nbsp;table<br />
nation&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;User&nbsp;table<br />
event&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;User&nbsp;table<br />
athlete&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;User&nbsp;table<br />
participant&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;User&nbsp;table<br />
olympic&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;User&nbsp;table<br />
game&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;User&nbsp;table<br />
record&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;User&nbsp;table<br />
history&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;User&nbsp;table</div>&nbsp;<p></p>

<table class="MsoNormalTable" border="1" cellspacing="0" cellpadding="0" style="border: none;">
 <tbody><tr>
  <td width="590" valign="top" style="width:6.15in;border:solid windowtext 1.0pt;
  mso-border-alt:solid windowtext .5pt;background:#FDE9D9;padding:0in 5.4pt 0in 5.4pt">
  <p class="MsoNormal">Using <i>schema_info</i> you can retrieve all the
  database schema information needed: list of objects, foreign keys, indexes
  and so on.<o:p></o:p></p>
  <p class="MsoNormal">You can find here
  all the details: <a href="http://packages.python.org/CUBRID-Python/">http://packages.python.org/CUBRID-Python/</a>.<o:p></o:p></p>
  
 </tr>
</tbody></table>

<p class="MsoNormal">&nbsp;</p>

<p class="MsoNormal"><span style="font-size:16.0pt;
color:#3399FF;mso-ansi-language:EN-US">Retrieving the Last Insert ID</p>

<p class="MsoNormal"><span style="text-align: center;">Finally, we will show how to retrieve
the </span><b style="text-align: center;">last insert id</b><span style="text-align: center;"> (if you are not
completely familiar with the concept – please read more here: </span><a href="/manual/90/en/LAST_INSERT_ID%20Function" style="text-align: center;">http://www.cubrid.org/manual/90/en/LAST_INSERT_ID%20Function</a><span style="text-align: center;">).</span></p>

<p class="MsoNormal"><o:p></o:p></p>

<p class="MsoNormal">The first thing we will
do is create a test table with the <i>auto_increment</i>
property set on the <i>id</i> column:<o:p></o:p></p>

<div editor_component="code_highlighter" code_type="Python" title="" first_line="1" collapse="false" highlight="" nogutter="false" style="border:#666 1px dotted;border-left:#2AE 5px solid;padding:5px;background:#FAFAFA url('./modules/editor/components/code_highlighter/code.png') no-repeat top right;">t_insert_id&nbsp;=&nbsp;'create&nbsp;table&nbsp;test_insertid&nbsp;(id&nbsp;numeric&nbsp;auto_increment(9999,&nbsp;2),&nbsp;name&nbsp;varchar)'<br />
cur&nbsp;=&nbsp;conn.cursor()<br />
cur.prepare(t_insert_id)<br />
cur.execute()</div>

<p class="MsoNormal">The next step is to insert some data:</p>

<p class="MsoNormal"><o:p></o:p></p>

<p class="MsoNormal"><o:p></o:p></p>

<div editor_component="code_highlighter" code_type="Python" title="" first_line="1" collapse="false" highlight="" nogutter="false" style="border:#666 1px dotted;border-left:#2AE 5px solid;padding:5px;background:#FAFAFA url('./modules/editor/components/code_highlighter/code.png') no-repeat top right;">cur.prepare("insert&nbsp;into&nbsp;test_insertid(name)&nbsp;values&nbsp;('Blair')")<br />
cur.execute()<br />
conn.commit()</div>

<p class="MsoNormal">In the end, we call the<i> insert_id</i>
method of the connection object:</p>

<p class="MsoNormal"><o:p></o:p></p>

<div editor_component="code_highlighter" code_type="Python" title="" first_line="1" collapse="false" highlight="" nogutter="false" style="border:#666 1px dotted;border-left:#2AE 5px solid;padding:5px;background:#FAFAFA url('./modules/editor/components/code_highlighter/code.png') no-repeat top right;">insert_id&nbsp;=&nbsp;conn.insert_id()<br />
print&nbsp;'Inserted&nbsp;id&nbsp;is:&nbsp;'&nbsp;+&nbsp;insert_id<br />
cur.close()<br />
conn.close()</div>

<p class="MsoNormal">And let’s now insert another row in the table and see again the last
insert id:</p>

<p class="MsoNormal"><o:p></o:p></p>

<p class="MsoNormal"></p>

<div editor_component="code_highlighter" code_type="Python" title="" first_line="1" collapse="false" highlight="" nogutter="false" style="border:#666 1px dotted;border-left:#2AE 5px solid;padding:5px;background:#FAFAFA url('./modules/editor/components/code_highlighter/code.png') no-repeat top right;">cur.prepare("insert&nbsp;into&nbsp;test_insertid(name)&nbsp;values&nbsp;('John')")<br />
cur.execute()<br />
conn.commit()<br />
insert_id&nbsp;=&nbsp;conn.insert_id()<br />
print&nbsp;'Inserted&nbsp;id&nbsp;is:&nbsp;'&nbsp;+&nbsp;insert_id</div>&nbsp;<p></p>

<table class="MsoNormalTable" border="1" cellspacing="0" cellpadding="0" style="border: none;">
 <tbody><tr>
  <td width="590" valign="top" style="width:6.15in;border:solid windowtext 1.0pt;
  mso-border-alt:solid windowtext .5pt;background:#FDE9D9;padding:0in 5.4pt 0in 5.4pt">
  <p class="MsoNormal">As you probably
  know, in CUBRID you can insert more than one row in the same INSERT statement
  - <a href="/manual/90/en/INSERT-Overview">http://www.cubrid.org/manual/90/en/INSERT-Overview</a>.<o:p></o:p></p>
  <p class="MsoNormal">Let’s see how it
  works in Python driver…<o:p></o:p></p>
  
 </tr>
</tbody></table>

<div editor_component="code_highlighter" code_type="Python" title="" first_line="1" collapse="false" highlight="" nogutter="false" style="border:#666 1px dotted;border-left:#2AE 5px solid;padding:5px;background:#FAFAFA url('./modules/editor/components/code_highlighter/code.png') no-repeat top right;">conn&nbsp;=&nbsp;_cubrid.connect('CUBRID:localhost:30000:demodb:::',&nbsp;'dba','')<br />
cur&nbsp;=&nbsp;conn.cursor()<br />
cur.prepare("insert&nbsp;into&nbsp;test_insertid(name)&nbsp;values&nbsp;('Anna'),&nbsp;(‘Bety’)")<br />
cur.execute()<br />
conn.commit()<br />
insert_id&nbsp;=&nbsp;conn.insert_id()<br />
print&nbsp;'Inserted&nbsp;id&nbsp;is:&nbsp;'&nbsp;+&nbsp;insert_id<br />
cur.close()<br />
conn.close()</div>

<p class="MsoNormal"></p>

<div editor_component="code_highlighter" code_type="Bash" title="" first_line="1" collapse="false" highlight="" nogutter="false" style="border:#666 1px dotted;border-left:#2AE 5px solid;padding:5px;background:#FAFAFA url('./modules/editor/components/code_highlighter/code.png') no-repeat top right;">After&nbsp;the&nbsp;first&nbsp;insert<br />
Inserted&nbsp;id&nbsp;is:&nbsp;9999<br />
After&nbsp;the&nbsp;second&nbsp;insert<br />
Inserted&nbsp;id&nbsp;is:&nbsp;10001<br />
After&nbsp;the&nbsp;multiple&nbsp;insert<br />
Inserted&nbsp;id&nbsp;is:&nbsp;10003</div>

<p class="MsoNormal">As you can see, only the last insert id for the last row will be
returned. This is in synch with the LAST_INSERT_ID() function specification: <a href="/manual/90/en/LAST_INSERT_ID%20Function">http://www.cubrid.org/manual/90/en/LAST_INSERT_ID%20Function</a>.</p>

<p class="MsoNormal"><o:p></o:p></p>

<p class="MsoNormal">…But what about when a table does not have an <i>auto_increment</i> column? …What will be the value of the last insert
id function…? Let’s see:</p>

<div editor_component="code_highlighter" code_type="Python" title="" first_line="1" collapse="false" highlight="" nogutter="false" style="border:#666 1px dotted;border-left:#2AE 5px solid;padding:5px;background:#FAFAFA url('./modules/editor/components/code_highlighter/code.png') no-repeat top right;">import&nbsp;_cubrid<br />
from&nbsp;_cubrid&nbsp;import&nbsp;*<br />
<br />
conn&nbsp;=&nbsp;_cubrid.connect('CUBRID:localhost:30000:demodb:::',&nbsp;'dba','')<br />
t_insert_id&nbsp;=&nbsp;'create&nbsp;table&nbsp;test_insertid2(id&nbsp;numeric,&nbsp;name&nbsp;varchar)'<br />
cur&nbsp;=&nbsp;conn.cursor()<br />
cur.prepare(t_insert_id)<br />
cur.execute()<br />
cur.prepare("insert&nbsp;into&nbsp;test_insertid2&nbsp;values(1,&nbsp;'Blair')")<br />
cur.execute()<br />
conn.commit()<br />
<br />
insert_id&nbsp;=&nbsp;&nbsp;conn.insert_id()<br />
print&nbsp;'Insert&nbsp;id&nbsp;is:&nbsp;'&nbsp;+&nbsp;(insert_id&nbsp;==&nbsp;None&nbsp;and&nbsp;'No&nbsp;insert&nbsp;id'&nbsp;or&nbsp;insert_id)<br />
<br />
cur.close()<br />
conn.close()</div>

<p></p>

<p class="MsoNormal">The output of the above code is:</p>

<p class="MsoNormal"><o:p></o:p></p>

<p class="MsoNormal"></p>

<div editor_component="code_highlighter" code_type="Bash" title="" first_line="1" collapse="false" highlight="" nogutter="false" style="border:#666 1px dotted;border-left:#2AE 5px solid;padding:5px;background:#FAFAFA url('./modules/editor/components/code_highlighter/code.png') no-repeat top right;">Insert&nbsp;id&nbsp;is:&nbsp;No&nbsp;insert&nbsp;id</div>

<p class="MsoNormal">As expected, there is no “valid” last insert id returned (as in Python <i>None</i> is the equivalent of <i>NULL</i>).</p>

<p class="MsoNormal"><o:p></o:p></p>

<p class="MsoNormal">This concludes our Python tutorial! We hope you enjoyed it and we wish
you great Python programming with CUBRID! <span style="font-family: Wingdings;">J</span>&nbsp;</p>

<p class="MsoNormal"><span style="font-size:16.0pt;
color:#3399FF;mso-ansi-language:EN-US"><br /></p>

<p class="MsoNormal"><span style="font-size:16.0pt;
color:#3399FF;mso-ansi-language:EN-US">Getting Help</p>

<p class="MsoNormal"><span style="text-indent: 0.5in;">At any time, if you feel lost somewhere, visit the main page
for&nbsp;</span><a href="/python_programming" style="text-indent: 0.5in;">Python Programming</a><span style="text-indent: 0.5in;">&nbsp;to
get fast on-track. Also, if you have any troubles using CUBRID with Python, please
post your questions to the&nbsp;</span><a href="/?mid=forum&amp;category=195535&amp;act=dispForumContent" style="text-indent: 0.5in;">Python
Forum</a><span style="text-indent: 0.5in;">.</span></p>

<p class="MsoNormal">Thank you!</p>

<p class="MsoNormal"><a href="/cubrid_python_tutorial_part1" target="_self">&lt;&lt; CUBRID Python Tutorial - Part I</a></p>]]></description>
                        <pubDate>Sat, 16 Feb 2013 01:23:48 -0800</pubDate>
                        <category>python</category>
                        <category>tutorial</category>
                        <category>driver</category>
                                </item>
            </channel>
</rss>
