# The &knoda; scripting tutorial

## The various actions

Each object in a form has its own programmable actions. General actions are

On click

script that will be executed when a mouse click on this object occurs

On doubleclick

script that will be executed when a mouse doubleclick on this object occurs

On open

script that will be executed when the form is created or put into viewmode

On close

script that will be executed when the form is closed or put into designmode

On getfocus

script that will be executed when this object gets the keyboard focus

On loosefocus

script that will be executed when this object looses the keyboard focus

On key

script that will be executed when this object has the keyboard focus and a key on the keyboard is pressed

Let's write a 'hello world' program. Create a new form and add a button. Select the button and click on the 'On click'-actionbutton in the property editor. Now add the following program into the program editor

### Hello world-program

    hk_this.show_warningmessage("Hello world")

Now close the window and change to viewmode. When you push the button you will see a dialog window with "Hello world".

The global variable hk_this is set from &hk_classes; and represents the current object, in this case the button. show_warningmessage() is a method of this object, that allows you to display a string in a GUI independent way (as &knoda; is a KDE application it will be displayed as a KDE window but if you start it within a command line program it will be displayed on the standard output).

Other GUI-independent ways for user interaction are

show_warningmessage(message)

displays 'message'

bool show_yesnodialog(message, bool default_value)

displays 'message' and returns true if you answer 'yes'. The parameter 'default_value' is used for convenience to preselect 'yes' or 'no' in dialog boxes

hk_string show_stringvaluedialog(text)

displays 'text' as a question and returns your response

In forms another global variable is hk_thisform. It represents the form. In reports the variable name is hk_thisreport.

## General object methods- the class hk_visible

Each visible object of &hk_classes; is inherited from a class called hk_visible, so we will now have a look on the general available methods

### Geometry specific methods

> set_size(x, y,width,height)

> lets you to set position and size of an object

> set_size(width,height)

> lets you to set size of an object

> set_position(x,y)

> lets you to set position of an object

> set_x(x)

> lets you to set the horizontal position of an object

> set_y(y)

> lets you to set the vertical position of an object

> set_width(width)

> set_height(height)

> x()

> returns the x co-ordinate of the object

> y()

> returns the y co-ordinate of the object

> width()

> returns the width of the object

> height()

> returns the height of the object

### Look and Feel methods

> set_font(fontname,size)

> sets the font, e.g. set_font("Arial",12)

> set_font(font)

> hk_font font()

> returns a font object

> set_foregroundcolour(colour)

> foregroundcolour()

> set_backgroundcolour(colour)

> hk_colour backgroundcolour()

### Miscelleanous methods

> set_label(labeltext)

> label()

> enum enum_visibletype {textlabel,button,rowselector,boolean,lineedit,memo,combobox,grid,form,report,reportsection,reportdata,other}

> enum_visibletype type(void)

> identifier()

> hk_presentation* presentation()

> returns the parent presentation object (either a form or a report)

The following example shows how to move the button within the form, how to change the colour and how to display different text on the button.

### Changing colour and position

    redcolour =hk_colour(255,0,0)
    greencolour =hk_colour(0,255,0)
    if hk_this.foregroundcolour().red()!=255:
    	hk_this.set_foregroundcolour(redcolour)
    	hk_this.set_backgroundcolour(greencolour)
    	hk_this.set_label("green button")
    else:
    	hk_this.set_foregroundcolour(greencolour)
    	hk_this.set_backgroundcolour(redcolour)
    	hk_this.set_label("red button")

    hk_this.set_position(hk_this.x()+50,hk_this.y()+10)

### Predefined colour names

> - hk_aqua

> - hk_beige

> - hk_black

> - hk_blue

> - hk_brown

> - hk_fuchsia

> - hk_darkgrey

> - hk_green

> - hk_grey

> - hk_lightgrey

> - hk_lime

> - hk_maroon

> - hk_navy

> - hk_olive

> - hk_orange

> - hk_pink

> - hk_purple

> - hk_red

> - hk_silver

> - hk_teal

> - hk_white

> - hk_yellow

## Data sensitive methods

Before we start writing programs, we will first have some theory. Below you see the (c++)- structure of &hk_classes;. There are classes that handle the contact to the database (see the left side of the graphic), while others handle the interaction with the user (we have already seen the hk_visible class in the previous section).

### hk_classes overview

### description of the most important classes

### Objects of type hk_dsvisible - the class hk_datasource

All data-sensitive objects are of type hk_dsvisible (or its child hk_dsdatavisible). hk_dsvisible inherits from hk_visible. The most important method of hk_dsvisible is datasource(). This method returns a class of type hk_datasource, representing the whole datasource (either a table or a query)

#### hk_datasource methods

> name()

> returns the name of the datasource

> goto_row(rownumber)

> moves the row selector (row cursor) to 'rownumber'

> goto_first()

> goto_last()

> goto_next()

> goto_previous()

> row_position()

> returns the row number of the current row

> max_rows()

> returns the total number of existing rows

> enable()

> disable()

> set_enabled(e)

> is_enabled()

> hk_column*column_by_name(name)

> returns an hk_column object of the column with the name 'name'

> store_changed_data()

> hk_database*database()

The following example shows how to move between rows in a datasource. For this, create a button in the form and set a datasource. Add the script to the "On click"-action.

#### rowposition

    ds=hk_this.datasource()
    ds.goto_row(ds.row_position() + 2)

How to get a specific hk_column object can be seen in the following example. For what you can do with this object, please see the next chapter.

#### find a specific column

    col=hk_this.datasource().column_by_name("author")

### Objects of type hk_dsdatavisible - the class hk_column

For the next example, create a form, set a datasource, create a lineedit field and connect it to a column of the datasource (for details see the &knoda; tutorial at http://hk-classes.sourceforge.net/tutorials.

A lineeditfield is of type hk_dsdatavisible. hk_dsdatavisible inherits from hk_dsvisible. The most important method of hk_dsdatavisible is column(). This method returns a class of type hk_column, representing a specific column of a table.

#### hk_column data methods

> set_asstring(value)

> lets you set a new value for this object

> asstring

> returns the current value as a string value

> set_asdouble(value)

> lets you set a new value for this object

> asdouble()

> returns the current value as a double value

> set_asinteger(value)

> lets you set a new value for this object

> asinteger()

> returns the current value as a integer value

> is_readonly()

> returns true if this column is read-only; if data can be changed it returns false

> unsigned int find(from_rownumber,to_rownumber,searchtext,bool wholephrase=false,bool casesensitive=false,bool backwards=false)

> searches for a specific value in a column, returns the row number if found, hk_datasource.max_rows()+1 if not found

> unsigned int find(searchtext,bool wholephrase=false,bool casesensitive=false,bool backwards=false)

> searches for a specific value in a column, returns the row number if found, hk_datasource.max_rows()+1 if not found. This version searches all rows of a datasource.

> hk_datasource* datasource()

hk_dsdatavisible contains the convenience function set_value(const hk_string&) which displays the value and sets the value by calling hk_column.set_asstring(const hk_string&) if the column is set. The convenience function hk_string value() returns the currently displayed value.

#### hk_column type methods

> hk_string name(void)

> void set_name(const hk_string&n)

> sets the column name

> enum_columntype {textcolumn,auto_inccolumn,smallintegercolumn,integercolumn,smallfloatingcolumn, floatingcolumn,datecolumn,datetimecolumn,timecolumn,timestampcolumn,binarycolumn, memocolumn,boolcolumn,othercolumn}

> columntype()

> returns the type of the column

> size()

> returns the column size (e.g. if this column was created as CHAR(10) it will return 10)

> bool is_primary(void)

> returns true if this column is part of a primary key

> bool set_primary(bool i)

> bool is_notnull(void)

> bool set_notnull(bool i)

> if true the column needs a value

#### Read data

    col=hk_this.datasource().column_by_name("name")
    hk_this.show_warningmessage(col.asstring())

#### Write data

    col=hk_this.datasource().column_by_name("name")
    col.set_asstring("my new value")

This changes the value of the current column. The data is saved either when

- the row position changes (e.g. by calling hk_datasource.goto_row())

- the datasource is disabled (e.g. by calling hk_datasource.disable())

- the changes are manually stored by calling hk_datasource.store_changed_data()

#### Search data

    col=hk_this.datasource().column_by_name("name")
    result=col.find("Schiller")
    if result > hk_this.datasource().max_rows():
         hk_this.show_warningmessage("value not found")
    else:
         hk_this.show_warningmessage("Value found at row position: "+str(result))

As this is working well, it is a bit inconvenient to handle. For this hk_dsdatavisible provides some convenience functions:

#### hk_dsdatavisible methods

> set_value(newvalue)

> sets the current value,where 'value' is a string. If a column is set, the datasource will be changed, if not it will be only displayed

> value()

> returns the displayed string (the current value)

> find(from_rownumber,to_rownumber,searchtext[,wholephrase[,casesensitive[,backwards]]])

> searches for a specific value in a column, returns the row number if found, hk_datasource.max_rows()+1 if not found

> find(searchtext[,wholephrase[,casesensitive[,backwards]]])

> searches for a specific value in a column, returns the row number if found, hk_datasource.max_rows()+1 if not found. This version searches all rows of a datasource.

The same examples as before, but now only using the hk_dsdatavisible functions:

#### Read data

    hk_this.show_warningmessage(hk_this.value())

#### Write data

    hk_this.set_value("my new value")

#### Search data

    result=hk_this.find("Schiller")
    if result > hk_this.datasource().max_rows():
         hk_this.show_warningmessage("value not found")
    else:
         hk_this.show_warningmessage("Value found at row position: "+str(result))

### Objects of type hk_presentation - forms and reports

Both forms and reports inherit from hk_presentation. The most import function of hk_presentation is set_mode(mode). The two possible modes are hk_presentation.designmode and hk_presentation.viewmode

#### Handling forms

If you want to get a reference to a specific object in a form you have two possibilities

- get_pyvisible(unique_number)

- get_pyvisible(const hk_string&identifier)

The first function needs the unique identifier number of the wanted object. Each visible object of type hk_visible has a unique number as an identifier, which can't be changed.

To find this number, click on the object. In the caption of the property editor you can see the number in brackets.

The second function uses a unique text identifier. This is a user-defined unique identifier and can be set in the property editor.

Both functions return a reference to the object.

In versions prior to 0.7.4 the usage of the function get_visible() was recommended. This function is still valid. Nevertheless you should use get_pyvisible(), because there is no need for type casting any more (the whole range of cast_* function are not required any more)

The next program shows you how to start a form:

##### displaying a form

    myform=hk_this.datasource().database().new_formvisible()
    myform.load_form("authorform")
    myform.set_mode(myform.viewmode)

#### Handling reports

Here is how to start a report:

##### displaying a report

    myreport=hk_this.datasource().database().new_reportvisible()
    myreport.load_report("complexreport")
    myreport.set_mode(myreport.viewmode)

A visible object in a report is of type hk_reportdata, which inherits from hk_dsdatavisible. The main methods are

- set_data(const hk_string& d)

- hk_string data(void)

The data property contains the value that is displayed. See the knodatutorial chapter "The report field" for details.

The following example shows how to print numbers in different colours. For this we use the "onprint" action

##### reportdata onprint

    value=hk_this.column().asdouble()
    if value<0:
       hk_this.set_foregroundcolour(hk_red)
    else:
       if value==0:
           hk_this.set_foregroundcolour(hk_black)
       else:
          hk_this.set_foregroundcolour(hk_blue)

## using &hk_classes; as a Python module

You can use &hk_classes; within Python. You can write your own Python applications using all the elements of &hk_classes; or interactively explore your data.

### Python module basics

    horst@horstnotebook:~> python
    Python 2.2.2 (#1, Mar 17 2003, 15:17:58)
    [GCC 3.3 20030226 (prerelease) (SuSE Linux)] on linux2
    Type "help", "copyright", "credits" or "license" for more information.
    >>> from hk_classes import *
    >>> dr=hk_drivermanager()
    >>> con=dr.new_connection("mysql")
    >>> con.set_password("mypassword")
    >>> con.connect()
    1
    >>> db=con.new_database("exampledb")
    >>> mytable=db.new_table("authors")
    >>> mytable.enable()

### hk_drivermanager

hk_drivermanager handles the database drivers. You need just one object of this type for your whole application.

> - vector<hk_string>* driverlist(void): returns a list of all available database drivers

> - hk_connection* new_connection(const hk_string&drivername): creates a new object of type hk_connection;

### hk_connection

hk_connection connects to the SQL Server. The most important functions are set_host(), set_user(), set_password() and connect().

> void set_host(const hk_string&)

> sets the host name or host IP number

> hk_string host(void)

> void set_user(const hk_string& )

> sets the user name used on the host

> hk_string user(void)

> void set_password(const hk_string&p)

> sets the password for the user

> void set_tcp_port(unsigned int t)

> sets the TCP port

> unsigned int tcp_port(void)

> virtual unsigned int default_tcp_port(void) const

> returns the default TCP port for this database server

> bool connect(enum_interaction c=interactive)

> connects to the server using the user, host and TCP data

> bool disconnect(void)

> disconnects from the server

> bool is_connected(void)

> returns true if this connection is connected to the server

> vector<hk_string>* dblist(void)

> returns a list of all existing databases in this connection

> hk_database* new_database(const hk_string&name="")

> creates a new hk_database object

> bool delete_database(const hk_string&dbase)

> deletes an exisiting database

> bool database_exists(const hk_string&databasename)

> returns true if the database "databasename" exists

### hk_database

hk_database represents a particular database on the SQL Server

> vector<hk_string>* tablelist(void)

> returns a list of all existing tables in this database

> vector<hk_string>* querylist(void)

> returns a list of all existing queries in this database

> vector<hk_string>* formlist(void)

> returns a list of all existing forms in this database

> vector<hk_string>* reportlist(void)

> returns a list of all existing reports in this database

> hk_datasource* new_table(const hk_string&name="",hk_presentation* p=NULL)

> gets a new table object of type hk_datasource (read and write)

> hk_datasource* new_resultquery(hk_presentation* p=NULL)

> gets a new query object of type hk_datasource (readonly)

> hk_actionquery* new_actionquery(void)

> gets a hk_actionquery object. It can execute SQL statements that don't return data and are only successful or not successful (e.g. CREATE TABLE)

> hk_datasource* load_datasource(const hk_string&name,bool query=false,hk_presentation* p=NULL)

> a convenience function for new_table and new_resultquery, that loads an existing datasource

> bool delete_table(const hk_string&table,enum_interaction x=interactive)

> deletes a table

> bool table_exists(const hk_string&tablename)

> returns true if the table 'tablename' exists

> bool query_exists(const hk_string&queryname)

> returns true if the query 'queryname' exists

## Tips and tricks

### How to display the data

#### Show data

    horst@horstnotebook:~> python
    Python 2.2.2 (#1, Mar 17 2003, 15:17:58)
    [GCC 3.3 20030226 (prerelease) (SuSE Linux)] on linux2
    Type "help", "copyright", "credits" or "license" for more information.
    >>> from hk_classes import *
    >>> dr=hk_drivermanager()
    >>> con=dr.new_connection("mysql")
    >>> con.set_password("secret")
    >>> con.connect()
    1
    >>> db=con.new_database("exampledb")
    >>> table=db.new_table("authors")
    >>> i=0
    >>> table.enable()
    SQL : SELECT  *  FROM  `authors`
    1
    >>> table.goto_first()
    1
    >>> while i< table.max_rows():
        ...    table.show_currentrow()
        ...    table.goto_next()
        ...    i=i+1
    ['1', 'Goethe,Johann Wolfgang', '1749', '1832', 'FALSE']
    1
    ['2', 'Schiller, Friedrich von', '1759', '1805', 'TRUE']
    1
    ['3', 'Lessing, Gotthold Ephraim', '1729', '1781', 'TRUE']
    1
    ['4', 'Kleist', '1400', '0', 'FALSE']

### How to execute a query

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

#### Execute a "SELECT" query

    horst@horstnotebook:~> python
    Python 2.2.2 (#1, Mar 17 2003, 15:17:58)
    [GCC 3.3 20030226 (prerelease) (SuSE Linux)] on linux2
    Type "help", "copyright", "credits" or "license" for more information.
    >>> from hk_classes import *
    d>>> dr=hk_drivermanager()
    >>> con=dr.new_connection("mysql")
    >>> con.set_password("secret")
    >>> con.connect()
    1
    >>> db=con.new_database("exampledb")
    >>> query=db.new_resultquery()
    >>> query.set_sql("SELECT * FROM authors")
    1
    >>> query.enable()
    SQL : SELECT  *  FROM  authors
    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.

#### Execute an action query

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

### How to create a table

To create a table, first get a new table object, set a name and set the mode to "createtable".

After that you can define new columns. First create it with new_column() and then set the type, name etc. When finished, create the table with create_table_now().

#### create table

    >>> table = db.new_table()
    >>> table.set_name("my new table")
    >>> table.setmode_createtable()
    >>> col=table.new_column()
    >>> col.set_columntype(hk_column.auto_inccolumn)
    >>> col.set_name("id")
    >>> col=table.new_column()
    >>> col.set_name("name")
    >>> table.create_table_now()
    CREATE TABLE `my new table` ( `id` BIGINT(1) NOT NULL AUTO_INCREMENT ,
     BIGINT, PRIMARY KEY ( `id` ) )
    Table created
    1

And here are the creation relevant methods of hk_column

#### hk_column type methods

> name()

> returns the name of this column

> set_name(name)

> sets the column name

> set_columntype(type)

> sets the type of the column

> Possible values are

> - textcolumn

> - auto_inccolumn

> - smallintegercolumn

> - integercolumn

> - smallfloatingcolumn

> - floatingcolumn

> - datecolumn

> - datetimecolumn

> - timecolumn

> - timestampcolumn

> - binarycolumn

> - memocolumn

> - boolcolumn

> - othercolumn

> columntype()

> returns the type of the column.

> set_size()

> sets the column size (e.g. if this column was should be a textcolumn with 10 characters set the type with set_type and the size with this function)

> size()

> returns the column size (e.g. if this column was created as CHAR(10) it will return 10)

> set_primary(primary)

> if 'primary' is true the column will be part of the primary key (primary index). Can only be edited if the datasource is in the mode ALTER or CREATE.

> is_primary()

> returns true if this column is part of a primary key

> set_notnull(notnull)

> if 'notnull' true the column needs a value

> is_notnull()

> returns True if this column has to have a value

### How to create a calculated value field

Create a lineedit field with the name "calculated_field" and don't set any datasource or field property. Put the following code in the onpush action of a button. The value you want to display has first be formatted with format_number and then be set with hk_dsdatavisible.set_value.

#### calculated values

    col1=hk_this.datasource().column_by_name("field_1")
    col2=hk_this.datasource().column_by_name("field_2")
    result=(col1.curval_asdouble()*col2.curval_asdouble())
    calcfield=hk_thisform.get_pyvisible("calculated_field")
    calcfield.set_value(format_number(result,
    calcfield.use_numberseparator(),calcfield.precision()))

### How to use the last inserted value as a default for the next row that will be inserted

Insert the following code into the after_insert action.

#### default values

    hk_this.set_defaultvalue(hk_this.value())

### How to define macros (I) - using modules

You can define macros - that means global available Python functions - within your database as a module

#### Macro example

    def mymakro(hk_this,v):
      hk_this.show_warningmessage("Displaying the text: "+v)

Store the above code as a module named "mytestmodule".

This can be loaded e.g. in the on_open action of a form:

#### Macro example (part II)

    hk_thisform.load_module("mytestmodule")

This can then be called from a buttons onclick action:

#### Macro example (part III)

    mymakro(hk_this," my text")

### How to define macros (II)

You can alternatively define macros - that means global available Python functions - in the onopen() action of the form, and then call it from any other action:

#### Macro example

    def mymakro(hk_this,v):
      hk_this.show_warningmessage("Displaying the text: "+v)

This can be called from a buttons onclick action:

#### Macro example (part II)

    mymakro(hk_this," my text")

### How to format numbers

To format a number you can use the function format_number(). It will return a string. The first parameter is the number you want to format, the second is a boolean value whether or not you want a thousands separator, the third parameter shows the number of digits, and with the last you can set the target locale.

#### Formatting a number

    horst@horstnotebook:~> python
    Python 2.3.2 (#1, Oct 28 2003, 21:22:16)
    [GCC 3.3 20030226 (prerelease) (SuSE Linux)] on linux2
    Type "help", "copyright", "credits" or "license" for more information.
    >>> from hk_classes import *
    >>> number=12345.6789
    >>> print number
    12345.6789
    >>> print format_number(number,1,2)
    12.345,68
    >>> print format_number(number,True,2)
    12.345,68
    >>> print format_number(number,True,2,"C")
    12345.68
    >>> print format_number(number,True,2,"de_DE")
    12.345,68
    >>> print format_number(number,False,2,"de_DE")
    12345,68
    >>>

### How to print rows in alternating colours

When printing a report it is often wanted to have every second row printed with a different colour to make it easier to read the output. To do so put the following code in the on_print action of a report field

#### Printing alternate colours

    if hk_thisreport.datasource().row_position() % 2 ==0:
       hk_this.set_backgroundcolour(hk_white)
    else:
       hk_this.set_backgroundcolour(hk_grey)

### How to set the focus in a form

Put the following code into the on_click action of a button:

#### Setting the taborder

    hk_this.datasource().setmode_insertrow()
    hk_thisform.goto_taborder_first()

When you push the button the datasource will allow you to insert a new row and move the focus from the button to the first field. Other possible functions are:

> set_focus(widget)

> will set the focus to 'widget'

> #### Setting the taborder for a specific field

>     hk_thisform.set_focus(hk_thisform.get_pyvisible(2))

> set_taborder( taborder[,registerchange [, forcesetting]])

> sets the tab order (the focus order) of the widgets. 'taborder' is a list of the presentation numbers of the widgets, which should get the focus

> goto_taborder_next()

> will move the the focus to the next widget (see set_taborder )

> goto_taborder_previous()

> will move the the focus to the previous widget (see set_taborder )

> goto_taborder_first()

> will move the the focus to the first widget (see set_taborder )

> goto_taborder_last()

> will move the the focus to the last widget (see set_taborder )

### How to handle binary data in a Python script

Put the following code into the on_click action of a button:

#### Setting binary data to a column

    myfile=open("/home/name/mypicture.jpg")
    mydata=myfile.read()
    x=struct_raw_data()
    x.length=myfile.tell()
    x.data=mydata

    mycol=hk_this.datasource().column_by_name("mybinarycolumn")
    mycol.set_asbinary(x)

    hk_this.datasource().store_changed_data()

### How to react on pressed keys

Put the following code into the on_key action of a lineedit field:

#### Reacting on keyboard input

    key=hk_this.key()
    print "on_key=(",key.key(),")(",key.text(),")"
    if key.state()&key.state_ctrl:
      print "ctrl"
    if key.state()&key.state_shift:
      print "shift"
    if key.state()&key.state_alt:
      print "alt"

    if key.key()==key.key_F5:
     show_warningmessage("F5 pressed. This will be ignored")
     key.set_accept_key(False)

    if key.key()==key.key_M and key.state()&key.state_ctrl:
     show_warningmessage("ctrl m pressed")

when the on_key action is triggered an object of type hk_key is created and can be called via hk_this.key(). If you set the hk_key.set_accept_key() method to 'False', the key will not be accepted as input.

### How to load a file content into a field using a filedialog

Put the following code into the on_click action of a button:

#### Loading a file content into a field

    filename=hk_this.show_filedialog()
    file=open(filename)
    data=file.read()
    datafield=hk_thisform.get_pyvisible("datafield")
    datafield.set_value(data)

This example lets the user select a file(line 1) and load its content (line 2 and 3) into a field (line 4 and 5).

### How to add or modify data, copy data between 2 datasources etc.

There are different ways within hk_classes to add data to a table:

- Using hk_classes commands

- Using SQL (INSERT, UPDATE etc.)

The advantage to use the hk_classes commands is, that you don't have to bother about correct SQL, gives you the possibility of a much finer grained data manipulation and it will also work over the frontiers of different database backends, even to those who don't understand SQL. I recommend to use this way.

#### Copy data from an existing datasource to an existing table in the same database

The following example copies all names of the authors into another,existing table "newauthors". It assumes that both datasources (tables) are not defined as form datasources and will be part of the same database (on the same SQL server). Of course it works the same way with form datasources (which you can access with the hk_thisform.get_datasource() function family), except the fact, that you don't need to enable them (lines 5 and 7).

Lines 1 to 7 are simple definition rows. Lines 8 and 9 define the source and the target columns. Of course you can use as many columns as needed, or write fixed text or whatever you want.

Copying happens in lines 10 to 15. The targettable must be in insertmode (line 11) otherwise it will update the current row.

To copy only some rows, simply edit the while loop.

    db=hk_thisform.database() (1)
    sourcetable=db.new_table("authors") (2)
    targettable=db.new_table("newauthors") (3)
    i=0 (4)
    sourcetable.enable() (5)
    sourcetable.goto_first() (6)
    targettable.enable() (7)
    sourcecolumn1=sourcetable.column_by_name("name") (8)
    targetcolumn1=targettable.column_by_name("name") (9)
    while i< sourcetable.max_rows(): (10)
        targettable.setmode_insertrow() (11)
        targetcolumn1.set_asstring(sourcecolumn1.asstring()) (12)
        targettable.store_changed_data() (13)
        sourcetable.goto_next() (14)
        i=i+1 (15)
    targettable.disable()(16)
    sourcetable.disable()(17)

#### Copy data from an existing datasource to an existing table in different databases (also different server backends)

To do the same, but store the data on a different server you simply have to replace line 3 from the previous example. Let's say your source data is on a Postgres server and the target data should be stored in a local format, e.g. SQLite3. It works the same with Mysql etc, simply replace the connection name in line 3. The connect() command in line 4 will open the password dialog when necessary.

    [Lines 1 to 2 as above] (1)
    dr=db.connection().drivermanager() (2)
    con=dr.new_connection("sqlite3") (3)
    con.connect() (4)
    sqlitedb=con.new_database("mysqlitedatabase") (5)
    targettable=sqlitedb.new_table("newauthors") (6)
    [Lines 4 to 17 as above] (7)

#### Copy data from an existing datasource to a new table

The last example copies the result a of query into a new table. In line 3 you can define the SQL statement. Be aware that you have to use the ANSI SQL delimiters, means " as identifier delimiter and ' as text delimiter. Particulary Mysql uses different delimiters. hk_classes will fix them in resultqueries (but not in actionqueries). Don't worry about the function name "copy_table" in line 5. It will also allow to copy data from resultqueries.

    targetdb=hk_thisform.database() (1)
    sourcequery=targetdb.new_resultquery() (2)
    sql="SELECT * from \"authors\"" (3)
    sourcequery.set_sql(sql) (4)
    targetdb.copy_table(sourcequery,True,False) (5)

### How to load a subform

It may be necessary that you want to load a subform by clicking a button. To do so, add the following code to the on_click action of a button. Line 1 gets a handle to the subformobject named "mysubform". In line 2 old settings will be cleared, line 3 adds depending fields between the form and the subform. In line 4 the name of the (existing) subform is added, and last not least in line 5 the definition will be loaded and activated.

#### Load a subform

    subform=hk_thisform.get_pyvisible("mysubform") (1)
    subform.clear_depending_fields() (2)
    subform.add_depending_fields("subfield","masterfield",False) (3)
    subform.set_name("Subformname") (4)
    subform.activate_subform() (5)