通过python删除mysql中的行

通过python删除mysql中的行,python,mysql,python-2.7,Python,Mysql,Python 2.7,我正试图通过python在mysql中编写一个表: 我想在MYSQL中创建表,但是删除超过3行的行,换句话说,我只需要3行。比如说 emp_no Num1 Num2 Num3 1 1 2 3 2 1 2 3 3 1 2 3 这些应该删除 4 1 2 3 5 1 2 3 6 1

我正试图通过python在mysql中编写一个表:

我想在MYSQL中创建表,但是删除超过3行的行,换句话说,我只需要3行。比如说

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

这些应该删除

4          1      2      3
5          1      2      3
6          1      2      3
.
.
.
我的代码是:

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()
##
当我运行此示例10次时,结果总是:

1 1 2
2 1 2
3 1 2
但当我评论这一部分时:

query2 = ("Delete FROM History "
        "WHERE  emp_no >  %s")
    cursor.execute(query2, ('3',))
我看到的很有趣:

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
Mysql中还有3行以上的内容,只是在注释删除查询之前没有显示出来


那么我如何才能从mysql中完全删除特定行呢?

似乎您需要调用commit,删除操作才能永久生效,当您在插入操作后执行此操作时。

这与mysql-error-1064有什么关系?很抱歉,我对mysql了解不多,我认为可能与此有关。您是否在代码中的某个地方遇到此错误?如果否,则应删除标记