mercredi 30 mars 2016

How to extract data from a csv file and put it to a sqlite database?

I am trying to use this code to extract data from a big csv file and insert into a database. The schema of the
database is provided in the code. However, I am doing something wrong in the last line. The code is giving me value error. I am using pandas to read the csv file. Could someone help me point out where am I going wrong ?

import pandas as pd
import sqlite3

conn = sqlite3.connect('newdb.sqlite')
cur = conn.cursor()
cur.executescript('''
DROP TABLE IF EXISTS Policy;
DROP TABLE IF EXISTS Statecode;
DROP TABLE IF EXISTS County;
DROP TABLE IF EXISTS Line;
DROP TABLE IF EXISTS Construction;
DROP TABLE IF EXISTS Point_Granularity;

CREATE TABLE Statecode (
    id  INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT UNIQUE,
    name    TEXT UNIQUE
);

CREATE TABLE County (
id  INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT UNIQUE,

name   TEXT UNIQUE
);
CREATE TABLE Line(
id INTEGER NOT NULL PRIMARY KEY
    AUTOINCREMENT UNIQUE,
name TEXT UNIQUE);
CREATE TABLE Construction(
id INTEGER NOT NULL PRIMARY KEY
    AUTOINCREMENT UNIQUE,
name TEXT UNIQUE);
CREATE TABLE Point_Granularity(
id INTEGER NOT NULL PRIMARY KEY
    AUTOINCREMENT UNIQUE,
number INTEGER UNIQUE);

CREATE TABLE Policy (
id  INTEGER NOT NULL PRIMARY KEY
    AUTOINCREMENT UNIQUE,
policyID INTEGER  ,
eq_site_line  FLOAT,
hu_site_line INTEGER,

statecode_id INTEGER,
county_id INTEGER,
line_id INTEGER,
construction_id INTEGER,
point_granularity_id INTEGER
);
''')
df = pd.read_csv('FL_insurance_sample.csv')
for policy in df.policyID:
    cur.execute('INSERT INTO Policy (policyID) VALUES (?)',policy)
    conn.commit()

Aucun commentaire:

Enregistrer un commentaire