简体   繁体   English

如何将它插入我的桌子上?

[英]How can I insert this on my table?

Creating my table: 创建我的表:

 cursor.execute("""
   CREATE TABLE if not exists intraday_quote (
   id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
   symbol VARCHAR(10) NOT NULL,
   date DATE,
   time DATE,
   open FLOAT,
   high FLOAT,
   low FLOAT,
   close FLOAT,
   volume INTEGER);
   """)

and I`m trying to insert this: 我正在尝试插入此代码:

    conn = sqlite3.connect('intraday_quote.db')
    cursor = conn.cursor()
    # Prepare SQL query to INSERT a record into the database.
    sql = """INSERT INTO intraday_quote(symbol) VALUES ('Mac123432')"""
    cursor.execute(sql)

No insertion happened in the database. 没有插入发生在数据库中。 What I am missing? 我缺少什么?

You need to commit your changes so they can get into effect in database. 您需要提交更改,以便它们在数据库中生效。 commit all db operations like update, insert. 提交所有数据库操作,例如更新,插入。

cursor.commit()

after your execute is succeeded. 执行成功后。 You can get return of the cursor.execute . 您可以返回cursor.execute If it is not None then you can try committing the changes else use rollback (exercise for you :) ) so you wont end up with wrong data updated in database. 如果不是None那么您可以尝试提交更改,否则请使用rollback (适合您的运动:)),这样就不会导致数据库中错误的数据更新。

You need to do conn.commit() to see the changes in the database. 您需要执行conn.commit()才能查看数据库中的更改。 Quoting the documentation 引用文档

This method commits the current transaction. 此方法提交当前事务。 If you don't call this method, anything you did since the last call to commit() is not visible from other database connections. 如果不调用此方法,则从其他数据库连接中看不到自上次调用commit()以来所做的任何操作。 If you wonder why you don't see the data you've written to the database, please check you didn't forget to call this method. 如果您想知道为什么看不到已写入数据库的数据,请检查您是否没有忘记调用此方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM