为什么会出现此故障,为什么mysql设置为false?

时间:2019-06-02 16:17:07

标签: python mysql exception

我想将这些数据放入数据库中,但是插入失败,可能是因为语句错误。

python3.6

# -*- coding: utf-8 -*-

import pymysql

tup = ('222.72.166.235:53281', '上海', '高匿', 'HTTPS')
name = tup[0]
ip_list = tup[0].split(':')
ip = ip_list[0]
port = ip_list[1]
location = tup[1]
anonymity = tup[2]
protocol = tup[3]
#instr = "'{0}', '{1}', '{2}', '{3}', '{4}', '{5}', ".format(name, ip, port, location, anonymity, protocol)
db_test = pymysql.connect('localhost', 'root', '123456', "test")
cursor = db_test.cursor()
sql = "INSERT INTO public_proxy(name, ip, port, location, anonymity, protocol) VALUES('%s', '%s',  '%s',  '%s',  '%s', '%s')" % (name, ip, port, location, anonymity, protocol)
try:
    cursor.execute(sql)
    db_test.commit()
    print('数据插入成功...')  
except:
    db_test.rollback()
    print('错误')
db_test.close()

我希望结果为'print('数据插入成功...')'

错误
[Finished in 0.4s]

[在此处输入图片描述] [1]

这是数据库结构表。

mysql> desc public_proxy;
+-------------+------------------+------+-----+---------+----------------+
| Field       | Type             | Null | Key | Default | Extra          |
+-------------+------------------+------+-----+---------+----------------+
| id          | int(10) unsigned | NO   | PRI | NULL    | auto_increment |
| name        | varchar(25)      | NO   |     | NULL    |                |
| ip          | varchar(20)      | NO   |     | NULL    |                |
| port        | varchar(10)      | NO   |     | NULL    |                |
| location    | varchar(50)      | NO   |     | NULL    |                |
| anonymity   | tinyint(3)       | NO   |     | NULL    |                |
| protocol    | tinyint(3)       | NO   |     | NULL    |                |
| source      | varchar(25)      | NO   |     | NULL    |                |
| create_time | datetime         | YES  |     | NULL    |                |
+-------------+------------------+------+-----+---------+----------------+

2 个答案:

答案 0 :(得分:0)

except Exception as e: db_test.rollback() print('错误') print("{0}: {1!r}".format(type(e).__name__, e.args)) 块中捕获异常时,应始终检查该异常。它将为您提供有关异常性质的线索以及特定的错误消息。

我尝试测试您的脚本,但是我将异常处理程序更改为以下内容:

错误
ProgrammingError: (1146, u"Table 'test.public_proxy' doesn't exist")

我得到以下输出:

{{1}}

这在我的环境中是可以预期的,因为在运行此脚本之前我没有创建表。

根据您的情况,可能是导致异常的其他原因。

重点是您必须检查异常中的信息,否则您将不知道原因。


发表评论:

该错误表明您正在尝试将字符串存储到整数列中,而MySQL不会将其转换。

您应该使用数字值。

答案 1 :(得分:0)

最初,我插入的值有问题,但是现在我进行了修改,并且成功了。

#!/usr/bin/env python
# -*- coding: utf-8 -*-

import pymysql

tup = ('222.72.166.235:53281', '上海', '高匿', 'HTTPS')
name = tup[0]
ip_list = tup[0].split(':')
ip = ip_list[0]
port = ip_list[1]
location = tup[1]
anonymity = tup[2]
protocol = tup[3]
source = 'xici'

if anonymity == '透明':
    anonymity = 0
elif anonymity == '高匿':
    anonymity = 1
elif anonymity == '':
    anonymity =2
else:
    anonymity = 3

if protocol == 'HTTP':
    protocol = 0
elif protocol == 'HTTPS':
    protocol = 1
elif protocol == '':
    protocol = 2
else:
    protocol = 3
#instr = "'{0}', '{1}', '{2}', '{3}', '{4}', '{5}', ".format(name, ip, prot, location, anonymity, protocol)
db_test = pymysql.connect('localhost', 'root', '123456', "test")
cursor = db_test.cursor()
sql = "INSERT INTO public_proxy(name, ip, port, location, anonymity, protocol, source) VALUES('%s', '%s', '%s', '%s', '%s', '%s', '%s')" % (name, ip, port, location, anonymity, protocol, source)
try:
    cursor.execute(sql)
    db_test.commit()
    print('数据插入成功...')  
except Exception as e:
    db_test.rollback()
    print('错误')
    print("{0}: {1!r}".format(type(e).__name__, e.args))

db_test.close()

结果:

数据插入成功...

谢谢,我的朋友。