在sqlite 3(python)中在表内创建表

2024-09-30 07:25:03 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试为一个配方存储程序创建一个sqlite数据库,它保存在配方表中,包括人数、配料、烹饪温度和烹饪时间。我打算在配料部分中创建另一个表,存储“id”(将自动递增)、质量编号(例如200)、质量类型(例如g)和食品类型(例如面粉)。在

我使用这段代码来获取变量并存储它们:

    def setIngredientsInRecipe(recipeName):
        cursor.execute("""INSERT INTO %s(ingredients) CREATE TABLE\
                    (ID INT PRIMARY KEY AUTO_INCREMENT, MassNumber VARCHAR(10), MassType VARCHAR(50) FoodType VARCHAR(50))""" % (recipeName))
        print ""
        massNoInput = raw_input("Please enter your first ingredient mass(just the number): ")
        massTypeInput = raw_input("Now please input the mass type (grams etc): ")
        foodInput = raw_input("Please enter your first ingredient type: ")

        cursor.execute("""INSERT INTO %s(ingredients(MassNumber)) VALUES('%s')""" % (recipeName,massNoInput))
        cursor.execute("""INSERT INTO %s(ingredients(MassType)) VALUES('%s')""" % (recipeName,massTypeInput))
        cursor.execute("""INSERT INTO %s(ingredients(FoodType)) VALUES('%s')""" % (recipeName,foodInput))

        return recipeName

显然,稍后我将添加一些数字检查,以首先查看输入是否有效。在

一开始就崩溃了光标.执行行,说是语法错误。。。在

如果有人能向我解释如何解决这个问题,我将非常感激,我已经看了这段代码很多年了!在

谢谢, 瑞安:)


Tags: 代码类型inputexecuteraw质量配方cursor
1条回答
网友
1楼 · 发布于 2024-09-30 07:25:03

使用两个表,一个用于配方,一个用于配料,并为每个配料记录其所属配方的ID:

CREATE TABLE recipe(ID, People, CookingTime);
CREATE TABLE ingredient(ID, RecipeID, MassNumber, MassType, FoodType);

要在配方中添加一种成分,只需在该表中插入一条记录即可 (这里,42是配方的ID):

^{pr2}$

相关问题 更多 >

    热门问题