5.3. 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

   1 >>> table = db.new_table()
   2 >>> table.set_name("my new table")
   3 >>> table.setmode_createtable()
   4 >>> col=table.new_column()
   5 >>> col.set_columntype(hk_column.auto_inccolumn)
   6 >>> col.set_name("id")
   7 >>> col=table.new_column()
   8 >>> col.set_name("name")
   9 >>> table.create_table_now()
  10 CREATE TABLE `my new table` ( `id` BIGINT(1) NOT NULL AUTO_INCREMENT , `name` BIGINT, PRIMARY KEY ( `id` ) )
  11 Table created
  12 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