Am trying to add the excel sheet datas into the database.
This the code am using to add the whole excel sheet values to the db.
from xlrd import cellname, cellnameabs, colname ,open_workbook
import sqlite3
book = open_workbook("Cases.xls")
s=book.sheet_by_index(0)
conn = sqlite3.connect('test.db')
c = conn.cursor()
c.execute('''CREATE TABLE exceltable (
sno TEXT,
TID TEXT,
Description TEXT,
OB TEXT,
IP TEXT,
OP TEXT,
RUN TEXT
)''')
for row in range(s.nrows):
for col in range(s.ncols):
Exceldata.append(s.cell(row,col).value)
print Exceldata
c.execute('INSERT INTO exceltable VALUES (?,?,?,?,?,?,?)', values)
c.commit()
In Exceldata am having all the values of excel sheet but trying to
split in the loop and adding but it gives an error to me.
> Am trying to add the excel sheet datas into the database.
> This the code am using to add the whole excel sheet values to the db.
> from xlrd import cellname, cellnameabs, colname ,open_workbook > import sqlite3 > book = open_workbook("Cases.xls") > s=book.sheet_by_index(0) > conn = sqlite3.connect('test.db') > c = conn.cursor() > c.execute('''CREATE TABLE exceltable ( > sno TEXT, > TID TEXT, > Description TEXT, > OB TEXT, > IP TEXT, > OP TEXT, > RUN TEXT > )''') > for row in range(s.nrows):
> for col in range(s.ncols): > Exceldata.append(s.cell(row,col).value) > print Exceldata > c.execute('INSERT INTO exceltable VALUES (?,?,?,?,?,?,?)', values) > c.commit()
> In Exceldata am having all the values of excel sheet
Sorry to have to contradict you, but in Exceldata are having absolutely nothing -- you haven't initialised Exceldata, so your program will abort the first time it tries to do Exceldata.append(something)
Next two problems are that you are using "values" in the c.execute('INSERT ...') (a) instead of "Exceldata" (b) uninitialised
Fourth problem is that as you are writing one Excel row as one database row, you don't need "all the values of excel sheet"; you need only the values in one row at a time.
I suggest that you replace your value-writing code with something simple, like this:
for row_index in xrange(s.nrows): values = s.row_values(row_index) print row_index, values c.execute('INSERT INTO exceltable VALUES (?,?,?,?,?,?,?)', values)
> but trying to > split in the loop and adding
What does that mean???
> but it gives an error to me.
I'm not surprised. Are the text of the error message and the traceback a state secret? If you get a problem with the next version of your code, please supply (1) the code that you ran (2) the traceback (3) the error message and don't type any of that from memory; use copy/paste.