Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to run pure sql in sqlx

The sqlx extends the database/sql library and have Exec|Query method to run the pure sql without argument, but when i tried to run sqlx.Exec it said the sqlx have no Exec method. How to let sqlx run the pure sql method without any argument?

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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")
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading