python - Migrate all data from excell to sqlite? -
l m fresh user of python.l m working on sql.l migrate data excel file sql. here code:
import xlrd import sqlite3 conn=sqlite3.connect("student.db") crs=conn.cursor() file_path="student.xlsx" file=xlrd.open_workbook(file_path) sheet=file.sheet_by_index(0) data=[[sheet.cell_value(r,c)for c in range(sheet.ncols)] r in range(sheet.nrows)] in range(sheet.ncols): colm=data[0][i] crs.execute(("create table if not exists student([%s] varchar(100))")%colm) firstly, l tried create tables using column in excel file l not it. after , l insert each row in columns. probably, l m doing wrong somewhere l not figure out.
why dont add whole table sqlite database?
conn = sqlite3.connect('student.db') cur = conn.cursor() file = pd.dataframe("student.xlsx") #assuming import pd dataframe? may want include path of file if not in working directory 'yourtablename'.to_sql("file", conn, if_exists="replace") conn.commit() 'yourtablename' should replaced whatever name want give table in database. can use below code verify table in database
con = sqlite3.connect('student.db') cur = con.cursor() cur.execute("select name sqlite_master type='table' order name;") cur.fetchall()
Comments
Post a Comment