简体   繁体   中英

Python and sqlite3 - import text file into database

Thanks for your help. I am new to SQLite3 and was indeed mixing up the two. I have tried the code below but it still does not work.

import csv
import sqlite3

# Create the database
connection = sqlite3.connect('addresses.db')
cursor = connection.cursor()

# Create the table
cursor.execute('DROP TABLE IF EXISTS addresses')
cursor.execute('''CREATE TABLE addresses
             (ID text, Firstname text, Surname text, Address1 text, Address2 text, Postcode text)''')
connection.commit()

# Load the CSV file into CSV reader
csvfile = open('addresses.txt', 'r')
creader = csv.reader(csvfile, delimiter=',', quotechar='"')

# Iterate through the CSV reader, inserting values into the database
for t in creader:
    cursor.execute('INSERT INTO  addresses VALUES (?,?,?,?,?,?)', t )

# Close the csv file, commit changes, and close the connection
csvfile.close()
connection.commit()
connection.close()

I get the error
for t in creader: File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/encodings/ascii.py", line 26, in decode return codecs.ascii_decode(input, self.errors)[0] UnicodeDecodeError: 'ascii' codec can't decode byte 0xe2 in position 0: ordinal not in range(128)

You didn't show the error you are getting.

However, it seems that you are confused between sqlite3 and MySQL. These are two completely separate databases, yet your question title refers to "mysqlite3". You connect to a sqlite db file in your code, but then you try to run LOAD DATA which is a MySQL-specific command not supported by sqlite.

You will have to write some Python code to read the CSV file, iterate through, and insert each row to the database. The csv module will help.

As far as I know LOAD DATA LOCAL INFILE is not supported by sqlite.

You should parse the file line by line and generate the inserts or use the import command

https://www.sqlite.org/cli.html

search for .import

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM