Incorrect decimal (integer) value: ' ' mySQL - mysql

In mySQL workbench database, one of the tables has latitude, longitude and district attributes
lat: decimal (10,8)
lng: decimal (11,8)
district: int(4)
I'm trying to import data from .csv file to this table
ERROR 1366: 1366: Incorrect decimal value: '' for column 'lat' at row 1
SQL Statement:
ERROR 1366: 1366: Incorrect integer value: '' for column 'district' at row 1
SQL Statement:
INSERT INTO `db`.`myTable` (`id`, `name_en`, `icon`, `lat`, `lng`, `district`, `city`, `postal_code`)
VALUES ('686', 'Name',?, '', '', '','1', 'P.O. Box 1111')

You have strict sql mode enabled, and you try to pass an empty string ('') as value for decimal fields in the insert. Empty string is an invalid value for a numeric field and in strict sql mode mysql generates an error if you try to insert an invalid data into a column, rather than providing a warning and use the default value (0 for numeric columns) of the particular column's data type:
Strict mode controls how MySQL handles invalid or missing values in
data-change statements such as INSERT or UPDATE. A value can be
invalid for several reasons. For example, it might have the wrong data
type for the column, or it might be out of range. A value is missing
when a new row to be inserted does not contain a value for a non-NULL
column that has no explicit DEFAULT clause in its definition. (For a
NULL column, NULL is inserted if the value is missing.) Strict mode
also affects DDL statements such as CREATE TABLE.
If strict mode is not in effect, MySQL inserts adjusted values for
invalid or missing values and produces warnings (see Section
13.7.5.40, “SHOW WARNINGS Syntax”). In strict mode, you can produce this behavior by using INSERT IGNORE or UPDATE IGNORE.
Remove the strict sql mode for the session before starting the import:
SET SESSION sql_mode = ''

Remove STRICT_TRANS_TABLE from global sql_mode. You might required other attributes such as NO_ZERO_DATE in global sql_mode if already set.
Set this under [mysqld] in my.cnf (or /etc/mysql/mysql.conf.d/mysqld.cnf) depending on your server version.
[mysqld]
sql_mode = "NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION"

All I did was change my query from INSERT INTO... to INSERT IGNORE INTO... Worked perfectly.

Related

Mysql enum field problems

in a mysql table I have a field of type Enum ('S', 'N') with default value = S. Now I have checked this table and I find many records that have no value in this field. How is it possible? I wanted to force the values of this field to be only S and N. Where did I go wrong?
Mysql version 5.0.92
my field definition:
`conteggia` enum('S','N') NOT NULL default 'S'
From the Empty or NULL Enumeration Values documentation:
If you insert an invalid value into an ENUM (that is, a string not present in the list of permitted values), the empty string is inserted instead as a special error value. ...
If strict SQL mode is enabled, attempts to insert invalid ENUM values result in an error.
So you must not have strict mode enabled, and due to some error in your code it assigned invalid values to the column.

How restrict mysql tiny-int to accept just integer

Im trying to get eeror about invalid data for a query but it seems mysql accept strings as tiney in queries and set 0 instead of returning any error.
So how can I restrict it to just accept integers.
Thanks in advance.
This is SQL Mode-dependent.
In strict mode you must receive either 'Data truncated' (inserted value starts from numeric chars) or 'Incorrect integer value' (first symbol is not numeric) error message, and none data must be inserted.
If strict mode is disabled then warning is generated instead of error, and the value is converted to numeric (all symbols starting from first non-numeric one are truncated, then the value is converted to number, if it is empty string then zero is assigned).
Data type checking (and convertion if needed) is performed firstly, even before BEFORE INSERT trigger.
DEMO

Incorrect string value errors in AES_ENCRYPT/AES_DECRYPT

I'm introducing myself to the encryption functions in MYSQL.
Just wrote a simple SQL statement to add an encrypted entry into a field
INSERT INTO test_table (field1) VALUES(aes_encrypt('fieldentry','password'))
When I execute the SQL I get the following error
Error 1366: Incorrect string value: '\xC7\xE13\xC4\xF4!...' for column 'field1' at row 1 SQL Statement - CHANGE COLUMN field1 VARCHAR(255) NOT NULL COMMENT ''
Now I've read it may have something to do with CHARACTER SET, and tried changing it from utf8-default collation to utf8mb4 - default collation as recommended, but that didnt make any difference.
I've also tried changing the column type from VARCHAR to VARBINARY. The SQL statement then ran successfully, however, when I tried the following to retreive the data:
SELECT AES_DECRYPT(field1, '12345') AS endata FROM test_table
Do Until rst.EOF
Response.Write(rst("endata"))
rst.movenext
Loop
The loop runs but no values are returned (blank lines)
I'm just looking for a straightforward what to encrypt and then decrypt my data using a password in the function AES_ENCRYPT/AES_DECRYPT.
AES_ENCRYPT() encrypts a string and returns a binary string.
AES_DECRYPT() decrypts the encrypted string and returns the original
string.
So you should change the type of field1,firstname from VARCHAR(255)to VARBINARY(255). It will fix the issue..
EDIT : For type missmatch.. please try this..
SELECT *,
CAST(AES_DECRYPT(field1, 'mypassword') AS CHAR(50)) end_data
FROM user
Hope this helps..

Is a non-nullable, MySQL enum column guaranteed to only contain those values?

If I have a column in a MySQL database that is:
Non-nullable
Is an ENUM with 5 possible values
Has a default value of 1 of those ENUM's
Can it be guaranteed that there can never be a value in that column apart from one of those ENUM's?
Not by default - there is one more value your column can have in addition to the values of the enum, an empty string in the column.
You have to enable strict SQL mode in MySQL to have the guarantee that only the enum values (or NULL if the column is nullable ) can occur.
Taken from here
If you insert an invalid value into an ENUM (that is, a string not
present in the list of permitted values), the empty string is inserted
instead as a special error value. This string can be distinguished
from a “normal” empty string by the fact that this string has the
numeric value 0. See Section 11.4.4, “ Index Values for Enumeration
Literals ” for details about the numeric indexes for the enumeration
values.
If strict SQL mode is enabled, attempts to insert invalid ENUM values
result in an error.
If strict SQL mode is enabled, attempts to insert invalid ENUM values result in an error.
Source

MySQL Enum's always contain '' (empty string) in possibilities

I'm trying to create a simple 'yes'/'maybe'/'no' Enum in MySQL with PhpMyAdmin
I set NULL to No, and 'maybe' as the default value
I am expecting an error when executing something like "SET EnumCol=''", because '' (an empty string) should not be a valid value.
But the query gets executed and the value gets set to '' - which means I'm forced to double check for this unwanted and illegal value whenever I read from the database!
Is this a bug in MySQL or PhpMyAdmin?
Does anyone know a way of disabling this behavior?
Thanks.
Empty string is error indicator of invalid values in ENUM. From mysql ENUM type manual:
If you insert an invalid value into an ENUM (that is, a string not present in the list of allowed values), the empty string is inserted instead as a special error value. This string can be distinguished from a “normal” empty string by the fact that this string has the numerical value 0. More about this later.
To disable this behaviour:
If strict SQL mode is enabled, attempts to insert invalid ENUM values result in an error.
To enable strict mode see Server SQL Modes.
ENUM's are a pain in the butt. unless you also need to set the value by a number, i would stay away from them.
instead, use a varchar column with a foreign key to a lookup table to restrict the values. that will make it impossible to insert a bad value.