Logging query errors in MySQL - mysql

We have an application that uses several SQL queries and might at times generate the odd error.
For example, it could be a :
INSERT INTO TABLE (ID, FIELD) VALUES (1, "field value");
which would result in a:
ERROR 1062 (23000): Duplicate entry '1' for key 'PRIMARY'
Because the unique primary key constraint has been violated.
Is it possible in MySQL to somehow log the error along with the query that caused it? I have tried to enable the error-log and general-log in the /etc/mysql/my.cnf but it never produced the expected result. I could enable logging of every query without their errors (pretty useless for us, we're only interested in queries that result in errors).
The errors can be caught by the applications (in our case they are, we are using Perl DBI), however when there are several statements in a stored procedure then we do not know which one as the error message does not include the text of the query, or even the name of the table involved. This makes troubleshooting quite difficult.
I am sure I am missing something obvious. For example, in Oracle this is the default behavior, query errors are logged into a text file where they can be easily identified.

This is a client thing. Isolate database accesses in an access layer and generate the log on the client. The database cannot log this.

Related

MySQL/MariaDB Configure Warnings to throw Errors

I need MySQL/MariaDB to throw error messages instead of warning messages.
Reason: In my development environment, warnings are ignored in SQL/PHP (and both SQL and PHP continue processing), therefore I can't detect any issues when testing. In production, however, all warnings crash the query, thus terminating the API, and returning a "failure" message to users.
This is a very bad result and caused many headaches in the past.
Here is a very specific scenario that caused many problems:
I have a table "testtable" with two columns "pk" and "bRequiredBoolean", where pk is the primary key (thus auto increments) and bRequiredBoolean HAS NO DEFAULT VALUE:
CREATE TABLE `test`.`testtable` ( `pk` INT NOT NULL AUTO_INCREMENT , `bRequiredBoolean` BOOLEAN NOT NULL , PRIMARY KEY (`pk`)) ENGINE = InnoDB;
In my development server, I can run the following query:
INSERT INTO `testtable`() VALUES ()
and receive the following warning:
Warning: #1364 Field 'bRequiredBoolean' doesn't have a default value
In my development server, the entry IS INSERTED and the API continues executing.
In my production server, the entry ISN'T INSERTED and the API crashes.
I know that the development/testing servers should be identical to prevent issues like this, but I currently don't have the funds for extra licenses, so I would like an alternative solution to throw error messages instead of warnings to catch scenarios like the one mentioned above.
Important note: I don't want to insert any extra code within each query, I'd prefer to edit some configuration file. For instance, I don't want to add the following code TO EVERY SINGLE QUERY:
SHOW COUNT(*) WARNINGS
I'd rather edit some configuration file so it will always throw an error, even if I do ad-hoc queries directly through the GUI.
I found other similar questions, but nobody has responded to those threads:
MariaDB shows warnings instead of errors
Throw error on mysql warning pdo
Any help is much appreciated.
Thanks to Barmar's comment, I found the solution to my problem.
You can check your SQL_Mode via:
SELECT ##SQL_MODE;
Apparently, there is a SQL_Mode setting that specifically throws errors for all warnings called "Traditional"
You can view the official mySQL website for more info regarding SQL_Mode values: https://dev.mysql.com/doc/refman/5.7/en/sql-mode.html#sql-mode-full
So I ran the following query:
SET GLOBAL sql_mode = 'TRADITIONAL';
After running the query, I executed the same query as before:
INSERT INTO `testtable`() VALUES ()
and received the following error:
Error
SQL query: Copy
INSERT INTO `testtable`() VALUES ();
MySQL said: Documentation
#1364 - Field 'bRequiredBoolean' doesn't have a default value
This is exactly what I needed. Thank you so much #Barmar!

MySQL UPDATE statement is throwing "Column count doesn't match value count" error

(NOTE: I know this is an error that's commonly asked about, but most of the time, the issue is in an INSERT statement. I couldn't find a question on this website where this error happened during an UPDATE.)
I have a table in MySQL (InnoDB / v. 5.7.19) called RESULTS which has, among others, two columns called TYPE and STATUS. Both are of type ENUM, with PASS, FAIL and IGNORE being the supported values in both. I'm trying to run this UPDATE statement on that table, using Workbench (also tried the same directly on the DB machine, using the mysql command):
update `RESULTS` set `TYPE`='IGNORE' where `STATUS`='IGNORE';
I'm getting this error:
Error Code: 1136. Column count doesn't match value count at row 1
Changing the single quotes to double quotes didn't help. I'm able to run this query successfully:
select count(`TYPE`) from `RESULTS` where `STATUS`='IGNORE';
I'm probably making a silly mistake here, but can anyone point out what's wrong with the UPDATE statement?
As requested I am posting it as an answer.
The error basically is self-explanatory like performing an operation on set of attributes but the values provided in the query are not enough. But in your case, you are performing an update operation with all attributes and their values and still, this error appears it may be a case that there is some trigger is registered for this table probably on before/after the event, If that is the case you need to update or remove that trigger if no needed.

Why am I getting a table doesn't exist error for a table that exists?

I am running a simple update statement:
UPDATE sometab
SET `somefield1` = '19',
`somefield2` = '3734941'
WHERE somefield3 = '1234';
and I am getting the error:
ERROR 1146 (42S02): Table 'prod._sometab_new' doesn't exist
I can successfully select from the table where somefield3 is 1234.
Why am I getting a table doesn't exist error for a table that exists? And why does the error message refer to a different table? I don't see any triggers associated with the table.
Additional information: A colleague just noticed that it is referring to a prod scheme, but the statement is running in a dev schema built from prod. The update statement works in DBs that were built a few days ago using the same method, but all of the DBs built after some, as of yet, unknown time exhibit the error.
The current theory is that a conversion script to move us to UTF-8 is currently running and creating tables like _ORIG_new as part of its conversion. We are going to wait for the conversion script to finish and then rebuild the dev databases and see if the error still persists.
Does this happen if you also try Insert into or Delete statements ?
Insert INTO sometab(somefield1, somefield2) VALUES (a, b).
If that works you should not have problems probably, otherwise you have problems accessing your database.
Second, are you sure you are using the correct database file and that are you connected to it properly. If you are using it in external application (c#), check your connection strings.
Also check how are you executing the query. I cant think of other more specific solution to your problem

Show human error if inserting duplicate data in access database

I've prohibited duplicate records in access database so no one will insert same data again and again. it works fine, but the error message is not helpful to the users, i want to show that error in a more human way possible.
Microsoft JET Database Engine (0x80004005)
The changes you requested to the table were not successful because
they would create duplicate values in the index, primary key, or
relationship. Change the data in the field or fields that contain
duplicate data, remove the index, or redefine the index to permit
duplicate entries and try again.
Instead of this lengthy message i just want to show "Sorry - Record already exist."
Is this possible?
Two ways to do this:
Catch the error. I think classic ASP requires on error resume next and then you need to check the err message. You might even have to match the string since I think the error code will be the same for different errors.
Do a select from the database before the insert to check if the value already exists
Either way you can then display your own message. Might be even more user-friendly to customize the message further ("Sorry - species 'Gruffalo' already exists").

MySQL stored procedure, do not ever raise error?

I would like to make a Stored procedure in MySQL with an try/catch to prevent the error going to my java project. But since there is no try/catch in MySQL im searching for alternatives.
The only thing i need to prevent, is to dont get a "Duplicate key" error from mysql in my java project. If i get this error, i dont want to show anything and act like it has inserted normal.
Is this possible?
Why don't you catch this error withi your application?
Anyway, from docs: http://dev.mysql.com/doc/refman/5.5/en/insert.html
If you use the IGNORE keyword, errors that occur while executing the
INSERT statement are treated as warnings instead. For example, without
IGNORE, a row that duplicates an existing UNIQUE index or PRIMARY KEY
value in the table causes a duplicate-key error and the statement is
aborted. With IGNORE, the row still is not inserted, but no error is
issued
.