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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T07:26:17+00:00 2026-06-13T07:26:17+00:00

I am trying to write a table in mysql through python: I want to

  • 0

I am trying to write a table in mysql through python:

I want to make table in MYSQL but delete the lines that are more than 3, in other words I just want 3 lines. for example

emp_no   Num1   Num2   Num3
1          1      2      3
2          1      2      3
3          1      2      3

These should be deleted

4          1      2      3
5          1      2      3
6          1      2      3
.
.
.

My code is:

from __future__ import print_function
import mysql.connector
from mysql.connector import errorcode



cnx = mysql.connector.connect(user='',
                              host='localhost')
cursor = cnx.cursor()

DB_NAME = 'DB'
cnx.database = DB_NAME 


TABLES = {}
TABLES['History'] = (
    "CREATE TABLE `History` ("
    "  `emp_no` int(11) NOT NULL AUTO_INCREMENT,"
    "  `Num1` text NOT NULL,"
    "  `Num2` text NOT NULL,"
    "  `Num3` text NOT NULL,"
    "  PRIMARY KEY (`emp_no`)"
    ") ENGINE=InnoDB")


####################33
##
Num1=1
Num2=2
Num3=3

add_employee = ("INSERT INTO History "
               "(Num1, Num2, Num3) "
               "VALUES (%s, %s, %s)")

data_employee = (str(Num1), str(Num2), str(Num3))
### Insert new employee
cursor.execute(add_employee, data_employee)

emp_no = cursor.lastrowid

### Make sure data is committed to the database
cnx.commit()

### *** I use this line for deleting the rows ***
query2 = ("Delete FROM History "
    "WHERE  emp_no >  %s")
cursor.execute(query2, ('3',))


# this part is used for printing the table elements    
query = ("SELECT emp_no, Num1, Num2, Num3 FROM History ")

cursor.execute(query)
#print("last_name")



for (emp_no, Num1, Num2, Num3) in cursor:
 print("{} {} {}".format(
     emp_no, Num1, Num2, Num3))



cursor.close()
cnx.close()
##

When I run this for example 10 times always the result is:

1 1 2
2 1 2
3 1 2

but when I comment out the part:

query2 = ("Delete FROM History "
        "WHERE  emp_no >  %s")
    cursor.execute(query2, ('3',))

what I see is interesting:

1 1 2
2 1 2
3 1 2
4 1 2
5 1 2
6 1 2
7 1 2
8 1 2
9 1 2
10 1 2

the lines more than 3 are still there in Mysql they were just not being shown before commenting out the delete query.

So How can I remove specific lines completely from mysql?

  • 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-13T07:26:18+00:00Added an answer on June 13, 2026 at 7:26 am

    It seems you would need to call commit for the DELETE operation to take effect permanently, as you are doing after the INSERT operation.

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

Sidebar

Related Questions

I am trying to write a query that joins another table, but in this
I trying to log into MySql table but it won't write. It doesn't show
I am trying to write a before delete trigger on mysql table here's is
I am trying to write a MySQL query that retrieves one record from table
I'm trying to write an insert statement for a SQL Server table that inserts
I'm trying to write a SQL script that inserts 2 rows into a table
I am trying to write a query that connects 3 tables. The first table
I'm trying to write a mysql query such that I can retreieve the first
I am trying to write a function that will insert data into a MySQL
All, I'm trying to write a function that would check in my users table

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.