The sqlx extends the database/sql
library and have Exec|Query
function to run the pure sql without argument, but when i tried to run sqlx.Exec
it said the sqlx have no Exec function
. How to let sqlx run the pure sql method without any argument?
CodePudding user response:
The github.com/jmoiron/sqlx
is a package. Packages do not have methods, they may have functions.
Types may have methods. The sqlx.DB
type is what you're looking for. It embeds the *sql.DB
type which have a DB.Exec()
method, which is promoted and is available on a value of type *sqlx.DB
too.
So first you need an *sqlx.DB
value, by connecting to the database, something like this:
db, err := sqlx.Connect("postgres", "user=foo dbname=bar sslmode=disable")
if err != nil {
log.Fatalln(err)
}
Here db
is of type *sqlx.DB
. Then you may use the Exec()
method:
result, err := db.Exec("SELECT * FROM mytable")