MySql Data truncated for column 'value' at row 1 - java

In my java web application, I am inserting some records into MySql db. I have a column name 'value' whose Field type is float. When I insert the following value 76.8653846153846 into db, it gives exception of Data truncated for column 'value' at row 1. I changed field type to Double but same message. Any idea?
Field description:

As per the documentation you must define your number type with the level of precision you require.
for your number 76.8653846153846 use
FLOAT(10,13)
The 13 being large enough to handle your .8653846153846
[Edit]
to alter your existing table execute this command substituting your table and column name for mytable and mycolumn respectively
ALTER TABLE mytable MODIFY mycolumn FLOAT(10,13)

This comes from the database engine where the declared field's description in the table is not big enough to insert the complete data

Related

The value in the generated column does not change

I created the generated column "available_quantity" -
alter table products_regions
add column available_quantity int8
GENERATED ALWAYS AS (quantity_total - quantity_ordered) STORED;
When performing some logic, only the "quantity_ordered" column changes.
But I get an
ERROR: the column "available_quantity" can only be assigned the DEFAULT value. The column "available_quantity" is generated.
I don't write anything directly into this field.
If you change the "quantity_ordered" field in the database manually, then "available_quantity" is calculated
How can I solve the problem?
The solution has been found.
If there is a calculated field in the Postgresql database and ORM Hibernate, then at the entity level it is required to specify an
#Column for this field (name = "your name", inserted = false, updated = false).
You cannot modify a generated column. The documentation is pretty clear about that:
A generated column cannot be written to directly. In INSERT or UPDATE commands, a value cannot be specified for a generated column, but the keyword DEFAULT may be specified.
Use a regular column and fill it with a trigger during INSERT.

Does the JDBC specification define what happens if you call `ResultSet.getObject(String)` for a query which has conflicting column names?

I found a SQL query in some Java code that looks like this:
SELECT * FROM tableA JOIN tableB ON ...
Both tableA and tableB contain the field named id, and the Java code that executes this query does this:
Integer id = (Integer)rs.getObject("id");
Does the JDBC spec have anything to say about which field value will be returned? I haven't been able to find anything that says one way or another. I see that some databases throw an error but MySQL/MariaDB (the database in use, here) do not complain and the value returned seems to be tableA.id, or the "leftmost" field whose name is "id".
I'm asking if there is spec-defined behavior in this case, or if it's up to the database and/or JDBC driver to decide how to behave.
Needless to say, I'll be fixing this to work predictably and unambiguously while I'm looking at the code, but I'm curious as to the answer.
The API documentation of ResultSet answers your question:
Column names used as input to getter methods are case insensitive.
When a getter method is called with a column name and several columns
have the same name, the value of the first matching column will be
returned.
The JDBC 4.3 specification document in section 15.2.3 Retrieving Values also says:
The columns are numbered from left to right, as they appear in the
select list of the query, starting at 1.
Column labels supplied to getter methods are case insensitive. If a
select list contains the same column more than once, the first
instance of the column will be returned.
The index of the first instance of a column label can be retrieved
using the method findColumn. If the specified column is not found,
the method findColumn throws an SQLException.
So, if a result set has multiple columns with the same (case insensitive(!)) name, then the value of the first matching column is returned.

Postgresql, get generated sequence after insert with JDBC

We have a java database abstraction that does a number of inserts for us. At runtime we'll know the table name, the column names to insert and the values. From that we generate a prepared statement and do the insert.
In sql server land we would tack on select id = ##identity to the end of the generated sql to get the newly generated id returned by the query.
Now that we're migrating to postgres this no longer works. It's my understanding that in postgres you can do ,
insert into foo(a, b) values('a', 'b') returning ID
Our problem is that at runtime we don't know the name of the ID column nor do we know the name of the sequence. Is there any way to generically get the value of the newly inserted sequence without knowing the name of the sequence or the name of the column?
If your insert is not triggering further inserts, you can use SELECT LASTVAL(); right after your insert statement

Oracle Number datatype value insertion

I am executing the following query, but it is inserting partial value of first field, i.e 95362 instead of 95362-07
Insertion query::
INSERT INTO ACCIDENT VALUES(95362-07,'Orthoptic education on visual ergonomics','Orthoptic education on visual ergonomics');
Getting values in the table ::
95362,Orthoptic education on visual ergonomics,Orthoptic education on visual ergonomics.
Table structure:
desc accident
Name Null Type
--------------------------------------------------
ID NUMBER
ACC_NAME VARCHAR2(4000)
ACC_DESC VARCHAR2(4000)
Here's the problem, your first column is expecting a data type of Number
desc accident
Name Null Type
--------------------------------------------------
ID NUMBER
ACC_NAME VARCHAR2(4000)
ACC_DESC VARCHAR2(4000)
And then, when you insert your query, the first parameter is being computed only the NUMBER part of it, which is 95362.
To store your ID records in the form XXXXX-XX you must change your ID column to a VARCHAR2 data type. Otherwise you'll keep getting this result.
I hope it helped. Cheers

order by primary key

I'm trying to do a comparison between the data of a table of a SQLServer database before and after some action.
I'm saving the first data in a file (actually I'm saving the MD5 of the data, but not relevant), so I can compare the new data with it after the action.
The first problem is that the data returned by the query is not always in the same order.
So to solve this problem I thought about using the ORDER BY clause to order the data.
Here comes the second problem. I'm executing a query defined by the user, so the query, and the table may be different.
So here is the question: How can I ORDER BY the PRIMARY_KEY (one or multiple) without previous knowledge of what table will be used??
Any other solution will be also welcomed,
Thanks for your time and effort,
You can use dynamic sql and query the meta tables to discover what the primary key is
SELECT column_name
FROM INFORMATION_SCHEMA.KEY_COLUMN_USAGE
WHERE OBJECTPROPERTY(OBJECT_ID(constraint_name), 'IsPrimaryKey') = 1
AND table_name = 'Person'
With dynamic sql you construct your sql statement like a string and hand it over to sp_executeSQL
More info at http://msdn.microsoft.com/en-us/library/ms188001.aspx
I'm trying to do a comparison between the data of a table of a
SQLServer database before and after some action.
If you are using SQL Server 2008, you can use CDC
Do you really need to order by the primary key? If not you can do
ORDER BY 1, 2, 3
that will order by column 1, 2 and 3 regardless the name of the column.
That would work on the PK if you have your Primary key on the first column of the table

Categories