我想把这些数据放到数据库中,但是插入失败了,可能是因为语句错误。
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()我希望结果是‘打印(’数据插入成功.‘)
错误
[Finished in 0.4s]在这里输入图像描述
这是数据库结构表。
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 | |
+-------------+------------------+------+-----+---------+----------------+发布于 2019-06-02 18:49:25
在except块中捕获异常时,应该始终检查它。它将为您提供关于异常性质的线索,以及一条特定的错误消息。
我尝试测试您的脚本,但我将异常处理程序更改为:
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")这在我的环境中是预期的,因为在运行这个脚本之前,我没有创建这个表。
在您的情况下,可能会有不同的原因导致异常。
关键是您必须检查异常中的信息,否则您无法知道原因。
关于你的评论:
此错误指示您试图将字符串存储到整数列中,而MySQL不会将其转换。
您应该使用一个数值。
发布于 2019-06-03 09:42:35
最初,我插入的值有问题,但现在我做了一个修改,它是成功的。
#!/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()结果:
数据插入成功...我的朋友。
https://stackoverflow.com/questions/56416769
复制相似问题