Sqlalchemy Sqlite 自动增量未按预期运行

发布于 2025-01-07 22:32:11 字数 891 浏览 0 评论 0原文

我在让我的脚本以正确的方式增加我的 PK 时遇到一些麻烦。按照 sqlalchemy 文档,必须完成一些特殊配置才能使其与 sqlite 一起使用。这是我的脚本:

def db_stuff():
    engine = create_engine('sqlite:///test.db', echo=True)
    metadata = MetaData()
    db = Table('users', metadata,
       Column('id', Integer, primary_key=True),
       Column('name', String),
       Column('fullname', String),
       Column('password', String),
       sqlite_autoincrement=True)
    metadata.create_all(engine) 
    return engine.connect(),db


def add_to_db():
    ret = db_stuff()
    conn = ret[0]
    db = ret[1]
    try:

        conn.execute("INSERT INTO users VALUES ('1','john','smith john','23')")
        result = conn.execute(db.select())
        for row in result:
            print row 
    finally:
        conn.close()

如果你能帮我弄清楚我在这里缺少什么,那就太酷了,我开始绝望了...... 问题是“id”并不是每次都会增加,并且当我运行脚本两次时,我收到一个错误,它应该是唯一的。

TIA

I have some trouble making my script incrementing my PK in a correct way. Following the sqlalchemy documentation some special configuration has to be done in order to make it work with sqlite. Here is my script:

def db_stuff():
    engine = create_engine('sqlite:///test.db', echo=True)
    metadata = MetaData()
    db = Table('users', metadata,
       Column('id', Integer, primary_key=True),
       Column('name', String),
       Column('fullname', String),
       Column('password', String),
       sqlite_autoincrement=True)
    metadata.create_all(engine) 
    return engine.connect(),db


def add_to_db():
    ret = db_stuff()
    conn = ret[0]
    db = ret[1]
    try:

        conn.execute("INSERT INTO users VALUES ('1','john','smith john','23')")
        result = conn.execute(db.select())
        for row in result:
            print row 
    finally:
        conn.close()

It would be cool if you could help me figuring out what I'm missing here, I start to be desperate...
The problem is that the "id" is not incremented each time and i get an error that it should be unique when I run the script twice.

TIA

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

中二柚 2025-01-14 22:32:11

try

conn.execute("INSERT INTO users(name, fullname, password) VALUES ('john','smith john','23')")

id 是自动增量的,因此我们不应该传递它,但是我们需要指定表中代表的其他参数,即值('john','smith john','23')应该放在哪里。

它应该有效。

try

conn.execute("INSERT INTO users(name, fullname, password) VALUES ('john','smith john','23')")

id is autoincrement hence we should not pass it, however we need to specify what other parameters represent in the table i.e. where should the values ('john', 'smith john', '23') should go.

It should work.

燃情 2025-01-14 22:32:11

执行此操作:

conn.execute("INSERT INTO users VALUES ('john','smith john','23')")

您将 id 设置为 1 - 始终。只要保留它,它就会因自动增量而被填充。

Do this:

conn.execute("INSERT INTO users VALUES ('john','smith john','23')")

You are setting the id to 1 - always. Just leave it and it will be filled due auto-increment.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文