Creating a Table

This tutorial shows you how to create a Symbian SQL table.

This tutorial uses code from the Basic SQL example application.

Assumptions

You have a database. The database has no tables and therefore no data.

SQL statements

The following SQL statements are used for this example:

CREATE TABLE Pets( person TEXT, cat SMALLINT, dog SMALLINT, rodent SMALLINT, bird SMALLINT)

The RSqlDatabase::Exec function provides a mechanism for executing Symbian SQL statements.

Create a table The steps required to create a table are shown here:
  1. Declare a constant to hold the table handle:
    _LIT(KTabCreate,"CREATE TABLE Pets( person TEXT, cat SMALLINT, dog SMALLINT, rodent SMALLINT, bird SMALLINT);");
    KTabCreate will be used later by the SQL EXECUTE command.
  2. Instantiate the required objects:
    RSqlDatabase db;
    CConsoleBase* iConsole;
    This creates an RSqlDatabase object, which is needed to execute the SQL statement.
  3. Execute the SQL statement:
    db.Exec(KTabCreate)
    This will execute the SQL statement to create the table.
The table now exists and you can begin adding data to it.

Your database now has at least one table. You can begin to add data to the table and you can query and edit it.

Create table example

The following code snippet is from the example used for this tutorial:

_LIT(KTabCreate,"CREATE TABLE Pets( person TEXT, cat SMALLINT, dog SMALLINT, rodent SMALLINT, bird SMALLINT);");
_LIT(KCreateTable,"\nCreating a table\n");
...
CConsoleBase* iConsole;
...
RSqlDatabase db;
iConsole->Printf(KCreateTable);
...
User::LeaveIfError(db.Exec(KTabCreate));
...

Now that you have created a table you need to populate it with some data. The following tasks will show you how to populate a table: