Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use gorm statements

2025-04-05 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article introduces the relevant knowledge of "how to use gorm sentence". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

Gorm

SQL spelling

SQL execution

Prometheus monitoring

1.SQL spelling

Gorm/tree/master/callbacks package implements CURD.

Select

Create

Update

Delete

Transaction

Type Statement struct {Table string Model interface {} Selects [] string / / selected columns Preloads map [string] [] interface {} Settings sync.Map ConnPool ConnPool Context context.Context SQL strings .Builder Vars [] interface {} / /....}

SQL: the format of spelling SQL

Vars: the value corresponding to the placeholder in sql format.

Select:

SELECT * FROM `sys_ users` WHERE (username =? AND password =?) AND `sys_ users`.`clients _ at`IS NULL ORDER BY `sys_ users`.`id` LIMIT 12.SQL execution

Sql

Params

Result = cursor.execute (sql, params)

Result is converted to a Model object in gorm.

The return value of ConnPool.ExecContext definition is the data of sql.Rows driver layer.

Import ("context"database/sql") type ConnPool interface {PrepareContext (ctx context.Context, query string) (* sql.Stmt, error) ExecContext (ctx context.Context, query string, args... interface {}) (sql.Result, error) QueryContext (ctx context.Context, query string, args... interface {}) (* sql.Rows, error) QueryRowContext (ctx context.Context, query string) Args... interface {}) * sql.Row}

Gorm executes sql:

Result, err: = db.Statement.ConnPool.ExecContext (db.Statement.Context, db.Statement.SQL.String (), db.Statement.Vars...) / / result to

Err: whether there is an error in sql execution

Result: the result returned by db server.

This is the end of "how to use gorm statement". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report