【问题标题】:Cherrypy Post command increments mysql auto increment but does not insert into the databaseCherrypy Post命令递增mysql自动递增但不插入数据库
【发布时间】:2015-05-07 21:03:23
【问题描述】:

我正在开发我的第一个 Web 应用程序,但我遇到了 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;

【问题讨论】:

  • 唐的字符串复合变量从你的请求中接收到语句中。将 %s 放入您的查询字符串中。将参数添加到执行语句中的第二个参数,例如cursor.execute(qn, [jobID, appID])。否则会被黑。

标签: mysql amazon-ec2 cherrypy


【解决方案1】:

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

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

希望这会有所帮助!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-20
    • 1970-01-01
    • 2013-04-12
    • 2018-02-05
    相关资源
    最近更新 更多