473,402 Members | 2,064 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,402 software developers and data experts.

How to read a binary file into a mysql table

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(), ))

also this doesn't work:

execute("insert into data values('file1', %s), (MySQLdb.escape_string(open("test.jpg", "rb").read()), ))

I always get this:

Warning: Data truncated for column 'file' at row 1

The blob data is actually chopped.

The Table has two columns, char(100), blob

Has someone a working idea how to get binary file into a blob using MySQLdb and python ?!

System is SuSE 10.0 Linux with python 2.5.1, current MySQLdb version, MySQL is: 5.0.26

Thanks a lot!

Greetings

Hans

Dec 14 '07 #1
4 7210
En Fri, 14 Dec 2007 12:19:41 -0300, Hans Müller <He******@web.deescribió:
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(), ))
Try wrapping the file contents with a Binary object (untested):

data = MySQLdb.Binary(open("test.jpg","rb").read())
cur.execute("insert into data values('file1', %s)", (data,))

--
Gabriel Genellina

Dec 14 '07 #2
On Dec 14, 5:41 pm, "Gabriel Genellina" <gagsl-...@yahoo.com.ar>
wrote:
En Fri, 14 Dec 2007 12:19:41 -0300, Hans Müller <HeinT...@web.deescribió:
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(), ))

Try wrapping the file contents with a Binary object (untested):

data = MySQLdb.Binary(open("test.jpg","rb").read())
cur.execute("insert into data values('file1', %s)", (data,))

--
Gabriel Genellina
I was suprised at what I could stick into a MySQL database. Also, you
might wanna compress the binary for database performance.
Dec 15 '07 #3
Hello,

thanks a lot for the Binary(). This does the trick, now I can import all my binary data.
But now I found the next problem:
The Blob is now limited to 65535 Bytes. I love all these stone age (16bit) limits on my 64bit machines...
All bigger files a truncated.

Has someone an idea how to solve this ?
Greetings

Hans
Dec 18 '07 #4
Sorry, I found the mistake:

There is more than one blob type, blob as a default stores only 64k of data.
LONGBLOB has a 4G limit which is Ok for my purposes.

Happy Christmas to all,

Hans
Dec 18 '07 #5

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

Similar topics

8
by: Gurk | last post by:
hi the settings of my program are stored in an ini file, is there a simple way to open the file in some textbox, ore a betteer soutable box. i also want it to be able to resave the ini so...
0
by: Ivan Horvath | last post by:
Dear All, i would like to store binary file contents to a mysql database in a mediumblob fields. i've got problem when there is a simple quote, double quot, backslash or NUL character in the...
0
by: JL | last post by:
Platform: Linux Red Hat RHEL 3 (and red hat 9) Installed MySQL from source. As a matter of fact, installed all LAMPS from source, and the mysql socket file was arranged in a place other than...
0
by: Jean Hagen | last post by:
I'm trying to write a script to weekly remove MySQL logs, the general, error and binary log files. Following the MySQL documentation, I've written a script that moves all current log files, then...
0
by: Bennett Haselton | last post by:
I'm trying to port my MySQL tables for a database called "tracerlock" from one server to another. On the old server, in the /var/lib/mysql/tracerlock directory, there was a ".MYD", ".MYI" and...
0
by: David List | last post by:
I am wondering what I miss to be able to handle binary data from the mysql client. I have ensured that the user has file_priv set to 'Y' and that max_allowed_packet is larger that the binary lumps...
3
by: Me Alone | last post by:
Hello: I am trying to edit some C code I found in "The definitive guide to using, programming, and administering MySQL" by Paul DuBois. This C client program connects and then segfaults when...
3
by: =?Utf-8?B?ZGF2aWQ=?= | last post by:
I try to follow Steve's paper to build a database, and store a small text file into SQL Server database and retrieve it later. Only difference between my table and Steve's table is that I use NTEXT...
2
by: tech101 | last post by:
Can I get the master (or slaves) to automatically remove the binary logs once they are processed by all slaves? It says in the mysql manual : If you are using replication, you should not delete...
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
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
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,...
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
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.