我有一部分代码创建和打开数据库,但我有问题,因为我想在同一个数据库中创建两个表。我该怎么办?
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();
}
我试图在一个声明中做到这一点
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));"
但失败了,
答案 0 :(得分:2)
您是否尝试将create table query作为两个单独的命令执行?
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。请注意,我不是Flex开发人员。