0

我尝试使用方法“insert_users”向数据库表添加一些信息。与变量 self.tests2 连接到 INSERT 查询相关的问题,当我尝试这样做时,我有:TypeError:并非所有参数都在字符串格式化期间转换。当我尝试插入而不是 'self.test2' 字符串 '%s','%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)
4

1 回答 1

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')

问题是您没有完整的字符串,并且想将数据插入“)”

于 2013-09-13T20:13:14.723 回答