python读取excel并写入数据库-python 读取excel表格并写入sqllite数据库 | 学步园

近日想把excel的电影目录,写入sqllite数据库

方便以后查询,添加等 用到了xlrd

下面是第一部分 创建数据库表

#1080P目录.xls excel文件

#_*_ coding:utf-8 _*_

import os,sys,sqlite3,string

from xlrd import open_workbook

cx = sqlite3.connect("myMovie.db")

cu = cx.cursor()

cu.execute("""create table movieBook (id integer primary key,

numMovie integer,

chineseName text,

size real,

enName,

typeMovie,

pubYear,

soundChanl,

movieScore real,

movieActor text,

movieDirector text,

summary text

)""")

excelName = "1080P目录.xls"

bk = open_workbook(excelName,encoding_override="utf-8")

for s in bk.sheets():

print "sheet:",s.name

try:

sh = bk.sheet_by_name("1080PRip")

except:

print "no sheet in %s named sheet1" % excelName

nrows = sh.nrows-16

ncols = sh.ncols

print os.getcwd(),"nrows %d,ncols %d" % (nrows,ncols)

#cell_value = sh.cell_value(0,12)

print sh.cell_value(3163,1)

row_list =[]

print "开始导入------------------->>>>>>>"

for i in xrange(1,nrows):

row_date = sh.row_values(i)

row_list.append(row_date)

n = i-1

cx.execute("insert into movieBook(numMovie,chineseName,size,enName,typeMovie,pubYear,soundChanl,movieScore,movieActor,movieDirector,summary)values (?,?,?,?,?,?,?,?,?,?,?)",

(int(row_list[n][0]),row_list[n][1],row_list[n][2],row_list[n][3],row_list[n][4],row_list[n][5],row_list[n][6],row_list[n][7],row_list[n][8],row_list[n][9],row_list[n][10]))

cx.commit()

#print row_list[n][4]

cu.close()

print "导入完成------------------->>>>>>>"

#print len(row_list[3])

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值