python 插库问题?
从DB里 select出 一个字段内容,然后赋值一个变量如 tele.tele内容如:
00876123456
02095539
139012340000
无
无
insert into test(phone) values('%s') % (tele) 这种不方不对,只能插入tele 中的一条数据。
QA:
1、现在问题是怎样把tele中的所有内容都插入到 test数据库的phone 字段里?
2、类似tele 有多个字段,都有好几行内容,都要插到test数据库相对应的字段上,有没有好点的方法? result 为搜索出的结果,是个元组for a,b,c,d in result:
T =(a,b,c,d)
cursor.execute('insert into test(A,B,C,D) values('%s,%s,%s,%s')',T) 本帖最后由 ppjer 于 2014-09-12 16:39 编辑
回复 2# huangxiaohen
但现在内容是以下形式,print type(tele)--><type 'unicode'>
tele内容如:
00876123456
02095539
139012340000
无
无
for i in tele --> 这种遍历是不对的。
而且tele 每天 记录数还不一样,有时5条,有 3条,用这种方式 for a,b,c,d in tele把变量写死行不通吧。 你可能没理解,a,b,c,d是字段的值,你select出来的, 而tele 一定是元组,你用cursor.execute('select')fetchall 一下 结果肯定是元组 这是没错的. 你的 tele 变量的值是 str 类型么? 还是元组类型or其他类型?>>> tele = '''00876123456
... 02095539
... 139012340000
... 无
... 无'''
>>>
>>>
>>> print tele
00876123456
02095539
139012340000
无
无
>>>
>>>
>>> print "insert into test(phone) values('%s')" % (tele)
insert into test(phone) values('00876123456
02095539
139012340000
无
无')
>>>
>>>
>>> tele = u'''00876123456
... 02095539
... 139012340000
... 无
... 无'''
>>> print type(tele)
<type 'unicode'>
>>>
>>>
>>> print "insert into test(phone) values('%s')" % (tele)
insert into test(phone) values('00876123456
02095539
139012340000
无
无')
>>> 回复 6# reyleon
是这种类型的 str
<type 'unicode'> 回复 4# huangxiaohen
这个值不是固定的,每天可能不一样,所以这种写法不太合理。
页:
[1]