So I have a created table as shown below (just with more columns that are too long to paste in here):
cur.execute("""CREATE TABLE IF NOT EXISTS Income (
SS_ID INTEGER PRIMARY KEY AUTOINCREMENT,
age INTEGER,
class_of_worker TEXT,
detailed_industry_code TEXT,
eductation TEXT,
The issue I am having is inserting the data from a large database (about 200,000 recrods) that is in the format as shown below (but with many more columns):
73, Not in universe, 0, 0, High school graduate, 0, Not in universe, Widowed,
#with 73 corresponding to age and so on
I am currently trying to import the data with the following:
with open('census-income.data', 'r') as data:
no_records = 0
for row in data:
cur.execute("INSERT INTO Income VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)",
row.split(","))
census_income.commit()
no_records += 1
census_income.close()
print('\n{} Records Transfered'.format(no_records))
However this does not work as it tries import age (the first column in the data) into SS_ID (the first column in the table which is the primary key and has autoincrement for IDing records) and I get the following error:
Exception has occurred: IntegrityError
UNIQUE constraint failed: Income.SS_ID
Is there a way to import the data either by just importing specific columns of data into specific columns of the table, or by skipping the first column (SS_ID PRIMARY KEY) when importing all the data?
Many thanks for any help, I have been struggling with this for a while.
You must enumerate the columns that will receive the values, something like:
INSERT INTO Income (age, class_of_worker, detailed_industry_code, eductation, ....) VALUES(?,?,....)
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.