Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8998503
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T00:02:26+00:00 2026-06-16T00:02:26+00:00

How can I update a row in my database using add() ? Here is

  • 0

How can I update a row in my database using add() ?

Here is the code I tried :

@view_config(route_name='doc_user_edit', renderer='doc/edit.mako')
def doc_user_edit(request):
    """ Edit a documentation page. """
    message = ""
    try:
        page = DBSession.query(Page).filter_by(title=request.matchdict['pagename']).first()
    except DBAPIError:
        return Response('An error occured while trying to contact the database', content_type='text/plain', status_int=500)

    if page is None:
        return HTTPNotFound('No such page')

    if 'form.submitted' in request.params:
        page = Page(request.params['title'], request.params['content'], datetime.now())
        DBSession.add(page)
        DBSession.commit()
        message = _('Page edited')

    return {'p':page,
            'm':message,
            'save_url': request.route_url('doc_user_edit', pagename=page.title),
    }  

Notes : DBSession is a scoped_session.

If the row is brand new (i.e. unique key “title” is not present), it correctly adds a new row with the values I passed to it.

But if the row is an existing one, it simply does… nothing. I’d like it to update the row with new values (i.e. values extracted from a form). How can I achieve that?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T00:02:27+00:00Added an answer on June 16, 2026 at 12:02 am

    You don’t need to use add() at all (it’s only necessary for new objects). Simply modify the existing page object and then commit.

    if 'form.submitted' in request.params:
        page.title = request.params['title']
        page.content = request.params['content']
        page.no_idea_what = datetime.now()
        DBSession.commit()
    

    By the way, unless there is a really good reason to use localtime it’s better to store your timestamps in UTC.


    Depending on how your site works you might want to combine the logic for edit and create though. It would basically look like this:

    def do_edit_whatever(obj):
        # Populate the fields here
        if obj.id is None:
            db.session.add(obj)
        db.session.commit()
    

    Then your view functions would call that function either with a new Page() or one that is already in the database.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

How can I update a complete data row by using a data row from
I’m using a database helper to update a table with one row and two
I have to update a row value form database using 2 values as conditions(1
I need a document based database where I can update single documents but when
I have a com.mysql.jdbc.exceptions.MySQLIntegrityConstraintViolationException in my code (using Hibernate and Spring) and I can't
Can we add multiple rows in the database table from one JSF page in
I am trying to update a set of queries using .NET's DataAdapter. Here's a
I'm using a table to update a database, the table has 264 checkboxes which
My database is currently using the InnoDB engine. Now I want to add the
I have the following code for using an access database OleDbConnection con = new

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.