Question: USE python write two functions 1. to_table: This function takes a csv filename, an SQLite filename, and a table name with default valuenew1. It adds

USE python write two functions

1. to_table: This function takes a csv filename, an SQLite filename, and a table name with default value"new1". It adds a new table with the specified name to the db that has the information from the csv file in it. The first row of the csv file contains the column names. All columns are TEXT type. The first column is the primary key. I recommend using Python's csv module so you don't have to parse the rows of the csv file yourself. HINT: look into using qmark notation with the sqlite3 .execute() method, ex:

c.exectue(INSERT INTO

VALUES (?, ?), (var1, var2))

2. to_csv: This function takes an SQLite filename, a table name, and a csv filename with default value"data.csv". It takes the data from the table and writes the information in it to the csv file. You can and should look at the Python documentation for the sqlite3 module, but here is some code for you to enter that shows briefly how you can get the column names for the first row of the csv file:

>>> import sqlite3 >>> conn = sqlite3.connect('classes_redacted.db') >>> sqlite3.row_factory = sqlite3.Row >>> c = conn.cursor() >>> c.execute('SELECT * FROM ista_350_s18;') >>> c.description

This will get you the information you need in something called a tuple of tuples. You can deal with it like you would a list of lists you can traverse it with a for loop, index into it, etc. Or you could get all of the table schema information as demonstrated here and work with that.

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!