我试图将一个整数,一个字符串和一个列表作为单个记录插入到数据库中,但是正在引发错误。TypeError:在使用参数插入时,并非所有在字符串格式化过程中转换的参数
这里是我的代码:
values=nprnd.randint(10, size=48) #NUMPY ARRAY
valuelist= map(None, values); #Convert to list
guid=''.join(random.choice(string.ascii_uppercase + string.digits) for x in range(20)) #Generate guid
for x in range(4):
var_string = ', '.join('?' * 48)
valuelist.insert(0,x)
valuelist.insert(0,ent_guid)
#50 coloums in table, guid, x and 48 randomly generated values
query_string = 'INSERT INTO schema1 VALUES (%s,%d,%s);' % (guid, x, var_string)
cursor.execute(query_string, valuelist)
我不断收到一个错误说:
Traceback (most recent call last):
File "script.py", line 19, in <module>
cursor.execute(query_string, valuelist)
File "/Library/Python/2.7/site-packages/MySQL_python-1.2.4b4-py2.7-macosx-10.8-intel.egg/MySQLdb/cursors.py", line 184, in execute
query = query % db.literal(args)
我知道这个错误的原因(即使如此,对相同的错误了几个问题)但没有我尝试了解决方案已经解决了这个错误,我
任何帮助,将不胜感激
如果你需要生成一个GUID,你应该真的使用UUID模块......它就是为了这个目的。我建议'从uuid导入uuid4; guid = uuid4()。hex' – Endophage 2013-04-23 15:10:23
与我目前的问题无关,但感谢您的提示! :) – navinpai 2013-04-23 15:11:21
@navinpal确实是这样,但看到我的答案为可能的解决方案:-) – Endophage 2013-04-23 15:14:15