Retrieving possible list for use in a subsequent INSERT

rurpy at yahoo.com rurpy at yahoo.com
Thu Oct 31 15:22:20 EDT 2013


On 10/31/2013 03:24 AM, Nick the Gr33k wrote:
>[...] 
> # find out if visitor has downloaded torrents in the past
> 	cur.execute('''SELECT torrent FROM files WHERE host = %s''', host )
> 	data = cur.fetchall()
> 
> 	downloads = []
> 	if data:
> 		for torrent in data:
> 			downloads.append( torrent )
> 	else:
> 		downloads.append( 'None Yet' )
> 
> # add this visitor entry into database
> cur.execute('''INSERT INTO visitors (counterID, refs, host, city, 
> useros, browser, visits, downloads) VALUES (%s, %s, %s, %s, %s, %s, %s, 
> %s)''', (cID, refs, host, city, useros, browser, visits, downloads) )
>[...]

and 

On 10/31/2013 03:32 AM, Nick the Gr33k wrote:
> The error seen form error log is:
> 
> [Thu Oct 31 09:29:35 2013] [error] [client 46.198.103.93]
> [Thu Oct 31 09:29:35 2013] [error] [client 46.198.103.93] Traceback 
> (most recent call last):
> [Thu Oct 31 09:29:35 2013] [error] [client 46.198.103.93]   File 
> "/home/nikos/public_html/cgi-bin/metrites.py", line 274, in <module>
> [Thu Oct 31 09:29:35 2013] [error] [client 46.198.103.93]     (cID, 
> refs, host, city, useros, browser, visits, downloads) )
> 
> [Thu Oct 31 09:29:35 2013] [error] [client 46.198.103.93] 
> pymysql.err.InternalError: (1241, 'Operand should contain 1 column(s)')
> 
> line 274 is:
> 
> # add this visitor entry into database
> cur.execute('''INSERT INTO visitors (counterID, refs, host, city, 
> useros, browser, visits, downloads) VALUES (%s, %s, %s, %s, %s, %s, %s, 
> %s)''',  (cID, refs, host, city, useros, browser, visits, downloads) )


You set the value of 'downloads' to a list:

> 	downloads = []
> 	if data:
> 		for torrent in data:
> 			downloads.append( torrent )

and when you use 'downloads', use have:

  INSERT INTO visitors (..., downloads) VALUES (..., %s), (..., downloads)

If the 'downloads' column in table 'visitors' is a 
normal scalar value (text string or such) then perhaps
you can't insert a value that is a list into it?  And
that may be causing your problem?

If that is in fact the problem (I am only guessing), you 
could convert 'downloads' to a single string for insertion
into your database with something like,

  downloads = ', '.join( downloads ) 



More information about the Python-list mailing list