On Tue, Jun 12, 2012 at 10:48 AM, John Fabiani <[email protected]> wrote: > > I found this: > > > Writing images > > Some people prefer to put their images into the database, some prefer to > keep them on the file system for their applications. Technical > difficulties arise when we work with millions of images. Images are > binary data. MySQL database has a special data type to store binary data > called *BLOB* (Binary Large Object). > > mysql> CREATE TABLE Images(Id INT PRIMARY KEY AUTO_INCREMENT, Data > MEDIUMBLOB); > Query OK, 0 rows affected (0.06 sec) > > For this example, we create a new table called Images. > > #!/usr/bin/python > # -*- coding: utf-8 -*- > > import MySQLdb as mdb > import sys > > try: > fin = open("chrome.png") > img = fin.read() > fin.close() > > except IOError, e: > > print "Error %d: %s" % (e.args[0],e.args[1]) > sys.exit(1) > > > try: > conn = mdb.connect(host='localhost',user='testuser', > passwd='test623', db='testdb') > cursor = conn.cursor() > cursor.execute("INSERT INTO Images SET Data='%s'" % \ > mdb.escape_string(img)) > > conn.commit() > > cursor.close() > conn.close() > > except mdb.Error, e: > > print "Error %d: %s" % (e.args[0],e.args[1]) > sys.exit(1) > > In the above script, we read a png image and insert it into the Images > table. > > fin = open("chrome.png") > img = fin.read() > > We open and read an image. The *read()* function returns the data as > string. > > cursor.execute("INSERT INTO Images SET Data='%s'" % \ > mdb.escape_string(img)) > > This string data is inserted into the table. Before doing so, it is > processed by the *escape_string()* method. It escapes a string for use > as a query parameter. This is common practice to avoid malicious sql > injection attacks. > > > Reading images > > In the previous example, we have inserted an image into the database > table. Now we are going to read the image back from the table. > > #!/usr/bin/python > # -*- coding: utf-8 -*- > > import MySQLdb as mdb > import sys > > try: > conn = mdb.connect(host='localhost',user='testuser', > passwd='test623', db='testdb') > > cursor = conn.cursor() > > cursor.execute("SELECT Data FROM Images LIMIT 1") > > fout = open('image.png','wb') > fout.write(cursor.fetchone()[0]) > fout.close() > > cursor.close() > conn.close() > > except IOError, e: > > print "Error %d: %s" % (e.args[0],e.args[1]) > sys.exit(1) > > We read one image from the Images table. > > cursor.execute("SELECT Data FROM Images LIMIT 1") > > We select one record from the table. > > fout = open('image.png','wb') > > We open a writable binary file. > > fout.write(cursor.fetchone()[0]) > > We fetch the data from the previous SQL statement and write it to the file. > > Now we should have an image called image.png in our current directory. > We can check if it is the same image, that we have inserted into the table. >
John, I tried using the mdb.escape_string an it's still giving me the same UnicodeEncodeError. I also discovered something else. I put the read file into the bizobj using .setFieldVal and then read the field using .getFieldVal, I found that they are not the same and are several hundred bytes different. Could it be the way Dabo handles the setFieldVal on the binary field? Regards, Nate --- StripMime Report -- processed MIME parts --- multipart/alternative text/plain (text body -- kept) text/html --- _______________________________________________ Post Messages to: [email protected] Subscription Maintenance: http://leafe.com/mailman/listinfo/dabo-dev Searchable Archives: http://leafe.com/archives/search/dabo-dev This message: http://leafe.com/archives/byMID/cal-abaxj0uz4yuhhtssnhx2c+d2vyjntky5pgwdzquuyfrc...@mail.gmail.com
