首页 > 解决方案 > IMDB 到 SQLite:如何创建表并将 imdb 数据插入 sqlite 数据库?

问题描述

我正在做一个项目,我想要从这里下载到 sqlite 数据库的选择 IMDB 文件(文件格式在 *.list 中)。不幸的是,我无法解决这个问题。我可以创建一个数据库,但不能用 IMDB 数据制作表格。

我一直在关注的文档是here。到目前为止,我创建了一个 sqlite 表,但它不会填充它。

import sqlite3
from sqlite3 import Error

def create_connection(db_file):
    """ create a database connection to the SQLite database
        specified by db_file
    :param db_file: database file
    :return: Connection object or None
    """
    try:
        conn = sqlite3.connect(db_file)
        return conn
    except Error as e:
        print(e)

    return None

def create_table(conn, create_table_sql):
    """    create a table from the create_table_sql statement
    :param conn: Connection object
    :param create_table_sql: a CREATE TABLE statement
    :return:

    """
    try:
        c = conn.cursor()
        c.execute(create_table_sql)
    except Error as e:
        print(e)

def main():
    database = "/Users/Erudition/Desktop/imdb_database/sqldatabase.db"

    sql_create_tile_akas = """ CREATE TABLE IF NOT EXISTS title (
                                        titleid text PRIMARY KEY,
                                        ordering integer NOT NULL,
                                        title text,
                                        region text,
                                        language text NOT NULL,
                                        types text NOT NULL,
                                        attributes text NOT NULL,
                                        isOriginalTitle integer NOT NULL

                                    ); """

    conn = create_connection(database)
    if conn is not None:
        # create projects table
        create_table(conn, sql_create_tile_akas)

    else:
        print("Error! cannot create the database connection.")

if __name__ == '__main__':
      main()

在终端中,我输入

 imdbpy2sql.py -d /Users/Erudition/Desktop/imdb_database/aka-titles.list/  
 -u sqlite:///sqldatabase.db'''

我期望的输出是一个填充了所有行的 sqlite 表。相反,我得到了几个没有填写任何内容的 sqlite 表。

终端输出为:

WARNING The file will be skipped, and the contained
WARNING information will NOT be stored in the database.
WARNING Complete error:  [Errno 20] Not a directory: 
'/Users/Erudition/Desktop/imdb_database/aka-titles.list/complete- 
cast.list.gz'
WARNING WARNING WARNING
WARNING unable to read the "/Users/Erudition/Desktop/imdb_database/aka- 
titles.list/complete-crew.list.gz" file.
WARNING The file will be skipped, and the contained
WARNING information will NOT be stored in the database.
WARNING Complete error:  [Errno 20] Not a directory: 
'/Users/Erudition/Desktop/imdb_database/aka-titles.list/complete- 
crew.list.gz'

标签: databasesqliteimdbpy

解决方案


我找到了解决方案!

pip install imdb-sqlite

然后

imdb-sqlite

这是链接


推荐阅读