Write a Python function that accepts a URL to a CSV file, downloads it and loads it into…
- User
Write a Python function that accepts a URL to a CSV file, downloads it and loads it into a SQLite database, creating a table with the correct columns
Assistant
Here is a function that downloads a CSV file from a URL and loads it into a SQLite database:
import sqlite3 import csv import requests def load_csv_to_sqlite(csv_url, db_name, table_name): # Download CSV file contents response = requests.get(csv_url) response.raise_for_status() csv_data = response.text # Create SQLite database and connect conn = sqlite3.connect(db_name) c = conn.cursor() # Read first row and create table with column names reader = csv.reader(csv_data.splitlines()) headers = next(reader) column_defs = ', '.join([header + ' text' for header in headers]) c.execute(f'CREATE TABLE IF NOT EXISTS {table_name} ({column_defs})') # Insert rows into table rows = [tuple(row) for row in reader] c.executemany(f'INSERT INTO {table_name} VALUES ({", ".join(["?"] * len(headers))})', rows) conn.commit() conn.close()
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.