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 7652073
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T11:40:15+00:00 2026-05-31T11:40:15+00:00

I have created a database with MySQLdb. In database I have a table with

  • 0

I have created a database with MySQLdb.
In database I have a table with name student with columns:

id(is int),
id_user(is int),
f_name(is str),
l_name(is str)

I want to update a row.
My code is below:

db=mdb.connect(host="localhost", use_unicode="True", charset="utf8", 
               user="", passwd="", db="test")                          
# prepare a cursor object using cursor() method
cursor = db.cursor()

sql="""SELECT id_user FROM student"""

try:
   # Execute the SQL command
   cursor.execute(sql)
   # Commit your changes in the database
   db.commit()
except:
   # Rollback in case there is any error
   db.rollback()

rows = cursor.fetchall()

the=int(7)
se=str('ok')
for row in rows:
    r=int(row[0])
    if r==the:          
        sql2 = """UPDATE student
                SET f_name=%s
                WHERE id_user = %s"""% (se,the)

               # Execute the SQL command
        cursor.execute(sql2)
        # Commit your changes in the database
        db.commit()

        db.rollback()
# disconnect from server
db.close()

When I run it I take the error there is column with name ok why?
Can anyone help me find what I am doing wrong please?

  • 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-05-31T11:40:17+00:00Added an answer on May 31, 2026 at 11:40 am

    You should run the query like this:

    sql2 = """UPDATE student
              SET f_name = %s
              WHERE id_user = %s"""
    cursor.execute(sql2, (se, the))
    

    Don’t use string interpolation, let the database driver handle passing the parameters for you. Otherwise you have to deal with syntax errors like this, or worse, SQL injection.

    More details here.

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

Sidebar

Related Questions

I have created a database with MySQLdb. In database I have a table with
I have a mysql database table called character with columns like name, strength, intelligence,
I have created a table on an Oracle 10g database with this structure :
I am playing with wpf and when i have created database it is created
I have created a MySQL database using MySQL Workbench. It has about 20 tables.
I have a database that I have created using SQL Server Developer 2008. I
I have created one user named tuser with create database rights in SQL server
I have created a shell script to backup my webfiles + database dump, put
I have created a simple windows service that periodically checks a remote database via
I have created a Windows Service in VB.net for building xML from a database.

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.