I am running into issues with POST and PUT for my backend. The relevant table structure can be seen here
DROP TABLE IF EXISTS coordinate;
CREATE TABLE coordinate
(
id int(10) NOT NULL auto_increment,
longitude double(8,4) NOT NULL,
latitude double(8,4) NOT NULL,
measurementId int(10) default NULL,
PRIMARY KEY (id),
FOREIGN KEY (measurementId)
REFERENCES measurement (id)
ON DELETE CASCADE
);
It has a corresponding Model
, which looks like this
public class Coordinate
{
public Coordinate()
{
}
public int ID { get; set; }
public double Latitude { get; set; }
public double Longitude { get; set; }
public int MeasurementId { get; set; }
...
}
I am trying to insert numeric values into coordinate
like so:
sql = "UPDATE coordinate SET longitude = '" + coordinate.Longitude + "," + " latitude = " + coordinate.Latitude + "," + " measurementId = " + coordinate.MeasurementId + "' WHERE id = " + id.ToString();
Notice the inclusion of the single bracket before the double bracket after longitude = '"
and + "' WHERE ...
When I run this with a debugger the string sql
is constructed like so:
sql = "UPDATE coordinate SET longitude = '55,6, latitude = 70,33, measurementId = 2' WHERE id = 2'
And my backend throws the following error:
{
"Message": "An error has occurred.",
"ExceptionMessage": "Data truncated for column 'longitude' at row 1",
"ExceptionType": "MySql.Data.MySqlClient.MySqlException",
...
}
My hunch is that the sql
string is not constructed properly because of the inclusion of the single bracket, so I remove it like and construct a new sql
string like so:
sql = "UPDATE coordinate SET longitude = " + coordinate.Longitude + "," + " latitude = " + coordinate.Latitude + "," + " measurementId = " + coordinate.MeasurementId + " WHERE id = " + id.ToString();
Notice the removal of the single bracket '
in the places it was present before. Now, in the debugger, the string sql
is constructed like so:
sql = "UPDATE coordinate SET longitude 55,6 latitude = 70,33, measurementId = 2 WHERE id = 2"
It's my impression that the numeric values should be separated by a .
and not a ,
, which could by why the backend now throws this error:
{
"Message": "An error has occurred.",
"ExceptionMessage": "You have an error in your SQL syntax; check the manual
that corresponds to your MySQL server version for the right syntax to use
near '6, latitude = 70,33, measurementId = 2 WHERE id = 2' at line 1",
}
I am a bit at a loss on how to then handle the insertion of numeric values in my database. Any help is much appreciated.