Cherrypy Post命令增加mysql自动增量,但不插入数据库
我正在开发我的第一个web应用程序,我遇到了mysql的问题。我尝试使用下面的POST函数插入mysql数据库。发生的情况是,在我应用的表中,自动增量值递增,但没有插入任何内容到表中。我可以使用以下方法在ec2实例中插入到表中:Cherrypy Post命令增加mysql自动增量,但不插入数据库,mysql,amazon-ec2,cherrypy,Mysql,Amazon Ec2,Cherrypy,我正在开发我的第一个web应用程序,我遇到了mysql的问题。我尝试使用下面的POST函数插入mysql数据库。发生的情况是,在我应用的表中,自动增量值递增,但没有插入任何内容到表中。我可以使用以下方法在ec2实例中插入到表中: insert into applied (jobID, appID) values (1,8); 比如说 def POST(self, empID, jobID): print "jobID",jobID output_format = cherryp
insert into applied (jobID, appID) values (1,8);
比如说
def POST(self, empID, jobID):
print "jobID",jobID
output_format = cherrypy.lib.cptools.accept(['application/json'])
try:
cnx = mysql.connector.connect(
user=self.db['user'],
host=self.db['host'],
database=self.db['name'],
)
cursor = cnx.cursor()
except Error as e:
print e
appID = 1
qn="insert into applied (jobID, appID) values (%s,%s);" % (jobID, appID)
try:
cursor.execute(qn)
cnx.close()
except Error as e:
print e
print qn
# Validate form data
# Insert or update restaurant
# Prepare response
result = {}
result['jobID'] = jobID
result['appID'] = appID
result['request'] = qn
return json.dumps(result)
生成数据库的mysql代码是:
CREATE TABLE `applied` (
`appliedID` int(11) NOT NULL AUTO_INCREMENT,
`jobID` int(11) NOT NULL,
`appID` int(11) NOT NULL,
PRIMARY KEY (`appliedID`),
KEY `jobID` (`jobID`),
KEY `appID` (`appID`),
CONSTRAINT `applied_ibfk_1` FOREIGN KEY (`jobID`) REFERENCES `jobs` (`jobID`) ON DELETE CASCADE,
CONSTRAINT `applied_ibfk_2` FOREIGN KEY (`appID`) REFERENCES `applicants` (`appID`) ON DELETE CASCADE
) ENGINE=InnoDB AUTO_INCREMENT=107 DEFAULT CHARSET=latin1;
您需要提交插入。试试这个
try:
cursor.execute(qn)
cursor.commit()
cnx.close()
except Error as e:
希望这有帮助 Don's string将从您的请求接收的变量复合到语句中。将%s放入查询字符串中。将参数添加到execute语句中的第二个参数,例如
cursor.execute(qn[jobID,appID])
。否则就会被黑客攻击。