In most cases, you will create the database table using your database management software. This gives you a more convenient interface to the creation process. Different databases use different syntax to create a table. However, if you need to create a table within your script, there are two options:
Call the ODBCConnection.CreateTable
method.
Call the ODBCConnection.QueryAndStore
method.
The CreateTable method helps to construct the query, but
it still requires you to understand the SQL syntax of table creation for your
database.
.conn.CreateTable ("table name",
"id int PRIMARY KEY IDENTITY",
"ptname VARCHAR(20) NOT NULL",
"ptvalue DOUBLE NOT NULL",
"pttimestamp DATETIME NOT NULL",
"ptquality VARCHAR(20) NOT NULL"
);The CreateTable arguments consist of the table name
followed by any number of definitions for the columns in the table. The column
definitions depend on the database being used. In particular, the primary key
field (in this example, id) is very different from one
database to another. The primary key must be integer, unique and
auto-incrementing. In the case of Microsoft Access, you must issue an additional
query to create a primary key:
.conn.CreateTable ("table_name",
"id COUNTER",
"ptname VARCHAR(20) NOT NULL",
"ptvalue DOUBLE NOT NULL",
"pttimestamp DATETIME NOT NULL",
"ptquality VARCHAR(20) NOT NULL"
);
.conn.QueryAndStore ("create unique index p_id on table_name (id)
with primary disallow null");The alternative to using
ODBCConnection.CreateTable is to
construct your own complete SQL statement and submit it to the database using
ODBCConnection.QueryAndStore:
.conn.QueryAndStore ("create table table_name
(id COUNTER, ptname VARCHAR(20) NOT NULL,
ptvalue DOUBLE NOT NULL,
pttimestamp DATETIME NOT NULL,
ptquality VARCHAR(20) NOT NULL)");
.conn.QueryAndStore ("create unique index p_id on table_name (id)
with primary disallow null");If any call to CreateTable or
QueryAndStore generates an error, then the error will
be thrown. You can catch the error using a try/catch block within
your script.