{
On Saturday I needed to grab some blob data and create files out of it. The following was some Python I used, inspired very much by this code.
The biggest point of interest is the strongly typed array, which in IronPython is simple once you know how: using Array.CreateInstance.
#GET IMAGES FROM IMAGE BINARY FIELD
# 02/02/2008 - David - @THE COMMAND CENTRE
import clr
clr.AddReference("System.Data")
from System import *
from System.Data import *
from System.Data.SqlClient import *
from System.IO import *
CONNECT = "Data Source=(local)\sqlexpress;Initial Catalog=DATABASE;Integrated Security=SSPI"
sql = "select ID_FIELD, BLOB_FIELD from product_set"
cn = SqlConnection(CONNECT)
cn.Open()
cmd = SqlCommand(sql, cn)
reader = cmd.ExecuteReader(CommandBehavior.CloseConnection)
buffSize = 100
buff = Array.CreateInstance(Byte, buffSize)
recCounter = 0
while reader.Read():
print "Creating " + str(reader[0])
fs = FileStream(str(reader[0]) + str(recCounter) + "_diagram.png", FileMode.OpenOrCreate, FileAccess.Write)
bw = BinaryWriter(fs)
if(reader[1] != DBNull.Value):
posit = 0
b = reader.GetBytes(1, posit, buff, 0, buffSize)
while(b == buffSize):
bw.Write(buff)
bw.Flush()
posit = posit + buffSize
b = reader.GetBytes(1, posit, buff, 0, buffSize)
bw.Write(buff, 0, b)
bw.Flush()
bw.Close()
fs.Close()
recCounter = recCounter + 1
reader.Close()
I'd like to clean it up a bit, but I've written about 5,000,000 things that I "intend" to clean up before posting and they're never posted.
No comments:
Post a Comment