golang-mysql驱动程序-数据库功能 [英] golang - mysql driver - database functions

查看:172
本文介绍了golang-mysql驱动程序-数据库功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个存储空间类型的结构,并创建了一个扫描功能来帮助查询数据库中的行.我在插入此类型时遇到问题.

I have created a struct to store spatial types and I have created a scan function to help query rows in my database. I am having issues inserting this type.

我可以使用以下sql插入数据;

I can insert data using the following sql;

INSERT INTO 'table' ('spot') VALUES (GeomFromText('POINT(10 10)'));

如果我在 database/sql/driver 中使用Value接口;

If I use Value interface in database/sql/driver;

类型值接口{}

type Value interface{}

值是驱动程序必须能够处理的值.它可以是nil或以下类型之一的实例:

Value is a value that drivers must be able to handle. It is either nil or an instance of one of these types:

int64

float64

布尔

[]字节

除Rows.Next之外的所有地方的字符串[*].

string [*] everywhere except from Rows.Next.

时间.时间

并使用此代码;

func (p Point) Value() (driver.Value, error) {
    return "GeomFromText('" + p.ToWKT() + "')", nil
}

我最终将以下sql语句发送到数据库;

I end up with the following sql statement going to the database;

INSERT INTO 'table' ('spot') VALUES ('GeomFromText('POINT(10 10)')');

问题是函数 GeomFromText 用引号引起来.有办法避免这种情况吗?我正在使用gorm并尝试将原始sql查询保持在最低水平.

The issue being that the function GeomFromText is in quotes. Is there a way to avoid this scenario? I am using gorm and trying to keep raw sql queries to a minimum.

在数据库端使用的mysql类型是 point .

The mysql type being used on the database end is a point.

推荐答案

请参阅下面的两个URL,这些概念是从该URL挖来的

Please see the two urls below where the concept was poached from

-- http://howto-use-mysql-spatial-ext.blogspot.com/

create table Points
(   id int auto_increment primary key,
    name VARCHAR(20) not null, 
    location Point NOT NULL, 
    description VARCHAR(200) not null, 
    SPATIAL INDEX(location),
    key(name)
)engine=MyISAM; -- for use of spatial indexes and avoiding error 1464

-- insert a row, so we can prove Update later will work
INSERT INTO Points (name, location, description) VALUES 
( 'point1' , GeomFromText( ' POINT(31.5 42.2) ' ) , 'some place');

更新声明

-- concept borrowed from http://stackoverflow.com/a/7135890
UPDATE Points 
set location = PointFromText(CONCAT('POINT(',13.33,' ',26.48,')'))
where id=1;

验证

select * from points;

(当您打开值编辑器"以查看blob时,该点将被更新)

(when you open the Value Editor to see the blob, the point is updated)

因此,最主要的是在update语句中使用concat()进行操作.

So, the takeaway is to play with the concat() inside of the update statement.

这篇关于golang-mysql驱动程序-数据库功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆