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().

Example 5.4. 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

Figure 5.1. 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