5.2. How to execute a query

Result queries (SELECT statement) return data when they are executed.


Example 5-2. Execute a "SELECT" query

   1 horst@horstnotebook:~> python
   2 Python 2.2.2 (#1, Mar 17 2003, 15:17:58)
   3 [GCC 3.3 20030226 (prerelease) (SuSE Linux)] on linux2
   4 Type "help", "copyright", "credits" or "license" for more information.
   5 >>> from hk_classes import *
   6 d>>> dr=hk_drivermanager()
   7 >>> con=dr.new_connection("mysql")
   8 >>> con.set_password("secret")
   9 >>> con.connect()
  10 1
  11 >>> db=con.new_database("exampledb")
  12 >>> query=db.new_resultquery()
  13 >>> query.set_sql("SELECT * FROM authors")
  14 1
  15 >>> query.enable()
  16 SQL : SELECT  *  FROM  authors
  17 1

Many queries, such as data definition queries, don't return data. They only report whether execution of the query was successful or whether it failed.

Example 5-3. Execute an action query

   1 horst@horstnotebook:~> python
   2 Python 2.2.2 (#1, Mar 17 2003, 15:17:58)
   3 [GCC 3.3 20030226 (prerelease) (SuSE Linux)] on linux2
   4 Type "help", "copyright", "credits" or "license" for more information.
   5 >>> from hk_classes import *
   6 d>>> dr=hk_drivermanager()
   7 >>> con=dr.new_connection("mysql")
   8 >>> con.set_password("secret")
   9 >>> con.connect()
  10 1
  11 >>> db=con.new_database("exampledb")
  12 >>> query=db.new_actionquery()
  13 >>> query.set_sql("CREATE TABLE `another new table` ( `id` BIGINT(1) NOT NULL AUTO_INCREMENT , `name` BIGINT, PRIMARY KEY ( `id` ) )")
  14 >>> query.execute()
  15 CREATE TABLE `another new table` ( `id` BIGINT(1) NOT NULL AUTO_INCREMENT , `name` BIGINT, PRIMARY KEY ( `id` ) )
  16 1