我尝试使用方法'insert_users'将一些信息添加到db表中。与变量self.tests2连接到INSERT查询相关的问题,当我尝试这样做时,我有:TypeError:并非在字符串格式化过程中转换所有参数。当我尝试插入而不是'self.test2'字符串'%s','%s','%s','%s','%s','%s','%s','% '一切正常。怎么了?
def insert_users(self, users_dict, rows_names):
self.test1 = len(rows_names) * ["%s"]
print self.test1
self.test2 = "'" + "','".join(self.test1) + "'"
self.cursor = self.connection.cursor()
for name, val in users_tuple.items():
self.cursor.execute("""INSERT INTO users(""" + \
",".join(rows_names) + """) VALUES(""" + **self.test2** + """)""" %\
(name, val, 'SomeNickname', 'password', '13-09-11', '2', '@gmail.com','1'))
ConnectToDb.connection_cot_cle(self)
users_dict = {'FirstName':'LastName'}
rows_names = ('Fname', 'Lname', 'login', 'password','reg_date', 'role_id',\
'email', 'is_active')
db_instans.insert_users(users_dict, rows_names)
答案 0 :(得分:1)
使用临时变量来保存您的字符串,如:
temp = "INSER INTO users(" + ",".join(rows_names) + ") VALUES(" + test2 + ")"
temp = temp % (name, val, 'SomeNickname', 'password', '13-09-11', '2', '@gmail.com','1')
然后将数据插入其中
或在
周围添加其他括号("INSER INTO users(" + ",".join(rows_names) + ") VALUES(" + test2 + ")")
它看起来像
temp = ("INSER INTO users(" + ",".join(rows_names) + ") VALUES(" + test2 + ")") % (name, val, 'SomeNickname', 'password', '13-09-11', '2', '@gmail.com','1')
问题是你没有完整的字符串,并希望将数据插入“)”