Question: Please, can anybody help me? The method will first create a connection to SQLite and then utilize a context manager to handle its lifetime: conn

Please, can anybody help me?

  • The method will first create a connection to SQLite and then utilize a context manager to

    handle its lifetime:

  • conn = sqlite3.connect(db_file) with conn: 

    ### The rest of the code will go here ###

  • Next, create a SQL command string to create the people table, open a cursor and then execute the command (the triple quotes are important for line continuations):

    sql_create_people_table = """ CREATE TABLE IF NOT EXISTS people ( id integer PRIMARY KEY, first_name text NOT NULL, last_name text NOT NULL); """

    cursor = conn.cursor() cursor.execute(sql_create_people_table)

  • The next step is to truncate any existing data from the table with the following commands: sql_truncate_people = "DELETE FROM people;"

    cursor.execute(sql_truncate_people)

  • Next, generate a list of person tuples by calling the generate_people() function created in the previous step:

  • people = generate_people(count) 

    Finally, create the query to add the people records and use a for loop to execute the statement:

    sql_insert_person = "INSERT INTO people(id,first_name,last_name) VALUES(?,?,?);"

    for person in people: #print(person) # uncomment if you want to see the person object cursor.execute(sql_insert_person, person) #print(cursor.lastrowid) # uncomment if you want to see the row id

    cursor.close() 

    Now, back in the __main__ section, execute the create_people_database() function:create_people_database(people_db_file, max_people)

    There will be no direct output from this method, but you can check the results by using a third party database UI, like DBeaver (https://dbeaver.io/).

This is may actually code:

import random from concurrent.futures import ThreadPoolExecutor

import sqlite3 from sqlite3 import Error

people_db_file = "sqlite.db" # The name of the database file to use max_people = 500 # Number of records to create

last_names = [] first_names = []

def read_name(filename): data = list() with open(filename, 'r') as filehandle: dataname = [line.rstrip() for line in filehandle] return dataname

def generate_people(count): with ThreadPoolExecutor(max_workers = 2) as executor: futureA = executor.submit(read_name, 'LastNames.txt') futureB = executor.submit(read_name, 'FirstNames.txt') last_names = futureA.result() first_names = futureB.result()

names = list()

for i in range(count): names.append((i, first_names[random.randint(0, len(first_names)-1)], last_names[random.randint(0, len(last_names)-1)])) return names

def create_people_database(db_file, count): conn = sqlite3.connect(db_file)

#--------->

# with conn:

if __name__ == "__main__": people = generate_people(5) print(people)

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!