CUBRID Python Tutorial – Part II
<< CUBRID Python Tutorial - Part I
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 Python. Let’s see now how we can make use of some more advanced features of the Python driver API:
- Insert and query a LOB object
- Retrieve database schema information
- Retrieve the last insert id
Handling LOB objects
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:
csql -u dba -c "CREATE TABLE test_cubrid(content CLOB);" demodb
|
You can read more about CUBRID LOB data types here: http://www.cubrid.org/manual/90/en/BLOB|CLOB |
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:
- Connect to the database
- Obtain a cursor from the connection object
- Prepare an INSERT statement
- Obtain a LOB object
- Get some data to insert into the CLOB column (the 'cubrid.log' file content, for example)
- Bind data to the INSERT statement
- Execute the INSERT command.
And here is the code:
import _cubrid
from _cubrid import *
conn = _cubrid.connect('CUBRID:localhost:30000:demodb:::', 'dba','')
cur = conn.cursor()
cur.prepare('insert into test_cubrid values (?)')
lob = conn.lob()
lob.imports('cubrid.log', 'C')
cur.bind_lob(1, lob)
cur.execute()
lob.close()
cur.close()
conn.close()
Let’s see next how we retrieve CLOB data from the table. The procedure is similar to the one above. Finally, we will write the data we retrieved to the ‘cubrid_out.log’ file, and compare the file with the initial one:
conn = _cubrid.connect('CUBRID:localhost:30000:demodb:::', 'dba','')
cur = conn.cursor()
cur.prepare('select * from test_cubrid')
cur.execute()
lob_fetch = conn.lob()
cur.fetch_lob(1, lob_fetch)
lob_fetch.export('cubrid_out.log')
lob_fetch.close()
cur.close()
conn.close()
Let’s run the code and compare the two files.
Insert done. Select done. ovidiu@ovidiu-mint ~ $ diff -s cubrid.log cubrid_out.log
Files cubrid.log and cubrid_out.log are identical.
Retrieving schema information
In the next example, we will see how we can retrieve metadata information about the tables in the database.
First we will select the user defined tables from the database and then retrieve the schema information for each of the tables:
import _cubrid
from _cubrid import *
conn = _cubrid.connect('CUBRID:localhost:30000:demodb:::', 'dba','')
cur = conn.cursor()
cur.prepare("SELECT class_name FROM db_class where is_system_class = 'NO'")
cur.execute()
row = cur.fetch_row()
print '_________________________________'
print 'Table name\tTable type'
print '_________________________________'
while row:
schema_info = conn.schema_info(CUBRID_SCH_TABLE, row[0])
print schema_info[0] + '\t\t' + (schema_info[1] == 2 and ‘User table’ and ‘Unknown type’)
row = cur.fetch_row()
cur.close()
conn.close()
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 schema_info. 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.
In particular, the result of executing the above code is:
_________________________________ Table name Table type _________________________________ stadium User table code User table nation User table event User table athlete User table participant User table olympic User table game User table record User table history User table
|
Using schema_info you can retrieve all the
database schema information needed: list of objects, foreign keys, indexes
and so on. You can find here
all the details: http://packages.python.org/CUBRID-Python/. |
Retrieving the Last Insert ID
Finally, we will show how to retrieve the last insert id (if you are not completely familiar with the concept – please read more here: http://www.cubrid.org/manual/90/en/LAST_INSERT_ID%20Function).
The first thing we will
do is create a test table with the auto_increment
property set on the id column:
t_insert_id = 'create table test_insertid (id numeric auto_increment(9999, 2), name varchar)' cur = conn.cursor() cur.prepare(t_insert_id) cur.execute()
The next step is to insert some data:
cur.prepare("insert into test_insertid(name) values ('Blair')")
cur.execute()
conn.commit()
In the end, we call the insert_id method of the connection object:
insert_id = conn.insert_id() print 'Inserted id is: ' + insert_id cur.close() conn.close()
And let’s now insert another row in the table and see again the last insert id:
cur.prepare("insert into test_insertid(name) values ('John')")
cur.execute()
conn.commit()
insert_id = conn.insert_id()
print 'Inserted id is: ' + insert_id
|
As you probably
know, in CUBRID you can insert more than one row in the same INSERT statement
- http://www.cubrid.org/manual/90/en/INSERT-Overview. Let’s see how it
works in Python driver… |
conn = _cubrid.connect('CUBRID:localhost:30000:demodb:::', 'dba','')
cur = conn.cursor()
cur.prepare("insert into test_insertid(name) values ('Anna'), (‘Bety’)")
cur.execute()
conn.commit()
insert_id = conn.insert_id()
print 'Inserted id is: ' + insert_id
cur.close()
conn.close()
After the first insert Inserted id is: 9999 After the second insert Inserted id is: 10001 After the multiple insert Inserted id is: 10003
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: http://www.cubrid.org/manual/90/en/LAST_INSERT_ID%20Function.
…But what about when a table does not have an auto_increment column? …What will be the value of the last insert id function…? Let’s see:
import _cubrid
from _cubrid import *
conn = _cubrid.connect('CUBRID:localhost:30000:demodb:::', 'dba','')
t_insert_id = 'create table test_insertid2(id numeric, name varchar)'
cur = conn.cursor()
cur.prepare(t_insert_id)
cur.execute()
cur.prepare("insert into test_insertid2 values(1, 'Blair')")
cur.execute()
conn.commit()
insert_id = conn.insert_id()
print 'Insert id is: ' + (insert_id == None and 'No insert id' or insert_id)
cur.close()
conn.close()
The output of the above code is:
Insert id is: No insert id
As expected, there is no “valid” last insert id returned (as in Python None is the equivalent of NULL).
This concludes our Python tutorial! We hope you enjoyed it and we wish you great Python programming with CUBRID! J
Getting Help
At any time, if you feel lost somewhere, visit the main page for Python Programming to get fast on-track. Also, if you have any troubles using CUBRID with Python, please post your questions to the Python Forum.
Thank you!
