dongzhi6905 2014-07-12 11:29
浏览 71
已采纳

Go,没有参数$ 1

Im trying to get the values of the database by this way. But the compiler throw me an error when I go to /myapps direction.

Structure:

type App struct{
    Title string
    Author string
    Description string
}

Function:

func myappsHandler(w http.ResponseWriter, r *http.Request){
    db, err := sql.Open("postgres"," user=postgres dbname=lesson4 host=localhost password=1234 sslmode=disable")
    if err != nil{
        log.Fatal(err)
    }
        rows, err := db.Query(`SELECT title, author, description FROM apps
                                 WHERE title ILIKE $1
                                 OR author ILIKE $1
                                 OR description ILIKE $1`)
            defer rows.Close()

            if err != nil{
                log.Fatal(err)
            }

            apps := []App{}
            for rows.Next(){
                b := App{}
                err := rows.Scan(&b.Title, &b.Author, &b.Description)

                if err != nil{
                log.Fatal(err)
            }
                apps = append(apps, b)
            }

            render(w, "myapps.html", map[string]interface{}{"apps" : apps})

        db.Close()
}

I read like this in the HTML:

{{ range .apps}}
  <tr>
    <td>{{ .Title }}</td>
    <td>{{ .Author }}</td>
    <td>{{ .Description }}</td>
    <td> <form action="/delete">
          <p class="navbar-form navbar-right"><button type="submit" class="btn btn-danger">Borrar</button> </p>
       </form></td>
  </tr>
{{ end  }}

The error is : there is no parameter $1.

Thank you in advance.

  • 写回答

2条回答 默认 最新

  • douxia6554 2014-07-13 12:10
    关注

    I was able to resolve my issue, thank you anyways for your answers and help :)

    I will post the working code:

    Structure:

    type App struct{
        Title string
        Author string
        Description string
    }
    

    Handler of Apps:

     db := SetupDB()
    
            rows, err := db.Query(`SELECT title, author, description FROM apps`)
                PanicIf(err)
    
                defer rows.Close()
    
                apps := []App{}
                for rows.Next(){
                    b := App{}
                    err := rows.Scan(&b.Title, &b.Author, &b.Description)
                PanicIf(err)
    
                    apps = append(apps, b)
                }
    
                render(w, "myapps.html", map[string]interface{}{"apps" : apps})
    
            db.Close()
    

    And the html:

    {{ range .apps}}
      <tr>
        <td>{{ .Title }}</td>
        <td>{{ .Author }}</td>
        <td>{{ .Description }}</td>
        <td> <form action="/delete">
              <p class="navbar-form navbar-right"><button type="submit" class="btn btn-danger">Borrar</button> </p>
           </form></td>
      </tr>
    {{ end  }}
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题