List:General Discussion« Previous MessageNext Message »
From:Jack Hatterly Date:March 9 2012 12:28pm
Subject:A Blob That's a String??
View as plain text  
Hi;
I have loaded an image from a form into MySQL using Python as a longblob. Here is my
table:

mysql> describe pics;
+-------------+------------+------+-----+---------+----------------+
| Field       | Type       | Null | Key | Default | Extra          |
+-------------+------------+------+-----+---------+----------------+
| ID          | int(11)    | NO   | PRI | NULL    | auto_increment | 
| Specials_ID | int(11)    | NO   | MUL | NULL    |                | 
| Number      | tinyint(1) | YES  |     | NULL    |                | 
| Pic         | longblob   | YES  |     | NULL    |                | 
+-------------+------------+------+-----+---------+----------------+

Here is my Python code that loads the data:

pic_zero = form.getfirst("pic_zero") # where pic_zero is an uploaded file (i.e., image)
from a form calling this script
...
    pics = [pic_zero, pic_one, pic_two, pic_three]
    i = 0
    for pic in pics:
      if pic:
        sql = 'insert into pics values (Null, "%s", "%s", "%s")' % (max_id,  i, "%s")
        cursor.execute(sql, (MySQLdb.Binary(pic),), )
        db.commit()
      i += 1

Here is the Python code that calls the image:

#!/usr/bin/python
import cgitb; cgitb.enable()
import MySQLdb
import cgi
import sys,os
sys.path.append(os.getcwd())
from login import login
user, passwd, db, host = login()
form = cgi.FieldStorage()
id = form['id'].value
number = form['number'].value
db = MySQLdb.connect(host, user, passwd, db)
cursor= db.cursor()
cursor.execute("select Pic from pics where ID=%s and Number=%s", (id, number))
content = cursor.fetchall()[0][0].tostring()
cursor.close()
print 'Content-Type: image/jpeg'
print
print content

Here is the Python error:

   14 cursor= db.cursor()


   15 cursor.execute("select Pic from pics where ID=%s and Number=%s", (id, number))


   16 content = cursor.fetchall()[0][0].tostring()


   17 cursor.close()


   18 print 'Content-Type: image/jpeg'


content undefined, cursor = <MySQLdb.cursors.Cursor object>, cursor.fetchall =
<bound method Cursor.fetchall of <MySQLdb.cursors.Cursor object>>, ].tostring
undefinedAttributeError: 'str' object has no attribute 'tostring'

      args =
("'str' object has no attribute 'tostring'",)




Even when I hard code in the correct values I get this error. However, when I select *
from pics, where there is only one longblob in the table, it prints out a tremendous load
of data which indicates to me that a binary file has indeed been loaded. What I am trying
to ascertain is why is this longblob considered a string? How can I more properly test if
a binary file has been loaded?
TIA,
Jack
 		 	   		  
Thread
A Blob That's a String??Jack Hatterly9 Mar
  • Re: A Blob That's a String??Reindl Harald9 Mar
    • RE: A Blob That's a String??Jack Hatterly9 Mar