Your Question

For example: models.db.save (& user) I want to get the executed SQL statement. Is there any way to get it?

The document you expected this should be explained

Expected answer

Comment From: shprotru

Hi, @zhangxuanru, is models.db.Debug().Save(&user) that you expect for? You can setup logger and do whatever you want with the executed SQL statements.

Comment From: zhangxuanru

Is there a function or method that can get SQL directly?

Comment From: zhangxuanru

Hi, @zhangxuanru, is models.db.Debug().Save(&user) that you expect for? You can setup logger and do whatever you want with the executed SQL statements.

Is there a function or method that can get SQL directly?

Comment From: shprotru

@zhangxuanru there is no method to get SQL directly, because that's impossible and that's why: GORM SQL queries building is based on fetched data, so it can execute by one request several SQL queries. Use custom logging mechanism and get them all.

Comment From: ghost

you can use the following code to get SQL statement , It is usually used in the gorm's plugin @zhangxuanru
sql := db.Dialector.Explain(db.Statement.SQL.String(), db.Statement.Vars...)

Comment From: sudopower

you can use the following code to get SQL statement , It is usually used in the gorm's plugin @zhangxuanru

sql := db.Dialector.Explain(db.Statement.SQL.String(), db.Statement.Vars...)

thanks !