不正确的整数值错误 [英] Incorrect Integer Value Error

查看:56
本文介绍了不正确的整数值错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将一些列表中的一些值输入到 Python 中 MySQL 的表中.我写了以下内容:

I am trying to enter some values from some lists I have into a table in MySQL in Python. I have written the following:

CREATE TABLE IF NOT EXISTS friend_attributes (
    Friend int(11) NOT NULL auto_increment, 
    Likes_Count varchar(512) NOT NULL default '', 
    ID varchar(512) NOT NULL default '', 
    PRIMARY KEY(Friend)) ENGINE=MyISAM DEFAULT CHARSET=utf8;

然后我尝试添加以下值(我已检查这些值是否存在并且是字符串):

I then attempt to add the following values (I have checked that these exist and are strings):

INSERT INTO friend_attributes VALUES(
    NULL, 
   '"+friend[16]+"',
   '"+friend[17]+"');

我收到以下错误:1366,不正确的整数值:''列'喜欢_计数'在第 1 行"

I get the following error: 1366, "Incorrect integer value: '' for column 'Likes_Count' at row 1"

我不知道为什么会出现错误,因为我知道要插入的值不是整数,而是字符串.任何帮助,将不胜感激.

I do not know why I am getting an error since I know that the value that I want to insert is not an integer, but a string. Any help would be appreciated.

我的完整代码在这里:

cur.execute("CREATE TABLE IF NOT EXISTS friend_attributes (
Friend int(11) NOT NULL auto_increment,
UID int(11) NOT NULL,
Name varchar(512) NOT NULL default '',
Sex varchar(512) NOT NULL default '',
First_Name varchar(512) NOT NULL default '',
Middle_Name varchar(512) NOT NULL default '',
Last_Name varchar(512) NOT NULL default '',
Locale varchar(512) NOT NULL default '',
About_Me varchar(512) NOT NULL default '', 
Hometown varchar(512) NOT NULL default '',
Birthday varchar(512) NOT NULL default '', 
Political varchar(512) NOT NULL default '', 
Relationship varchar(512) NOT NULL default '', 
Religion varchar(512) NOT NULL default '', 
Likes_Count varchar(512) NOT NULL default '', 
Friend_Count varchar(512) NOT NULL default '', 
Mutual_Friends varchar(512) NOT NULL default '', 
Pic_URL varchar(512) NOT NULL default '', 
Label varchar(512) NOT NULL default '', 
ID varchar(512) NOT NULL default '', 
PRIMARY KEY(Friend)) ENGINE=MyISAM DEFAULT CHARSET=utf8;")

然后:

for friend in friends:
    cur.execute("INSERT INTO friend_attributes VALUES(NULL, '"+friend[0]+"',     
    '"+friend[1]+"', '"+friend[2]+"', '"+friend[3]+"', '"+friend[4]+"', 
    '"+friend[5]+"', '"+friend[6]+"', '"+friend[7]+"', '"+friend[8]+"', 
    '"+friend[9]+"', '"+friend[10]+"','"+friend[11]+"', '"+friend[12]+"',
    '"+friend[13]+"', '"+friend[14]+"', '"+friend[15]+"', '"+friend[16]+"',
    '"+friend[17]+"', '"+friend[18]+"');")
conn.commit()

推荐答案

您必须转义单引号字符,否则查询会混淆什么是字符串,什么不是.

You have to escape single quote characters or the query gets mixed up about what is a string and what is not.

这行不通:

Select * from test where name = 'O'Connor' 

但这会起作用:

Select * from test where name = 'O\'Connor'

看看mysqli.real-escape-string 它将为您完成工作.

Have a look at mysqli.real-escape-string which will do the job for you.

这篇关于不正确的整数值错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆