473,398 Members | 2,404 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,398 software developers and data experts.

how to store binary file data to a database blob

Hi,

I'd like to read the contents of a file into memory. The problem is that
this file is binary. I then want to store the whole thing in memory to a
database as a blob.
I have no problem reading from a file: I did this:

import os
f = open('/bin/ls' , 'r+b')
data = f.read()
f.close()

How do I place that in a blob? If I concatenate this to a sql string, how
will the database sql parser know where my data string ends, knowing that my
data string could contain lots of ' and " and other little things in it. ???

storeString = "insert into mytable (filedata) values ( %s ) " % data

Does that look right? I don't think it would execute correctly.

Thanks
David Stockwell
-------
Cell: http://cellphone.duneram.com/index.html
Cam: http://www.duneram.com/cam/index.html
Tax: http://www.duneram.com/index.html

__________________________________________________ _______________
MSN Toolbar provides one-click access to Hotmail from any Web page – FREE
download! http://toolbar.msn.click-url.com/go/...ave/direct/01/
Jul 18 '05 #1
3 5566
> I'd like to read the contents of a file into memory. The problem is
that this file is binary. I then want to store the whole thing in
memory to a database as a blob.
I have no problem reading from a file: I did this:

import os
f = open('/bin/ls' , 'r+b')
data = f.read()
f.close()

How do I place that in a blob? If I concatenate this to a sql string,
how will the database sql parser know where my data string ends, knowing
that my data string could contain lots of ' and " and other little
things in it. ???

storeString = "insert into mytable (filedata) values ( %s ) " % data

Does that look right? I don't think it would execute correctly.

This probably won't work... Try this (assuming you are working with a
Python DB API compatible database class):

cursor.execute("insert into mytable (filedata) values ( %s ) ", data)

This will let the database class replace %s by your data, including all
required escaping stuff - in contrast to a 1:1 string replacment when
using the % operator. This works at least for arbitrary strings (and
that's the way to handle strings, if you don't like SQL injections ;)
But I never tried this with blobs - perhaps they need some special
treatment (thinking e.g. of laaarge files)...
Jul 18 '05 #2
Benjamin Niemann wrote:
using the % operator. This works at least for arbitrary strings (and
that's the way to handle strings, if you don't like SQL injections ;)
But I never tried this with blobs - perhaps they need some special
treatment (thinking e.g. of laaarge files)...


It is generally NOT a good idea to write large binary strings to a (sql
relational) database, even if it has blobs. A much more robust (and faster)
solution is to save general data about the blob in the database (its size,
name, whatever, etc.) and the actual data in a separate file in the
filesystem (or maybe in a "db" database (BDB, GDBM...), if you really like
them).

YMMV, mostly depending on the size of blobs. :)
Jul 18 '05 #3
Your DB-API driver should allow you to pass binary data easily via a typed
parameter to execute (or executemany for multiple rows). For example, in
ThinkSQL, you can say:

blobdata=open('image.jpg', 'rb').read()
s1.execute("INSERT INTO picture_table (image) VALUES (?)",
(ThinkSQL.Binary(blobdata),))

Regards,
Greg Gaughan
www.thinksql.co.uk
"David Stockwell" <wi*******@hotmail.com> wrote in message
news:ma*************************************@pytho n.org...
Hi,

I'd like to read the contents of a file into memory. The problem is that
this file is binary. I then want to store the whole thing in memory to a
database as a blob.
I have no problem reading from a file: I did this:

import os
f = open('/bin/ls' , 'r+b')
data = f.read()
f.close()

How do I place that in a blob? If I concatenate this to a sql string, how will the database sql parser know where my data string ends, knowing that my data string could contain lots of ' and " and other little things in it. ???
storeString = "insert into mytable (filedata) values ( %s ) " % data

Does that look right? I don't think it would execute correctly.

Thanks
David Stockwell


Jul 18 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: cover | last post by:
The question is, we have two options to store images, either in a Database (MySQL, Postgres, ...) like blob data, or in the hard disk the file and the path in database. Which option is better?...
8
by: Jerry | last post by:
I have an off-the-shelf app that uses an Access database as its backend. One of the tables contains a field with an "OLE Object" datatype. I'm writing some reports against this database, and I...
4
by: clark | last post by:
I have an app that needs to upload images for each active member. Which is the best approach? 1. to store the img directly in SQL Server database or 2. to store path/filename in db and put...
10
by: Peter Stojkovic | last post by:
I want store an integer-array of 1000 Values in a blob in a SQL-database. I will do this every 10 Seconds. How can I do this ???? What datatypes a have to use ??? Thanks
0
by: Wescotte | last post by:
I'm abit confused on how to work with binary data with an ODBC connection (My database is DB2 btw) Say I have a table like CREATE TABLE EJWLIB.BLOBTEST ( ID NUMERIC(5) NOT NULL, FILENAME...
2
by: Trip | last post by:
Hello all, Is there anyway to store an array to MS Access 2002 without parsing the entire array row-by-row. For example, Oracle allows you to store BLOBs (binary large objects). I would like...
4
by: RedHair | last post by:
I'd like to set up a file system for the ASP.NET 2.0 application to store user-uploaded files, since the members are more than 100,000 people, the basic requirements are as below: (1) The file...
5
by: lawrence k | last post by:
I'm a little weak on my basic I/O. Help me out please. Is it right to say that I can just open any file with file(), get it as a string, and then store in a MySql database, in, say, a MediumText...
4
by: =?ISO-8859-1?Q?Hans_M=FCller?= | last post by:
Good morning folks, I cannot read a binary file into a mysql database. Everything I tried did not succeed. What I tried (found from various google lookups...) is this: con =...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.