gpt4 book ai didi

sqlite - 如何在移动应用程序的SQLite中的同一数据库中创建多个表?

转载 作者:行者123 更新时间:2023-12-03 19:03:54 25 4
gpt4 key购买 nike

我有一部分代码可以创建和打开数据库,但是有一个问题,因为我想在同一数据库中创建两个表。我该怎么做?

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));"


但是失败了

最佳答案

您是否尝试过将创建表查询作为两个单独的命令执行?

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开发人员。

关于sqlite - 如何在移动应用程序的SQLite中的同一数据库中创建多个表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8460552/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com