import sqlite3
import csv
def create_database(csv_file, db_file, table_name):
# Connect to the SQLite database
connection = sqlite3.connect(db_file)
cursor = connection.cursor()
# Create a table in the database
cursor.execute(f'''CREATE TABLE IF NOT EXISTS {table_name} (
column1 TEXT,
column2 INTEGER,
column3 REAL
)''')
# Read data from the CSV file and insert into the table
with open(csv_file, 'r') as file:
csv_reader = csv.reader(file)
next(csv_reader) # Skip header row if it exists
for row in csv_reader:
cursor.execute(f'INSERT INTO {table_name} VALUES (?, ?, ?)', row)
# Commit changes and close the connection
connection.commit()
connection.close()
if __name__ == "__main__":
csv_input_file = 'input_data.csv'
database_file = 'output_database.db'
table_name = 'my_table'
create_database(csv_input_file, database_file, table_name)
import sqlite3
def create_database(input_file, database_file):
# Connect to the SQLite database file
conn = sqlite3.connect(database_file)
cursor = conn.cursor()
# Create a table in the database
cursor.execute('''
CREATE TABLE IF NOT EXISTS my_table (
id INTEGER PRIMARY KEY AUTOINCREMENT,
data TEXT
)
''')
# Read data from the input text file and insert it into the database
with open(input_file, 'r') as file:
for line in file:
data = line.strip() # Assuming each line in the text file is a piece of data
cursor.execute('INSERT INTO my_table (data) VALUES (?)', (data,))
# Commit the changes and close the connection
conn.commit()
conn.close()
if __name__ == "__main__":
input_file = "input.txt" # Replace with the actual path to your input text file
database_file = "my_database.db" # Replace with the desired name for your database file
create_database(input_file, database_file)