Flask sqlalchemy更新限制

时间:2015-04-29 15:03:51

标签: python mysql sql-update limit flask-sqlalchemy

我尝试使用Flask-SQLAlchemy(而非SQLAlchemy!)在Flask中执行带限制的更新查询。

db.session.query(ItemObject).filter_by(owner_type=0,item_id=item_id.decode('hex')).update({'owner_type': '1'})

当我尝试" .limit(1)"或者"更新({' owner_type':' 1'},mysql_limit = 1)"我发现错误

AttributeError: 'long' object has no attribute 'limit'

TypeError: update() got an unexpected keyword argument 'mysql_limit'

如何在不使用execute()的情况下进行此查询?

1 个答案:

答案 0 :(得分:0)

对于您的第一个错误,听起来您在调用update()后尝试调用limit()。

function onOpen() {
    var ss = SpreadsheetApp.getActiveSpreadsheet();   
    var menuEntries = [ {name: "Add all to calendar", functionName: "pushToCalendar"} ];
    ss.addMenu("AutoCalendar", menuEntries);
}


//push new events to calendar
function pushToCalendar() {

    //spreadsheet variables
    var sheet = SpreadsheetApp.getActiveSheet();
    var calendar = CalendarApp.getCalendarById('athomas12033@gmail.com')[0];
    var numRows = sheet.getLastRow()
    var updateRange = sheet.getRange('L1'); 
    var cell = SpreadsheetApp.getActiveRange();
    var cellCol = cell.getColumn();
    var cellRow = cell.getRow();
    var numRows = sheet.getLastRow();
    var dataRange = sheet.getRange(1, 1,sheet.getLastRow(), 13);
    var data = dataRange.getValues();

    for (i in data) {
        var row = data[i];
        var name = row[0];
        var type = row[3];
        var dateReq = row[4];
        var tstart = row[5];
        var tstop = row[6]; 
    }

    if (tstop == " ") {
        var tstop = tstart;
    }

    //show updating message
    updateRange.setFontColor('Blue');
    var title = row[7];
    if (title == " ") {
        var title = type;
        var narr = "";
    } else {
        var narr = row[8];
    }

    var meet = row[9];
    var realName = row[2];
    var contact = row[10];
    var appr = row[12];
    if (appr == "y") {
        cal.createEvent(title, new Date(dateReq+" "+tstart), new Date(dateReq+" "+tstop),                 
                {description:type+"\n"+narr+"\n"+realName+", "+contact,location:meet,guests:name,sendInvites:true});
    }     


    // Delete approved programs.
    for (var delRow = 1; delRow < numRows; delRow++) {
        var range = sheet.getRange(delRow,14,1,1);
        if (range.getValues() == "y") {
            sheet.deleteRow(delRow);
            var delRow = delRow-1;
        }
    }
}

此行返回db.session.query(ItemObject).filter_by(owner_type=0,item_id=item_id.decode('hex')).update({'owner_type': '1'}) ,因为long返回匹配的行数(未修改!)。您无法在更新后链接限制。

也无法使用update()来查询limit,因此无论如何都无法正常工作。这只是一个MySQL的事情。

看一下之前的答案 - https://stackoverflow.com/a/25943713/175320 - 它向您展示了如何使用子查询以可移植的方式获得相同的最终结果。