package main

import(
"fmt"
"database/sql"
_ "github.com/lib/pq"
"github.com/kataras/iris"
"github.com/kataras/iris/middleware/logger"
"github.com/kataras/iris/middleware/recover"
) const(
host = "127.0.0.1"
port =
user = "test"
password = "test"
dbname = "test"
) type User struct{
Uno string `json:"uno"`
Uname string `json:"uname"`
} func main(){
app := iris.New() app.Logger().SetLevel("debug") app.Use(recover.New())
app.Use(logger.New()) app.Get("/users", func(ctx iris.Context){
psqlInfo := fmt.Sprintf("host=%s port=%d user=%s "+
"password=%s dbname=%s sslmode=disable",host, port, user, password, dbname) db, err := sql.Open("postgres", psqlInfo)
if err != nil{
panic(err)
}
defer db.Close() err = db.Ping()
if err != nil{
panic(err)
} fmt.Println("Successfully connected!") var users []User rows,err := db.Query("select uno,uname from t_user")
if err != nil{
panic(err.Error)
}
defer rows.Close() for rows.Next(){
rows.Columns() var user User
err = rows.Scan(&user.Uno,&user.Uname) if err != nil {
panic(err.Error)
} users = append(users, user)
} ctx.JSON(users)
}) app.Get("/user", func(ctx iris.Context){
psqlInfo := fmt.Sprintf("host=%s port=%d user=%s "+
"password=%s dbname=%s sslmode=disable",host, port, user, password, dbname) db, err := sql.Open("postgres", psqlInfo)
if err != nil{
panic(err)
}
defer db.Close() err = db.Ping()
if err != nil{
panic(err)
} fmt.Println("Successfully connected!") sqlQuery := `select uno,uname from t_user where uno=$` var user User row := db.QueryRow(sqlQuery, ctx.URLParam("uno")) err = row.Scan(&user.Uno,&user.Uname) switch err{
case sql.ErrNoRows:
fmt.Println("No rows were returned!")
case nil:
fmt.Println(user)
ctx.JSON(user)
default:
panic(err)
} }) app.Run(iris.Addr(":8080"), iris.WithoutServerError(iris.ErrServerClosed))
}

005-Go 操作PostgreSQL数据库的更多相关文章

  1. hibernate 操作 Postgresql 数据库报 operator does not exist: integer = character varying

    网上的说法如下: Java开发Postgresql 数据库兼容应用的问题,与Oracle有一些不同: Java类型映射数据库类型的不同,Oracle jdbc驱动程序处理Java String类型可正 ...

  2. C#/Python/MATLAB操作PostgreSQL数据库

    PostgreSQL数据库是一个功能非常强大的开源数据库,支持多种SQL特性,非常好用.此外由于结合PostGIS可以实现空间数据库功能,故非常适合GIS领域的使用.本文旨在介绍C#.Python.M ...

  3. Python 操作 PostgreSQL 数据库

    我使用的是 Python 3.7.0 PostgreSQL可以使用psycopg2模块与Python集成. sycopg2是用于Python编程语言的PostgreSQL数据库适配器. psycopg ...

  4. Jmeter_实现操作postgresql数据库

    [环境] ①Jmeter版本:3.2,JDK:1.8: ②postgresql驱动包postgresql-9.3-1103.jdbc4,将该jar包置于..\apache-jmeter-3.2\lib ...

  5. 使用JPA + Eclipselink操作PostgreSQL数据库

    首先确保您已经安装了PostgreSQL.您可以参考我这篇文章PostgreSQL扫盲教程. 使用Eclipse创建一个新的JPA project: Platform选择EclipseLink,作为J ...

  6. python操作postgresql数据库

    import psycopg2 conn = psycopg2.connect(database=") cur = conn.cursor() cur.execute("CREAT ...

  7. 010-Go 操作PostgreSQL数据库2

    1:sql脚本 create table post( id serial primary key, content text, author ) ) 2:post.go package post im ...

  8. ASP.NET MVC 使用 Petapoco 微型ORM框架+NpgSql驱动连接 PostgreSQL数据库

    前段时间在园子里看到了小蝶惊鸿 发布的有关绿色版的Linux.NET——“Jws.Mono”.由于我对.Net程序跑在Linux上非常感兴趣,自己也看了一些有关mono的资料,但是一直没有时间抽出时间 ...

  9. Rust 连接 PostgreSQL 数据库

    这次,我们使用 postgres 这个 crate 来连接和操作 PostgreSQL 数据库. 创建好项目后,在 cargo.toml 里添加 postgres 的依赖: 首先,导入相关的类型,并创 ...

随机推荐

  1. IOS NSLog 打印bool值

    输出BOOL值的方法:NSLog(@"%@",YES?@"YES":@"NO");%@输出字符串. NSLog(@"ifReadO ...

  2. C#编程(四十)----------运算符重载

    运算符重载 所谓的运算符重载是指允许用户使用用户定义的类型编写表达式的能力. 例如,通常需要编写类似与以下内容的代码,入江两个数字相加,很明显,sum是两个数字之和. int i=5,j=4; int ...

  3. FieldExists

    import arcpy import os import sys def FieldExists(TableName,FieldName): desc = arcpy.Describe(TableN ...

  4. 【CentOS】centos7上查看服务开机启动列表

    centos7上查看服务开机启动列表 命令: systemctl list-unit-files; 点击回车,可以向下翻页查询

  5. ibatis.net:第二天,Hello,World ?

    背景 本文的内容全部来自于官方的文档,此处仅仅为了强化记忆. 项目结构 Properties.config <?xml version="1.0" encoding=&quo ...

  6. 详细解读Android中的搜索框(一)—— 简单小例子

    这次开的是一个讲解SearchView的栏目,第一篇主要是给一个小例子,让大家对这个搜索视图有一个了解,之后再分布细化来说. 目标: 我们先来定个目标,我们通过搜索框来输入要搜索的联系人名字,输入的时 ...

  7. Invalid Host header 的解决方案

    composer 显示:Invalid Host header的解决方案 I have tried this workaround: Edit the following line in node_m ...

  8. Can't create pdf file with font calibri bold 错误解决方案

    错误情况: %%[ ProductName: Distiller ]%% Mangal not found, using Courier. %%[ Error: invalidfont; Offend ...

  9. python文档生成工具:pydoc、sphinx;django如何使用sphinx?

    文档生成工具: 自带的pydoc,比较差 建议使用sphinx 安装: pip install sphinx 安装主题: 由各种主题,我选择常用的sphinx_rtd_theme pip instal ...

  10. MySQL 锁模式

    InnoDB implements standard row-level locking where there are two types of locks, shared (S) locks an ...