473,396 Members | 1,966 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,396 software developers and data experts.

ODBC and storing binary data (specially BLOB)

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
following method

$MyInputStream = fopen($MyFilename, "rb");
$binary_data = fread($MyInputStream, filzesize($MyFilename));
// Do I need to check to make sure the file is not greater than the
alloted 2M or will the SQl statement just fail if I try to insert
something larger?

$query = "INSERT INTO $MYTABLE VALUES ($RECORD_ID, '$MyFilename', ?)";
$prepared_sql = odbc_prepare($MyConnection, $query);

$result=odbc_execute($prepared_sql, array("$binary_data");

// $result never seems to be false even if I try to insert more than 2M
of binary data
// When I attempt to insert more than 2M it seems to simply skip
inserting the entire row itself but doesn't display any error message
if ($result === false)
echo "Failed SQL statement $query " . odbc_errormsg();

Now, when I want to retrieve my binary data I do so like

$query = "SELECT * FROM $MYTABLE WHERE ID = ?";
$prepared_sql = odbc_prepare($MyConnection, $query);
$result = odbc_execute($prepared_sql, array("$RECORD_ID"));

while (odbc_fetch_row($result)) {
$filename = odbc_result($result, "FILENAME");
$OutputStream= fopen($filename, "wb");

$binary = odbc_result($result, "BINARY");
fwrite($OutputStream,$binary);

fclose($OutputStream);
}

The other issue I have is the max amount of binary data I seem to be
able to retrieve is 4K. Any binary data i insert with the above method
when I attempt to retrieve it I get the correct resutls however I only
get the first 4K (or the entire file if it was originally under 4K).
Any ideas?

Nov 30 '05 #1
0 2316

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

Similar topics

6
by: Juergen Gerner | last post by:
Hello Python fans, I'm trying and searching for many days for an acceptable solution... without success. I want to store files in a database using BLOB fields. The database table has an ID field...
2
by: David Horowitz | last post by:
Hi folks, I want to be able to store and retrieve UNSAVED Word documents as BLOBs. I got all the info for storing them if they're already saved on the file system. But what if they're not...
11
by: bissatch | last post by:
Hi, I am trying to upload an image, create a new file based on that image and then store the base64 encoded image data in a database. I dont really know where my code is going wrong so I will...
3
by: hamvil79 | last post by:
I'm implementig a java web application using MySQL as database. The main function of the application is basically to redistribuite documents. Those documents (PDF, DOC with an average size around...
4
by: Hazzard | last post by:
What is the best way to do this? Binary with 0 representing off and 1 on? Int16 with 1 representing first button, 2 the second, 3 ... varchar with a character values at certain positions in the...
3
by: Chris | last post by:
Hi all, I am trying to use System.Data.ODBC to insert a file into a binary field in SQL Server 2000. Now, before you yell at me, the reason I am doing this is that I am only writing intitial...
0
by: NM | last post by:
Hello, I've got a problem inserting binary objects into the postgres database. I have binary objects (e.g. images or smth else) of any size which I want to insert into the database. Funny is it...
8
by: Godzilla | last post by:
Dear all, I cannot find a solution for my problem with inserting a blob object (>4000 in length) into an ORACLE database via ODBC. I have tried the two ways of inserting the blob object (a...
3
by: rustyhowell | last post by:
I'm trying to store binary data in a sqlite database and call into the db using pysqlite 3. What I've got so far is this: import sqlite con = sqlite.connect(DB_PATH) cur = con.cursor() query...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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,...
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
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...
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,...

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.