{"id":13416663,"url":"https://github.com/jmoiron/sqlx","last_synced_at":"2025-05-12T16:24:37.438Z","repository":{"id":6633457,"uuid":"7877397","full_name":"jmoiron/sqlx","owner":"jmoiron","description":"general purpose extensions to golang's database/sql","archived":false,"fork":false,"pushed_at":"2024-08-15T16:19:19.000Z","size":877,"stargazers_count":16887,"open_issues_count":379,"forks_count":1103,"subscribers_count":195,"default_branch":"master","last_synced_at":"2025-05-05T14:18:07.423Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"http://jmoiron.github.io/sqlx/","language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/jmoiron.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2013-01-28T19:40:00.000Z","updated_at":"2025-05-05T12:04:00.000Z","dependencies_parsed_at":"2024-11-05T22:47:13.625Z","dependency_job_id":null,"html_url":"https://github.com/jmoiron/sqlx","commit_stats":{"total_commits":352,"total_committers":91,"mean_commits":3.868131868131868,"dds":0.4289772727272727,"last_synced_commit":"41dac167fdad5e3fd81d66cafba0951dc6823a30"},"previous_names":[],"tags_count":10,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jmoiron%2Fsqlx","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jmoiron%2Fsqlx/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jmoiron%2Fsqlx/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jmoiron%2Fsqlx/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/jmoiron","download_url":"https://codeload.github.com/jmoiron/sqlx/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":253774822,"owners_count":21962233,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-07-30T22:00:18.849Z","updated_at":"2025-05-12T16:24:37.419Z","avatar_url":"https://github.com/jmoiron.png","language":"Go","readme":"# sqlx\n\n[![CircleCI](https://dl.circleci.com/status-badge/img/gh/jmoiron/sqlx/tree/master.svg?style=shield)](https://dl.circleci.com/status-badge/redirect/gh/jmoiron/sqlx/tree/master) [![Coverage Status](https://coveralls.io/repos/github/jmoiron/sqlx/badge.svg?branch=master)](https://coveralls.io/github/jmoiron/sqlx?branch=master) [![Godoc](http://img.shields.io/badge/godoc-reference-blue.svg?style=flat)](https://godoc.org/github.com/jmoiron/sqlx) [![license](http://img.shields.io/badge/license-MIT-red.svg?style=flat)](https://raw.githubusercontent.com/jmoiron/sqlx/master/LICENSE)\n\nsqlx is a library which provides a set of extensions on go's standard\n`database/sql` library.  The sqlx versions of `sql.DB`, `sql.TX`, `sql.Stmt`,\net al. all leave the underlying interfaces untouched, so that their interfaces\nare a superset on the standard ones.  This makes it relatively painless to\nintegrate existing codebases using database/sql with sqlx.\n\nMajor additional concepts are:\n\n* Marshal rows into structs (with embedded struct support), maps, and slices\n* Named parameter support including prepared statements\n* `Get` and `Select` to go quickly from query to struct/slice\n\nIn addition to the [godoc API documentation](http://godoc.org/github.com/jmoiron/sqlx),\nthere is also some [user documentation](http://jmoiron.github.io/sqlx/) that\nexplains how to use `database/sql` along with sqlx.\n\n## Recent Changes\n\n1.3.0:\n\n* `sqlx.DB.Connx(context.Context) *sqlx.Conn`\n* `sqlx.BindDriver(driverName, bindType)`\n* support for `[]map[string]interface{}` to do \"batch\" insertions\n* allocation \u0026 perf improvements for `sqlx.In`\n\nDB.Connx returns an `sqlx.Conn`, which is an `sql.Conn`-alike consistent with\nsqlx's wrapping of other types.\n\n`BindDriver` allows users to control the bindvars that sqlx will use for drivers,\nand add new drivers at runtime.  This results in a very slight performance hit\nwhen resolving the driver into a bind type (~40ns per call), but it allows users\nto specify what bindtype their driver uses even when sqlx has not been updated\nto know about it by default.\n\n### Backwards Compatibility\n\nCompatibility with the most recent two versions of Go is a requirement for any\nnew changes.  Compatibility beyond that is not guaranteed.\n\nVersioning is done with Go modules.  Breaking changes (eg. removing deprecated API)\nwill get major version number bumps.\n\n## install\n\n    go get github.com/jmoiron/sqlx\n\n## issues\n\nRow headers can be ambiguous (`SELECT 1 AS a, 2 AS a`), and the result of\n`Columns()` does not fully qualify column names in queries like:\n\n```sql\nSELECT a.id, a.name, b.id, b.name FROM foos AS a JOIN foos AS b ON a.parent = b.id;\n```\n\nmaking a struct or map destination ambiguous.  Use `AS` in your queries\nto give columns distinct names, `rows.Scan` to scan them manually, or \n`SliceScan` to get a slice of results.\n\n## usage\n\nBelow is an example which shows some common use cases for sqlx.  Check \n[sqlx_test.go](https://github.com/jmoiron/sqlx/blob/master/sqlx_test.go) for more\nusage.\n\n\n```go\npackage main\n\nimport (\n    \"database/sql\"\n    \"fmt\"\n    \"log\"\n    \n    _ \"github.com/lib/pq\"\n    \"github.com/jmoiron/sqlx\"\n)\n\nvar schema = `\nCREATE TABLE person (\n    first_name text,\n    last_name text,\n    email text\n);\n\nCREATE TABLE place (\n    country text,\n    city text NULL,\n    telcode integer\n)`\n\ntype Person struct {\n    FirstName string `db:\"first_name\"`\n    LastName  string `db:\"last_name\"`\n    Email     string\n}\n\ntype Place struct {\n    Country string\n    City    sql.NullString\n    TelCode int\n}\n\nfunc main() {\n    // this Pings the database trying to connect\n    // use sqlx.Open() for sql.Open() semantics\n    db, err := sqlx.Connect(\"postgres\", \"user=foo dbname=bar sslmode=disable\")\n    if err != nil {\n        log.Fatalln(err)\n    }\n\n    // exec the schema or fail; multi-statement Exec behavior varies between\n    // database drivers;  pq will exec them all, sqlite3 won't, ymmv\n    db.MustExec(schema)\n    \n    tx := db.MustBegin()\n    tx.MustExec(\"INSERT INTO person (first_name, last_name, email) VALUES ($1, $2, $3)\", \"Jason\", \"Moiron\", \"jmoiron@jmoiron.net\")\n    tx.MustExec(\"INSERT INTO person (first_name, last_name, email) VALUES ($1, $2, $3)\", \"John\", \"Doe\", \"johndoeDNE@gmail.net\")\n    tx.MustExec(\"INSERT INTO place (country, city, telcode) VALUES ($1, $2, $3)\", \"United States\", \"New York\", \"1\")\n    tx.MustExec(\"INSERT INTO place (country, telcode) VALUES ($1, $2)\", \"Hong Kong\", \"852\")\n    tx.MustExec(\"INSERT INTO place (country, telcode) VALUES ($1, $2)\", \"Singapore\", \"65\")\n    // Named queries can use structs, so if you have an existing struct (i.e. person := \u0026Person{}) that you have populated, you can pass it in as \u0026person\n    tx.NamedExec(\"INSERT INTO person (first_name, last_name, email) VALUES (:first_name, :last_name, :email)\", \u0026Person{\"Jane\", \"Citizen\", \"jane.citzen@example.com\"})\n    tx.Commit()\n\n    // Query the database, storing results in a []Person (wrapped in []interface{})\n    people := []Person{}\n    db.Select(\u0026people, \"SELECT * FROM person ORDER BY first_name ASC\")\n    jason, john := people[0], people[1]\n\n    fmt.Printf(\"%#v\\n%#v\", jason, john)\n    // Person{FirstName:\"Jason\", LastName:\"Moiron\", Email:\"jmoiron@jmoiron.net\"}\n    // Person{FirstName:\"John\", LastName:\"Doe\", Email:\"johndoeDNE@gmail.net\"}\n\n    // You can also get a single result, a la QueryRow\n    jason = Person{}\n    err = db.Get(\u0026jason, \"SELECT * FROM person WHERE first_name=$1\", \"Jason\")\n    fmt.Printf(\"%#v\\n\", jason)\n    // Person{FirstName:\"Jason\", LastName:\"Moiron\", Email:\"jmoiron@jmoiron.net\"}\n\n    // if you have null fields and use SELECT *, you must use sql.Null* in your struct\n    places := []Place{}\n    err = db.Select(\u0026places, \"SELECT * FROM place ORDER BY telcode ASC\")\n    if err != nil {\n        fmt.Println(err)\n        return\n    }\n    usa, singsing, honkers := places[0], places[1], places[2]\n    \n    fmt.Printf(\"%#v\\n%#v\\n%#v\\n\", usa, singsing, honkers)\n    // Place{Country:\"United States\", City:sql.NullString{String:\"New York\", Valid:true}, TelCode:1}\n    // Place{Country:\"Singapore\", City:sql.NullString{String:\"\", Valid:false}, TelCode:65}\n    // Place{Country:\"Hong Kong\", City:sql.NullString{String:\"\", Valid:false}, TelCode:852}\n\n    // Loop through rows using only one struct\n    place := Place{}\n    rows, err := db.Queryx(\"SELECT * FROM place\")\n    for rows.Next() {\n        err := rows.StructScan(\u0026place)\n        if err != nil {\n            log.Fatalln(err)\n        } \n        fmt.Printf(\"%#v\\n\", place)\n    }\n    // Place{Country:\"United States\", City:sql.NullString{String:\"New York\", Valid:true}, TelCode:1}\n    // Place{Country:\"Hong Kong\", City:sql.NullString{String:\"\", Valid:false}, TelCode:852}\n    // Place{Country:\"Singapore\", City:sql.NullString{String:\"\", Valid:false}, TelCode:65}\n\n    // Named queries, using `:name` as the bindvar.  Automatic bindvar support\n    // which takes into account the dbtype based on the driverName on sqlx.Open/Connect\n    _, err = db.NamedExec(`INSERT INTO person (first_name,last_name,email) VALUES (:first,:last,:email)`, \n        map[string]interface{}{\n            \"first\": \"Bin\",\n            \"last\": \"Smuth\",\n            \"email\": \"bensmith@allblacks.nz\",\n    })\n\n    // Selects Mr. Smith from the database\n    rows, err = db.NamedQuery(`SELECT * FROM person WHERE first_name=:fn`, map[string]interface{}{\"fn\": \"Bin\"})\n\n    // Named queries can also use structs.  Their bind names follow the same rules\n    // as the name -\u003e db mapping, so struct fields are lowercased and the `db` tag\n    // is taken into consideration.\n    rows, err = db.NamedQuery(`SELECT * FROM person WHERE first_name=:first_name`, jason)\n    \n    \n    // batch insert\n    \n    // batch insert with structs\n    personStructs := []Person{\n        {FirstName: \"Ardie\", LastName: \"Savea\", Email: \"asavea@ab.co.nz\"},\n        {FirstName: \"Sonny Bill\", LastName: \"Williams\", Email: \"sbw@ab.co.nz\"},\n        {FirstName: \"Ngani\", LastName: \"Laumape\", Email: \"nlaumape@ab.co.nz\"},\n    }\n\n    _, err = db.NamedExec(`INSERT INTO person (first_name, last_name, email)\n        VALUES (:first_name, :last_name, :email)`, personStructs)\n\n    // batch insert with maps\n    personMaps := []map[string]interface{}{\n        {\"first_name\": \"Ardie\", \"last_name\": \"Savea\", \"email\": \"asavea@ab.co.nz\"},\n        {\"first_name\": \"Sonny Bill\", \"last_name\": \"Williams\", \"email\": \"sbw@ab.co.nz\"},\n        {\"first_name\": \"Ngani\", \"last_name\": \"Laumape\", \"email\": \"nlaumape@ab.co.nz\"},\n    }\n\n    _, err = db.NamedExec(`INSERT INTO person (first_name, last_name, email)\n        VALUES (:first_name, :last_name, :email)`, personMaps)\n}\n```\n","funding_links":[],"categories":["Popular","工具库","Utilities","HarmonyOS","开源类库","Database","Go","Misc","Golang Packages","Open source library","公用事业公司","常用工具","实用工具","實用工具","工具库`可以提升效率的通用代码库和工具`","Utility","\u003ca name=\"Go\"\u003e\u003c/a\u003eGo"],"sub_categories":["查询语","Utility/Miscellaneous","Windows Manager","数据库","HTTP Clients","Advanced Console UIs","Database","实用程序/Miscellaneous","爬虫工具","\u003cspan id=\"高级控制台用户界面-advanced-console-uis\"\u003e高级控制台用户界面 Advanced Console UIs\u003c/span\u003e","高级控制台界面","高級控制台界面","Fail injection","交流"],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjmoiron%2Fsqlx","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fjmoiron%2Fsqlx","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjmoiron%2Fsqlx/lists"}