Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
  1. You don't need anything extra special to read 1MB chunks of a file at a time, the read function can do that quite easily.
  2. If you use placeholders in your SQL (and you always always always should whenever possible), you never have to worry about special/dangerous characters in the data you're inserting into the DB.

Here's a little demo that reads 1MB at a time, and inserts it into the DB using placeholders.

open my $fh, 'big-binary-data.file' or die "Couldn't open file: $!"; # prepares an SQL statement with data to be filled in later my $sql = "insert into table set chunk_num=?, data=?"; my $sth = $dbh->prepare($sql) or die "Couldn't prepare sql statement: +$!"; my $chunk_num = 0; my $chunk_size = 1024 * 1024; while ( read( $fh, my $buffer, $chunk_size ) ) { $chunk_num++; # executes the statement with the appropriate data filled in $sth->execute( $chunk_num, $buffer ) or die "Couldn't insert data: $!"; } $sth->finish;
This should give you a decent starting place. I highly recommend reading the database tutorials here on PM (as well as the DBI documentation) if you're a little unsure of what placeholders do. After understanding them, you will quickly realize their usefulness.

blokhead


In reply to Re: Inserting binary data into MySQL by blokhead
in thread Inserting binary data into MySQL by markolus

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others scrutinizing the Monastery: (3)
As of 2024-04-25 17:29 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found