如何将这个插入到我的桌子上?

2024-10-05 13:17:02 发布

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

创建我的表:

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

我试着插入这个:

    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)

数据库中没有插入。我错过了什么


Tags: executesqldatecreatetablenotintegerfloat
2条回答

您需要执行conn.commit()以查看数据库中的更改。引用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. 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.

您需要提交更改,以便它们在数据库中生效。 提交所有数据库操作,如update、insert

cursor.commit()

执行成功后。您可以返回cursor.execute。如果不是None,那么您可以尝试提交更改,否则请使用rollback(练习:),这样您就不会在数据库中更新错误的数据

相关问题 更多 >

    热门问题