Python Forum

Full Version: Setting up data base
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
good day, i got an assignment but i don't understand parts of it, because I never makes a DB like this in java.
I created the DB and gave the columns a name, and now I get did as an assignment.

Question 1

def insert_into_database(cursor, data):
    """
    Load all selected data from the CSV to the database.

    """
    ### FILL IN - START ###
    # Add your column names as (col1, col2, ..., col_n)
    column_names = "(col1,col2 )"
    # Add empty values as (?, ?, ..., ?) with the same amount of ? as column names.
    values = "(?, ?, )"
    # Fill this list with tuples of values from the columns.
    # Each tuple represents one row.
    # (value1, value2, ..., value_n)
    # Example: ("Avatar", "James Cameron", 2009-12-17, 162, 237000000, 760507625.0)
    insert_into_db = []
    ### FILL IN - END ###
    cursor.executemany("""INSERT INTO movies {} VALUES {};""".format(column_names, values), insert_into_db)
what I understand I have to enter the column names again only shortened because you can only use 166 charter in that line, and the next step is to add the value

what do they want me to do specifically?

Question 2

if __name__ == "__main__":
    data = None
    with open(DATA_PATH) as data_file:
        data = csv.reader(data_file, delimiter=";")

        connection = sqlite3.connect(DATABASE_PATH)
        cursor = connection.cursor()

        create_database(cursor)
        insert_into_database(cursor, data)

        ### FILL IN - START ###
        # Create two functions similar to create_database and insert_into_database.
        # Call those functions here.
        # This time, you will create an actors table and fill it with actor data from actor_dataset.csv
        # Note that this will just be added to the existing database movies.db
        ### FILL IN - END ###

        connection.commit()
        connection.close()
if I understand they want me to repeat the previous steps to create the other two database tables?
(Jul-21-2020, 10:14 AM)vgywer Wrote: [ -> ]what I understand I have to enter the column names again only shortened because you can only use 166 charter in that line

What? Where does that limit come from?

Quote:what do they want me to do specifically?

The goal of the function is described in the docstring in line 3 and the comments in the rest of the function pretty much lead you through the task. Surely you've seen these things before in your class?
I have 2 CSV files one has 16 columns the other one 11 columns
I am fixing only the first csv file now


this is the full conde page

import csv
from pathlib import Path
import os
import sqlite3

DIRECTORY = os.getcwd() + "/"
DATA_PATH = Path(DIRECTORY) / "data/selected_data.csv"
DATABASE_PATH = Path(DIRECTORY) / "movies.db"


def create_database(cursor):
    """
    Create a Sqlite database to store movie data.
    Fill in all data columns that you selected.
    """
    # Delete table if it exists.
    cursor.execute("""DROP TABLE IF EXISTS movies;""")

    ### FILL IN - START ###
    # Add your columns to the SQL table, separated by commas.
    # This can be done after the line
    # id INTEGER PRIMARY KEY
    sql_command = """
        CREATE TABLE movies (
        id INTEGER PRIMARY KEY,
        movie_title text,
        director_name text,
        summary	text,
        genres text,
        release_date real,	
        duration integer,
        budget real, 	
        opening_weekend_usa real, 	
        gross_usa real, 	
        gross_worldwide real, 	
        imdb_score real, 	
        num_critic_for_reviews real, 	
        num_voted_users	real, 
        metascore integer,	
        oscar_wins integer,	
        oscar_nominees integer
        );"""
    ### FILL IN - END ###

    cursor.execute(sql_command)


def insert_into_database(cursor, data):
    """
    Load all selected data from the CSV to the database.

    """
    ### FILL IN - START ###
    # Add your column names as (col1, col2, ..., col_n)
    column_names = "(col1,col2,col3,col4,col5,col6,col7,col8,col9,col10,col11,col12,col13,col14,col15)"
    # Add empty values as (?, ?, ..., ?) with the same amount of ? as column names.
    values = "(Avatar, James Cameron, A paraplegic marine dispatched to the moon Pandora on a unique mission becomes torn between following his orders and protecting the world he feels is his home.,Action | Adventure | Fantasy | Sci-Fi,17-12-2009,162, 237000000.0,77025481.0,760507625.0,2787965087.0,7.8,727.0,1038709.0,83,3,9)"
    # Fill this list with tuples of values from the columns.
    # Each tuple represents one row.
    # (value1, value2, ..., value_n)
    # Example: ("Avatar", "James Cameron", 2009-12-17, 162, 237000000, 760507625.0)
    insert_into_db = []
    ### FILL IN - END ###
    cursor.executemany("""INSERT INTO movies {} VALUES {};""".format(column_names, values), insert_into_db)


if __name__ == "__main__":
    data = None
    with open(DATA_PATH) as data_file:
        data = csv.reader(data_file, delimiter=";")

        connection = sqlite3.connect(DATABASE_PATH)
        cursor = connection.cursor()

        create_database(cursor)
        insert_into_database(cursor, data)

        ### FILL IN - START ###
        # Create two functions similar to create_database and insert_into_database.
        # Call those functions here.
        # This time, you will create an actors table and fill it with actor data from actor_dataset.csv
        # Note that this will just be added to the existing database movies.db
        ### FILL IN - END ###

        connection.commit()
        connection.close()
what am I doing wrong?
Why do you think you're doing something wrong?
it gives me a error at 70 "Redeclared 'data'defined above without usage"

and i don't get why ore wat i missed
That doesn't sound like an error from Python and the program should still run. In any case, you don't need line 68 since you never use the value you assign there before redesigning to data on line 70.