mardi 8 décembre 2015

Python: Creating database (SQLite), populating table using a text file and then use User input to search database by last name

The title explains the steps I need to do. The problem is that I can't get the data from the text file to write to the database. The text file is organized line by line ex. John Doe 012-345-6789 As you can see, I've split the text and have all the categories straightened out. But keep getting errors like "InterfaceError: Error binding parameter 0 - probably un supported type." or something about me writing 47 entries to one field. I'll try to figure out the searching if I could get just get an idea of what I am missing. Thank you very much in advance (this is my first post. I apologize if I'm breaking any rules or bad formatting)

import sqlite3

conn = sqlite3.connect('phoneNumbers.db')
cur = conn.cursor()

cur.execute('DROP TABLE IF EXISTS People ')
cur.execute('CREATE TABLE People (firstname TEXT, lastname TEXT, phonenumber TEXT)')


########

file = open('phoneNumbers.txt', 'r')

try:
    file = open('phoneNumbers.txt', 'r')
except:
    print "File not found"

data = file.read()
data = data.split()

for line in data:
    first = data[0::3]
    last = data [1::3]
    num = data [2::3]


cur.execute('INSERT INTO People (firstname, lastname, phonenumber) VALUES (?, ?, ?)', (first, last, num))
conn.commit()

cur.execute('SELECT firstname, lastname, phonenumber FROM People')
for row in cur:
    print row

conn.close()

Aucun commentaire:

Enregistrer un commentaire