Datatype to Store Longitude/Latitude in Mysql

CodeTalk picture CodeTalk · Mar 5, 2012 · Viewed 13.9k times · Source
'43.005895','-71.013202'

Trying to use:

INSERT INTO table(fanDetLocZip, fanDetLocCity, fanDetLocState, fanDetLocLat, fanDetLocLong, fanDetLocTZ, fanDetLocDST)  
VALUES(00210, 'Portsmouth', 'NH', '43.005895', '-71.013202', -5, 1);

I'm currently using the datatype SPATIAL, GEOMETRY.

Its giving me errors like:

Cannot get geometry object from data you send to the GEOMETRY field

All the values have 2 digits, and 6 decimal places after decimal. How do I store this in mysql?

Error I get when I use:
INSERT INTO Table(fanDetLocZip, fanDetLocCity, fanDetLocState, fanDetLocLatLong, fanDetLocTZ, fanDetLocDST)
VALUES(00210, 'Portsmouth', 'NH', point(43.005895,-71.013202), -5,1)

Error Image:img5

Answer

ypercubeᵀᴹ picture ypercubeᵀᴹ · Mar 5, 2012

You can use POINT() to store into a column of type GEOMETRY or POINT:

POINT(43.005895, -71.013202)

If the Geometry column is named geom, you can use this:

INSERT INTO table
    ( ..., geom, ...) 
  VALUES
    ( ..., POINT(43.005895, -71.013202), ...)

If you want to show data stored, you can use the X() and Y() functions:

SELECT X(geom) AS x, Y(geom) AS y
FROM table