473,766 Members | 2,180 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 5590
> 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("INS ERT INTO picture_table (image) VALUES (?)",
(ThinkSQL.Binar y(blobdata),))

Regards,
Greg Gaughan
www.thinksql.co.uk
"David Stockwell" <wi*******@hotm ail.com> wrote in message
news:ma******** *************** **************@ python.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
3682
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? When? Why? Thanks you for your answers.
8
25403
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 believe this field contains data I need. When I view the table in datasheet view, all I can see in this field is the string "Long binary data". So, I've got the problem of needing to extract data from this field, but I don't know what format...
4
4107
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 file on server volume is potentially 10,000+ images
10
5238
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
2348
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 VARCHAR(128) NOT NULL, BINARY BLOB(2M) ) Now I (I assume this is the correct method) insert data in the
2
2768
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 to be able to say in VB/VBA With rstName .AddNew !fieldName = ArrayName .Update
4
2551
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 system is separate with front-end web site (2) Need to re-size user-uploaded image file to same size (3) Need to rename file name to avoid duplicate name (4) How to design directory and file structure to increase disk I/O performance (5) How to...
5
3374
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 field? I realize that MySql supports binary fields, but my current MySql schema does not have any binary fields, and I'm wondering if I can store the file without changing my current schema. Can I recreate the file later, if I wish, just be...
4
7243
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 = MySQLdb.connect(to server) cur = con.cursor() cur.execute("insert into data values('file1', %s)", (open("test.jpg", "rb").read(), ))
0
9571
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9404
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10168
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
9959
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8835
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7381
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6651
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5423
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3532
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.