【问题标题】:Python coding for SQLite is not populating the DatabaseSQLite 的 Python 编码未填充数据库
【发布时间】:2020-02-17 01:19:12
【问题描述】:

我在网上找到了一个教程,它可以让用户在命令行中输入电影,它会搜索 IMDB 并返回所有信息。那是有效的。

所以我决定我希望能够将电影数据导入 SQLite 数据库。正在使用表生成数据库,但没有获取信息,我也没有收到错误消息。这是我所拥有的。

这是 SQLite 代码:

import sqlite3
from sqlite3 import Error
def sql_connection():
    try:
        con=sqlite3.connect('movies.db')
        return con
    except Error:
        print(Error)

def sql_table(con):
    cursorObj=con.cursor()
    cursorObj.execute("CREATE TABLE movies(id integer PRIMARY KEY,\ 
    tite text, year integer, full cast text, rating integer, plot \
    text )")

    con.commit()
con=sql_connection()
sql_table(con)

def sql_insert(con,entities):
    cursorObj=con.cursor()
    cursorObj.execute('INSERT INTO movies(id, title, year of \
    release, full cast, rating, cast) VALUES(?,?,?,?,?,?)',entities)\
    con.commit()
sql_insert(con,entities)
con.close()

【问题讨论】:

  • sql_insert(con,entities) 中的entities 是什么?你有任何错误吗?请阅读How to Ask
  • 不确定实体。我正在关注一个教程。不过,我没有收到任何错误。

标签: python sqlite imdb


【解决方案1】:

我看到了错误:

错字:tite 而不是title in:

cursorObj.execute("CREATE TABLE movies(id integer PRIMARY KEY,\ 
tite text, year integer, full cast text, rating integer, plot \
text )")

在第二个语句中,您尝试更新不存在的字段 year of release(您之前将其命名为 year)。

cursorObj.execute('INSERT INTO movies(id, title, year of \
release, full cast, rating, cast) VALUES(?,?,?,?,?,?)',entities)\
con.commit()

如果您要使用包含空格的列名,则需要反引号或方括号,这是我会避免的。因此,我将使用下划线字符(_)代替full cast 来避免空格:full_cast。但是在您的 create 语句中,您将字段称为 cast,因此插入不起作用。

【讨论】:

  • 感谢您的快速回复。我做了你建议的更正。我修正了拼写错误,我用了分数作为名字,并称之为演员表,全演员表。我还注意到插页上有两个演员表。我把最后一个演员改成情节,这就是它应该的样子。但我仍然得到一个空数据库。它制作了桌子,就是这样。谢谢你的帮助。
猜你喜欢
  • 1970-01-01
  • 2023-03-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-03-19
  • 2018-03-20
  • 2017-07-15
相关资源
最近更新 更多