python - Import .csv to SQL -


i trying use csv in order fill 34 columns sql database using python, though can't.

import csv sqlite3 con = sqlite3.connect(":memory:") cur = con.cursor()  cur.execute("create table t (no, source, host, link, date, time, time2, category, authorid, authorname, authorurl, auth, followers, following, age, gender, language, country, province, city, location, sentiment, title, snippet, description, tags, contents, view, comments, rating, favourites, duration, bio, uniqueid);")}  open('database.csv', 'rb') fin:     dr = csv.reader(fin)      dicts = ({'no': line[0], 'source': line[1], 'host': line[2], 'link': line[3], 'date': line[4], 'time': line[5], 'time2': line[6], 'category': line[7], 'authorid': line[8], 'authorname': line[9], 'authorurl': line[10], 'auth': line[11], 'followers': line[12], 'following': line[13], 'age': line[14], 'gender': line[15], 'language': line[16], 'country': line[17], 'province': line[18], 'city': line[19], 'location': line[20], 'sentiment': line[21], 'title': line[22], 'snippet': line[23], 'description': line[24], 'tags': line[25], 'contents': line[26], 'view': line[27], 'comments': line[28], 'rating': line[29], 'favourites': line[30], 'duration': line[31], 'following': line[32], 'uniqueid': line[33]} line in dr)     to_db = ((i['no'], i['source'], i['host'], i['link'], i['date'], i['time'], i['time2'], i['category'], i['authorid'], i['authorname'], i['authorurl'], i['auth'], i['followers'], i['following'], i['age'], i['gender'], i['language'], i['country'], i['province'], i['city'], i['location'], i['sentiment'], i['title'], i['snippet'], i['description'], i['tags'], i['contents'], i['view'], i['comments'], i['rating'], i['favourites'], i['duration'], i['bio'], i['uniqueid']) in dicts)  cur.executemany("insert t values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)", to_db) con.commit() 

i've been following many indications, although first time pythoning , don't know how that.

could please me this? lot in advanced.

pd: in case not inferable, csv file without header, , trying fill column column @ once.

if csv elements positionally correct, can not more straight forward i.e. example following data

1,2,3 a,b,c 

use following;

import sqlite3 con = sqlite3.connect(":memory:") cur = con.cursor()  cur.execute("create table t (col1,col2,col3);")  open('database.csv', 'rb') fp:     line in fp.readlines():         cur.execute("insert t values (?, ?, ?)",line.strip().split(',')) con.commit()  row in cur.execute("select * t;"):     print row 

Comments

Popular posts from this blog

android - Pass an Serializable object in AIDL -

How to provide Authorization & Authentication using Asp.net, C#? -

How to use Authorization & Authentication in Asp.net, C#? -