4


I'm new to Perl programming (and to SO too) so my question may be formulated in a bad way, but I really have read a lot of books and tutorials and I haven't found anything addressing (even mentioning) my problem.

I'm trying to use DBI and SQLite to write some code which retries an insertion query if a recoverable error occurs (DB full or locked, etc.), but dies if the error is unrecoverable (DB deleted or corrupted, etc.).

I've found that the SQLite C interface exports the error codes:

http://www.sqlite.org/c3ref/c_abort.html

but I haven't found anything similar for Perl. I really wish I don't have to use magic numbers in my very first Perl program! :-)

By the way, the documents and examples I saw online explain very well the manual vs. automatic (i.e. with exceptions) error handling in DBI, but none of them shows how to take different actions according to the error type. Isn't this a common use case?
Moreover, they all agree that DBI::err is not the correct variable to tell which error occurred. They more or less implicitly say that DBI::errstr is to be used, but I find it a bit awkward to rely on a string comparison against a human oriented, possibly multi-line error string...

Thanks for any suggestion!

4
  • In most applications, any database error is considered fatal. Commented Apr 30, 2013 at 14:25
  • DBI says the opposite: "The errstr() method should not be used to test for errors, use err() for that, because drivers may return 'success with information' or warning messages via errstr() for methods that have not 'failed'." Commented Apr 30, 2013 at 18:39
  • @charlesbridge It says to use err() to check if there's an error, but not to tell what kind of error occurred. For some drivers err is -1 whatever is the error. This seems not to be true for DBD::SQLite, thus my question: are there symbolic constants to compare err() with? Commented May 1, 2013 at 9:33
  • P.S. this is where I got the info: docstore.mik.ua/orelly/linux/dbi/ch04_05.htm#ch04-ch04_error_1 Commented May 6, 2013 at 14:04

1 Answer 1

1

My work with DBI has almost always been with mysql instead of sqlite3, so I can't speak from direct experience. However, don't feel too bad if you absolutely must check for magic strings and numbers. The key thing is how you do it. Whenever you have to rely on magic strings and/or numbers, put them in a constant or hash in the configuration portion of your script (or perhaps even a configuration file).

The bad thing about magic numbers/strings is that they are hard to manage if they change, or either no one quite remembers what generates that condition, etc. But this is mitigated if you do it correctly and document it.

BTW - if you're just getting started, I strongly recommend reading Damian Conway's, "Perl Best Practices". I checked, and he doesn't address handling magic strings/numbers, but it's still the best book I've read on Perl. Once you go through PBP, take a look at Perl Critic -- it's a wonderful tool that will make you cry. It will flag magic strings, and many, many other things :)

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.