Query: A Simple db helper with golang

query

Simple db helper

Installation

go get github.com/eatmoreapple/query

Example

fmt.Println(db.Query("SELECT 1").Bool()) // true fmt.Println(db.Query("SELECT Host from user").StringSlice()) // [localhost localhost localhost] var count int64 fmt.Println(db.Query("SELECT COUNT(*) FROM user").Value(&count), count) // 3 type User struct { Host string User string } var user User fmt.Println(db.Query("SELECT * FROM user LIMIT 1").Struct(&user), user) // {localhost root} var user2 = new(User) fmt.Println(db.Query("SELECT * FROM user LIMIT 1").Struct(&user2), user2) // &{localhost root} var users []User fmt.Println(db.Query("SELECT * FROM user").StructList(&users), users) // [{localhost root} {localhost mysql.session} {localhost mysql.sys}] var users2 []*User fmt.Println(db.Query("SELECT * FROM user").StructList(&users2), users2) // [0xc00013e040 0xc00013eb60 0xc00013f660] } ">
package main

import (
	"fmt"
	"github.com/eatmoreapple/query"
	_ "github.com/go-sql-driver/mysql"
	"log"
)

func main() {
	db, err := query.Open("mysql", "root:qwe123@tcp(localhost:3306)/mysql")
	if err != nil {
		log.Fatal(err)
	}
	defer db.Close()

	fmt.Println(db.Query("SELECT 1").Int64()) // 1 
          

	fmt.Println(db.Query("SELECT 1").Bool()) // true 
          

	fmt.Println(db.Query("SELECT Host from user").StringSlice()) // [localhost localhost localhost] 
          

	var count int64
	fmt.Println(db.Query("SELECT COUNT(*) FROM user").Value(&count), count) // 
          
            3
          

	type User struct {
		Host string
		User string
	}

	var user User
	fmt.Println(db.Query("SELECT * FROM user LIMIT 1").Struct(&user), user) // 
          
            {localhost root}
          

	var user2 = new(User)
	fmt.Println(db.Query("SELECT * FROM user LIMIT 1").Struct(&user2), user2) // 
          
            &{localhost root}
          

	var users []User
	fmt.Println(db.Query("SELECT * FROM user").StructList(&users), users) // 
          
            [{localhost root} {localhost mysql.session} {localhost mysql.sys}]
          

	var users2 []*User
	fmt.Println(db.Query("SELECT * FROM user").StructList(&users2), users2) // 
          
            [0xc00013e040 0xc00013eb60 0xc00013f660]
          
}
Owner
聂青
I have so many ideas
聂青
Similar Resources

Simple project in Go to play around with some CRUD operations using a PostgreSQL database and pgx

Record Store November 2021 I started learning Go a few weeks ago and this is my first proper project using Go. I wanted to use it to get to grips with

Nov 26, 2021

Simple REST API application using GO [/net/http & mux]

REST-API-CRUD Simple REST API application using GOLANG [/net/http & mux] Available URLs: URL METHOD http://127.0.0.1:8888/ GET http://127.0.0.1:8888/l

Dec 3, 2021

A simple CRUD app built with Go for Shopify Backend Developer Intern Challenge - Summer 2022

Shopify Backend Developer Intern Challenge - Summer 2022 A simple CRUD app built with Go and Gin. This let you update track your inventory, add new it

Jan 18, 2022

A simple CRUD API made with Go, Postgres, FIber, Gorm and Docker.

golang-test-api A simple CRUD API made with Go, Postgres, FIber, Gorm and Docker. Cloning the repository To clone the repository run the following com

Dec 14, 2022

Golang struct-to-table database mapper

Structable: Struct-Table Mapping for Go Warning: This is the Structable 4 development branch. For a stable release, use version 3.1.0. Structable deve

Dec 27, 2022

100% type-safe ORM for Go (Golang) with code generation and MySQL, PostgreSQL, Sqlite3, SQL Server support. GORM under the hood.

go-queryset 100% type-safe ORM for Go (Golang) with code generation and MySQL, PostgreSQL, Sqlite3, SQL Server support. GORM under the hood. Contents

Dec 30, 2022

Golang Event Scheduling Sample Using Postgresql Database as persisting layer.

Database Based Event Scheduling Example that demonstrates super basic database based event scheduling. To run this example; Copy .env.example to .env

Nov 28, 2022

A pure golang SQL database for learning database theory

Go SQL DB 中文 "Go SQL DB" is a relational database that supports SQL queries for research purposes. The main goal is to show the basic principles and k

Dec 29, 2022

Golang Object Graph Mapper for Neo4j

GoGM Golang Object Graph Mapper v2 go get -u github.com/mindstand/gogm/v2 Features Struct Mapping through the gogm struct decorator Full support for

Dec 28, 2022
Related tags
QBS stands for Query By Struct. A Go ORM.

Qbs Qbs stands for Query By Struct. A Go ORM. 中文版 README ChangeLog 2013.03.14: index name has changed to {table name}_{column name}. For existing appl

Sep 9, 2022
Scope function for GORM queries provides easy filtering with query parameters

Gin GORM filter Scope function for GORM queries provides easy filtering with query parameters Usage go get github.com/ActiveChooN/gin-gorm-filter Mod

Dec 28, 2022
Code that will query the meta data of an instance within AWS and provide a json formatted output on Go

EC2 meta-data, output JSON Code that will query the meta data of an instance wit

Dec 16, 2021
Simple-crm-system - Simple CRM system CRUD backend using Go, Fiber, SQLite, Gorm

Simple CRM system CRUD backend using GO, Fiber, Gorm, SQLite Developent go mod t

Nov 13, 2022
Very simple example of golang buffalo CRUD

Buffalo CRUD exemple Introduction Site du projet : https://gobuffalo.io/fr Documentation générale : https://gobuffalo.io/fr/docs/overview/ Documentati

Nov 7, 2021
A simple wrapper around sql.DB to help with structs. Not quite an ORM.

go-modeldb A simple wrapper around sql.DB to help with structs. Not quite an ORM. Philosophy: Don't make an ORM Example: // Setup require "modeldb" db

Nov 16, 2019
Simple Go ORM for Google/Firebase Cloud Firestore

go-firestorm Go ORM (Object-relational mapping) for Google Cloud Firestore. Goals Easy to use Non intrusive Non exclusive Fast Features Basic CRUD ope

Dec 1, 2022
A simple and fast Redis backed key-value store library for Go

store store is a data-store library for Go that provides a set of platform-independent interfaces to persist and retrieve data. Its primary goal is to

Sep 27, 2022
Simple and Powerful ORM for Go, support mysql,postgres,tidb,sqlite3,mssql,oracle, Moved to https://gitea.com/xorm/xorm

xorm HAS BEEN MOVED TO https://gitea.com/xorm/xorm . THIS REPOSITORY WILL NOT BE UPDATED ANY MORE. 中文 Xorm is a simple and powerful ORM for Go. Featur

Jan 3, 2023
Simple and performant ORM for sql.DB

Simple and performant ORM for sql.DB Main features are: Works with PostgreSQL, MySQL, SQLite. Selecting into a map, struct, slice of maps/structs/vars

Jan 4, 2023