Paweł
Paweł

Reputation: 85

how to create multiple tables in same database in SQLite on mobile application?

I have a part of my code creating and opening database but I have problem because I want to create two tables in the same database. How can I do it?

private var db:File = File.userDirectory.resolvePath("DataBase.sql");
        private var conn:SQLConnection;
        private var createTableStmt:SQLStatement;
        private var createTableSQL:String =
            "CREATE TABLE IF NOT EXISTS RecipeDB (" +
            "RecipeID INTEGER PRIMARY KEY AUTOINCREMENT," +
            "Name VARCHAR(20)," + "Category VARCHAR(20)," + "Origin VARCHAR(20)," + "Recipe VARCHAR(2000)," + "Favorite BOOL)";

        private var selectStmt:SQLStatement;
        private var selectSQL:String = "SELECT * FROM RecipeDB";
        private var insertStmt:SQLStatement;
        private var insertSQL:String =
            "INSERT INTO FoodDish2 (Name, Category, Origin, Recipe)" +
            "VALUES (:Name, :Category, :Origin, :Recipe)";
        protected function application1_applicationCompleteHandler
        (event:FlexEvent):void
        {
            conn = new SQLConnection();
            conn.addEventListener(SQLEvent.OPEN, openHandler);
            conn.addEventListener(SQLErrorEvent.ERROR, errorHandler);
            conn.openAsync(db);
        }
        private function openHandler(event:SQLEvent):void {
            log.text += "Database opened successfully";
            conn.removeEventListener(SQLEvent.OPEN, openHandler);
            createTableStmt = new SQLStatement();
            createTableStmt.sqlConnection = conn;
            createTableStmt.text = createTableSQL;
            createTableStmt.addEventListener(SQLEvent.RESULT, createResult);
            createTableStmt.addEventListener(SQLErrorEvent.ERROR,
                errorHandler);
            createTableStmt.execute();
        }

I've tried to do it in one statement

private var createTableSQL:String ="CREATE TABLE IF NOT EXISTS RecipeDB(
RecipeID INTEGER PRIMARY KEY AUTOINCREMENT,
Name VARCHAR(20),
Category VARCHAR(20),
Origin VARCHAR(20),
Recipe VARCHAR(2000));

CREATE TABLE IF NOT EXISTS IngredientDB(
RecipeID INTEGER REFERENCES RecipeDB(RecipeID),
Ingredient VARCHAR(20),
Quantity VARCHAR(20));"

but failed,

Upvotes: 1

Views: 2652

Answers (1)

Michał Powaga
Michał Powaga

Reputation: 23183

Have you tried to execute create table query as two separated commands?

private var db:File = File.userDirectory.resolvePath("DataBase.sql");
private var conn:SQLConnection;
private var createTableStmt:SQLStatement;
private var createTableRecipeSQL:String =
    "CREATE TABLE IF NOT EXISTS RecipeDB (" +
    "RecipeID INTEGER PRIMARY KEY AUTOINCREMENT," +
    "Name VARCHAR(20)," + "Category VARCHAR(20)," + "Origin VARCHAR(20)," + "Recipe VARCHAR(2000)," + "Favorite BOOL)";

private var createTableIngredientSQL:String =
    "CREATE TABLE IF NOT EXISTS IngredientDB(" +
    "RecipeID INTEGER REFERENCES RecipeDB(RecipeID)," +
    "Ingredient VARCHAR(20),Quantity VARCHAR(20))";

......

private function openHandler(event:SQLEvent):void {
    log.text += "Database opened successfully";
    conn.removeEventListener(SQLEvent.OPEN, openHandler);
    createTableStmt = new SQLStatement();
    createTableStmt.sqlConnection = conn;
    createTableStmt.text = createTableRecipeSQL;
    createTableStmt.addEventListener(SQLEvent.RESULT, createResult);
    createTableStmt.addEventListener(SQLErrorEvent.ERROR, errorHandler);
    createTableStmt.execute();

    createTableStmt = new SQLStatement();
    createTableStmt.sqlConnection = conn;
    createTableStmt.text = createTableIngredientSQL;
    createTableStmt.addEventListener(SQLEvent.RESULT, createResult);
    createTableStmt.addEventListener(SQLErrorEvent.ERROR, errorHandler);
    createTableStmt.execute();
}

ps. please mind that I'm not a Flex developer.

Upvotes: 2

Related Questions