简体   繁体   中英

How can I correctly set the value of a field having type point in a MySql database?

I am not so into geospatial data and I have the following problem. I am using MySql and I am finding some difficulties trying to add the value of a field having point as type in this table rows.

I have a table like this:

CREATE TABLE MarketDetails (
  id              BigInt(20) NOT NULL AUTO_INCREMENT,
  localization_id BigInt(20) NOT NULL,
  market_name     VarChar(255) CHARACTER SET latin1 COLLATE latin1_swedish_ci NOT NULL,
  market_cod      VarChar(255) CHARACTER SET latin1 COLLATE latin1_swedish_ci,
  description     Text CHARACTER SET latin1 COLLATE latin1_swedish_ci,
  gps             point, 
  PRIMARY KEY (
      id
  )
)

As you van see this table contains the gps field having type point . It seems to me that this data type is the best for store the GPS coordinates of a point.

I have tryied to insert the values of this field using an IDE but it doesn't works...using my IDE I can insert all the other fields value but not the gps field value.

How can I try to do it manually? For example I have inserted this record:

id               localization_id      market_name       market_cod       description                  gps                                                                                                                                                                                                                                                            
---------------------------------------------------------------------------------------------------------------
1                31                   Kibuye            cod001           Kibuye Village Market        

How can I correctly create an update SQL query that update this record setting the gps field with these coordinates?:

lat: -2,17897
long: 29,371491

I was trying something like this but it doesn't works:

UPDATE  MarketDetails
SET     gps = Point(ST_GeomFromText('POINT(1 1)'))
WHERE id = 1

I obtain this error message:

#42000You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ') WHERE id = 1' at line 2

What is wrong? What am I missing? How can I fix this issue?

if you are able to import the lat/lon values in separated columns as eg DOUBLE data type, then you can try this:

ALTER TABLE table ADD coords Point;
UPDATE table SET coords = POINT(lat, lon);

In this case I added the column "coords" as a POINT type. Then update the coords column by using the lan and lon columns.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM