Cherrypy Post命令增加mysql自动增量但不插入数据库

时间:2015-05-07 21:03:23

标签: mysql amazon-ec2 cherrypy

我正在开发我的第一个网络应用程序,我遇到了mysql的问题。我尝试使用下面的POST函数插入我的mysql数据库。会发生的情况是,在我的应用表中,自动增量值会递增,但不会在表中插入任何内容。我可以使用:

在我的ec2实例中插入表格
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;

1 个答案:

答案 0 :(得分:0)

您需要提交插入内容。试试这个...

try:
    cursor.execute(qn)
    cursor.commit()
    cnx.close()
except Error as e:

希望这有帮助!