首页 文章

golang - mysql驱动程序 - 数据库函数

提问于
浏览
1

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

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

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

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

type Value interface {}值是驱动程序必须能够处理的值 . 它是nil或这些类型之一的实例:int64 float64 bool [] byte string [*]除了Rows.Next之外的任何地方 . 了time.time

并使用此代码;

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

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

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

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

在数据库端使用的mysql类型是一个点 .

1 回答

  • 1

    请参阅以下两个网址,其中挖掘了这个概念

    架构

    -- 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时,该点会更新)

    因此,最重要的是使用更新语句中的 concat() .

相关问题