Well, I made this great little application that writes to and updates a table in MySQL, but now I have more than one user making updates, and while I feel that the chance of a corruption is slim, I want to be able to lock the table for all other users(threads), write the information to the DB, then unlock the tables.

I am using DBD::mysql. Below is an sample of the code I used to write to the DB:

my $sth = $dbh->prepare("$SQLCommand") or err_trap("Cannot prepare the + load_category_names query"); $sth->execute or err_trap("Cannot execute the SQL query to SQL COMMAND +");

Simple stuff! Yet if I copy these lines above and use LOCK TABLES table read, I cannot write as I am locking all data.

I understand I have a two part issue here:
1) proper perl syntax to write to the database three times
a. lock tables
b. execute my sql command
c. unlock talbes

2) Determining the proper syntax of the lock and unlock to get me where I need to be.

For Perl, do I just copy the two lines of code, replace $SQLCommand with appropriate lock statement. Use the same two lines of code to write my SQLCommand. Then, use the same two lines of code replacing $SQLCommand again with UNLOCK TABLES?

Also if any one has some sample SQL lines on locking procedures *I know this is not a MySQ site, but what the heck, it is related. Thanks folks.

Edited: Sun Oct 14 07:11:57 2001, footpad - Added/Corrected <P> tags and removed repeated paragraphs.


In reply to LOCK TABLES using Perl in MySQL by sdyates

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



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.