0
votes

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]+"');

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.

EDIT: My full code is here:

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;")

And then:

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()
1
It seems to work, can we see the Python?Sam
Sure, I'll edit my original message and put the python in.kpz
Is friend[13] an integer by any chance? Can we get a dump of friend?Sam
There is some personal information in 'friend', but I have checked that the type of each element within friend is a string.kpz
List the columns explicitly in the insert.Gordon Linoff

1 Answers

1
votes

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

This won't work :

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

But this will work :

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

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