在python的数据库插入操作中,当你想在双引号中间引用参数来执行语句时,可以使用下面的方法:
例:
# 加入学生
def addstudent(self, user, password, sno, name, sex, birthday, phone, dorm):
self.condatabase(self, user, password)
sql = "insert into student values" \
"(%s,%s,%s,%s,%s,%s)" % (repr(sno), repr(name), repr(sex), repr(birthday), repr(phone), repr(dorm))
try:
# 使用 execute() 方法执行 SQL
self.cursor.execute(sql)
self.db.commit()
except:
self.db.rollback()
# 关闭数据库连接
self.db.close()
sql = “insert into student values”
"(%s,%s,%s,%s,%s,%s)" % (repr(sno), repr(name), repr(sex), repr(birthday), repr(phone), repr(dorm))
这样就可以插入数据了
关于repr函数: