English 中文(简体)
• 如何阻止我的主要关键识别器从加固中删除?
原标题:How to stop my primary key ID from incrementing?
  • 时间:2024-03-28 00:17:07
  •  标签:
  • python
  • sql

Shortened, essential code, for reference:

def sqlInput(groupValues, userLogin):
    sqliteConnection = None
    try:
        sqliteConnection = sqlite3.connect( NEA.db )
        cursor = sqliteConnection.cursor()
        print()
        print("Connected to SQLite server.")
        print()
        cursor.execute(   CREATE TABLE IF NOT EXISTS "Food Tracker" (
                        ID INTEGER PRIMARY KEY,
                        "Entry Number" INTEGER,
                        "Calories" INTEGER,
                        "Food Item" TEXT,
                        "Date" TEXT,
                        "Time" TEXT
                    )   )

        insertQuery ="""INSERT INTO Food Tracker(
                                    "ID", "Food Item", "Calories", "Date", Time) 
                                VALUES 
                                (?,?,?,?,?,?)"""
        inserting = list(
                zip(userLogin.primarykey, groupValues.entry, groupValues.food, map(int, groupValues.calories), groupValues.date, groupValues.time))
        cursor.executemany(insertQuery,inserting)
        sqliteConnection.commit()
        print("Successfully entered data into SQLite database.")
        print()
        menu(userLogin)
    except sqlite3.Error as error:
        print()
        print("Failed to insert data into SQLite database:", error)
        print()
    finally:
        if sqliteConnection:
            sqliteConnection.close()

在这方面,我正在奋起努力。

 insertQuery ="""INSERT INTO Food Tracker(
                                    ID, Food Item, Calories, Date, Time) 
                                VALUES 
                                (?,?,?,?,?)"""
        inserting = list(
                zip(userLogin.primarykey, groupValues.entry, groupValues.food, map(int, groupValues.calories), groupValues.date, groupValues.time))

Everything inserts fine into the database. No problems with anything apart from the ID column. It won t display as my ID, which is stored in userInput.primarykey (no errors with that either), and instead of ID being "62381" for example which is a valid example ID, it will display as "1" and increment with every entry added. How do I get my ID to stay as 62381 for example within userInput.primarykey and not increment?

我曾尝试增加一个精良的外国钥匙,我曾尝试改变母体的次序,并使用其他指挥,如MAP & ZIP。

Nothing works, AI won t provide an answer, I really need a solution. Any further information needed, let me know although I believe I ve added everything you d need.

请帮助我解决这一问题,因为我已有很多问题被搁置,但这是有效的,我需要迅速的解决办法。

Error message: Failed to retrieve last entry number: UNIQUE constraint failed: Food Tracker.ID How can I use the primary key multiple times?

问题回答

我如何利用主要关键多时期?

页: 1 主要关键必须是独特的。

我如何获得我的身份证,例如,在用户投入中停留在62381。 主要关键因素而不是增量?

Don t. Leave the ID as is and do not insert your own. integer primary key is special in SQLite and will provide a good unique primary key by default. Other databases would require you to declare it auto-increment or identity.

相反,在储存<编码>用户行程>上添加一栏。

我只能猜测,<代码>用户行程表>、主要<>/代码”是指与食品跟踪器浏览有关的用户。 它应当是“User ID”,并宣布为


说明1:您的插入有5项价值,但你在6项中再次通过。

注2:与<条码>细致。 如果你改变你的表格定义,你将不使用;你将使用旧的定义。 相反,对于像这种微薄的测试数据库来说,该表已经下降,并重新启用。

看来,你再次谈论的是自动加固的主要关键。 这是一个共同的数据库特征,在无意为主要钥匙确定明确价值的情况下,通常用于假设情景。

我们可以在这样一行的DDL中看到,你暗含地界定了<代码>栏。 FoodTracker.ID as with an auto-increment first key.

ID INTEGER PRIMARY KEY,

post解释这种行为。

从你试图在座标上添加一个价值,作为第一个<代码>?参数,可以看出,你正在试图为这个领域明确确定一个特殊价值。

INSERT INTO Food Tracker(
                                    ID, Food Item, Calories, Date, Time) 
                                VALUES 
                                (?,?,?,?,?)

但是,我建议你遵循本表定义中关于id的制约因素的公认答复中的建议。

它还建议使用<条码>INT数据类型取代<条码>t> INTEERT/code>,这是一个少见之处,但在《评论》中指出。 较老的SO员额。

两种数据类型之间的区别由以下概念加以解释:affinity,可在官方网站 section。 3. 基本而言,<代码>INTEERT是一个“灵活”的数据类型,可与其他同类领域(即动态分类)相互兼容。 但是,自动加固的主要钥匙的数据类型需要与静态分类一样,因此type 代码>INT而不是“affinity”INTEERT

见第2节,我们改为

Any column in an SQLite version 3 database, except an INTEGER PRIMARY KEY column, may be used to store a value of any storage class.

这似乎表明,以这种方式界定的一栏无法储存价值,就像你试图插入的栏。

在回答你关于这一职位的评论时,看来“Schwern”是你打算插入“foreign key参引的,而不是你原先描述的主要关键价值。

在此情况下,虽然上述主要要素仍然有效,但你可能希望DDL作这样的陈述:

CREATE TABLE IF NOT EXISTS "UserLogin" (
                        ID INTEGER PRIMARY KEY,
                        "UserName" TEXT
);


CREATE TABLE IF NOT EXISTS "Food Tracker" (
                        ID INTEGER PRIMARY KEY,
                        "UserLoginId" INTEGER,
                        "Entry Number" INTEGER,
                        "Calories" INTEGER,
                        "Food Item" TEXT,
                        "Date" TEXT,
                        "Time" TEXT,
                        FOREIGN KEY (UserLoginId) REFERENCES UserLogin (ID)
);

我在这里是为了展示基本的逻辑,但你可以在必要时用适当的参数执行。

First, make sure we have a user:

INSERT INTO UserLogin (
    UserName  
)
VALUES ( 42123 );

然后,我们在食品轨迹上添加一个记录。

INSERT INTO "Food Tracker" (
    "UserLoginId", 
    "Entry Number"
    --,etc.
) 
SELECT
    (
        SELECT Id FROM UserLogin
        WHERE UserName =  42123 
    ) as UserLoginId,
    7 as [EntryNumber];

注:<代码>的主要关键内容 食物追踪器没有在插入中具体指明。 还注意到该次排序,使我们能够通过自由界定的扼杀手段,提及特定用户,同时在使用记录仪表上保留独一无二、自动加固的主要钥匙。 这常常是有用的,是一个建议的表格设计。

最后,我们可以询问与特定用户相关的跟踪信息,并简单地审视:

SELECT
    ul.UserName,
    ft.* 
FROM "Food Tracker" ft
    INNER JOIN UserLogin ul ON ft.UserLoginId = ul.Id
WHERE ul.UserName =  42123 ;

你们能否向我们展示更多的错误信息代码?





相关问题
Can Django models use MySQL functions?

Is there a way to force Django models to pass a field to a MySQL function every time the model data is read or loaded? To clarify what I mean in SQL, I want the Django model to produce something like ...

An enterprise scheduler for python (like quartz)

I am looking for an enterprise tasks scheduler for python, like quartz is for Java. Requirements: Persistent: if the process restarts or the machine restarts, then all the jobs must stay there and ...

How to remove unique, then duplicate dictionaries in a list?

Given the following list that contains some duplicate and some unique dictionaries, what is the best method to remove unique dictionaries first, then reduce the duplicate dictionaries to single ...

What is suggested seed value to use with random.seed()?

Simple enough question: I m using python random module to generate random integers. I want to know what is the suggested value to use with the random.seed() function? Currently I am letting this ...

How can I make the PyDev editor selectively ignore errors?

I m using PyDev under Eclipse to write some Jython code. I ve got numerous instances where I need to do something like this: import com.work.project.component.client.Interface.ISubInterface as ...

How do I profile `paster serve` s startup time?

Python s paster serve app.ini is taking longer than I would like to be ready for the first request. I know how to profile requests with middleware, but how do I profile the initialization time? I ...

Pragmatically adding give-aways/freebies to an online store

Our business currently has an online store and recently we ve been offering free specials to our customers. Right now, we simply display the special and give the buyer a notice stating we will add the ...

Converting Dictionary to List? [duplicate]

I m trying to convert a Python dictionary into a Python list, in order to perform some calculations. #My dictionary dict = {} dict[ Capital ]="London" dict[ Food ]="Fish&Chips" dict[ 2012 ]="...

热门标签