去吧,没有参数 $1。

huangapple go评论102阅读模式
英文:

Go , there is no parameter $1

问题

我正在尝试通过这种方式获取数据库的值。但是当我转到/myapps路径时,编译器抛出了一个错误。

结构:

type App struct{
    Title string
    Author string
    Description string
}

函数:

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()
}

我在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。

提前谢谢。

英文:

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(&quot;postgres&quot;,&quot; user=postgres dbname=lesson4 host=localhost password=1234 sslmode=disable&quot;)
    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(&amp;b.Title, &amp;b.Author, &amp;b.Description)

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

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

        db.Close()
}

I read like this in the HTML:

{{ range .apps}}
  &lt;tr&gt;
    &lt;td&gt;{{ .Title }}&lt;/td&gt;
    &lt;td&gt;{{ .Author }}&lt;/td&gt;
    &lt;td&gt;{{ .Description }}&lt;/td&gt;
    &lt;td&gt; &lt;form action=&quot;/delete&quot;&gt;
          &lt;p class=&quot;navbar-form navbar-right&quot;&gt;&lt;button type=&quot;submit&quot; class=&quot;btn btn-danger&quot;&gt;Borrar&lt;/button&gt; &lt;/p&gt;
       &lt;/form&gt;&lt;/td&gt;
  &lt;/tr&gt;
{{ end  }}

The error is : there is no parameter $1.

Thank you in advance.

答案1

得分: 7

你需要传递以下参数:

title := '标题'
author := '约翰'
description := '一本书'

rows, err := db.Query(`
    SELECT title, author, description
    FROM apps
    WHERE title ILIKE $1
          OR author ILIKE $2
          OR description ILIKE $3`
    , title, author, description
)

使用 % 字符表示“匹配任何内容”

http://golang.org/pkg/database/sql/#DB.Query

请参考上述链接了解更多关于 % 字符的用法。

http://www.postgresql.org/docs/current/static/functions-matching.html#FUNCTIONS-LIKE

英文:

You need to pass the parameters

title := &#39;%the title%&#39;
author := &#39;%John%&#39;
description := &#39;%a book%&#39;

rows, err := db.Query(`
    SELECT title, author, description
    FROM apps
    WHERE title ILIKE $1
          OR author ILIKE $2
          OR description ILIKE $3`
    , title, author, description
)

http://golang.org/pkg/database/sql/#DB.Query

Use the % character to mean match anything

http://www.postgresql.org/docs/current/static/functions-matching.html#FUNCTIONS-LIKE

答案2

得分: 0

我能够解决我的问题,不管怎样,还是谢谢你的答案和帮助 去吧,没有参数 $1。

我将发布工作代码:

结构:

type App struct{
    Title string
    Author string
    Description string
}

应用程序处理程序:

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()

以及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 }}
英文:

I was able to resolve my issue, thank you anyways for your answers and help 去吧,没有参数 $1。

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(&amp;b.Title, &amp;b.Author, &amp;b.Description)
            PanicIf(err)

                apps = append(apps, b)
            }
            
            render(w, &quot;myapps.html&quot;, map[string]interface{}{&quot;apps&quot; : apps})

        db.Close()

And the html:

{{ range .apps}}
  &lt;tr&gt;
    &lt;td&gt;{{ .Title }}&lt;/td&gt;
    &lt;td&gt;{{ .Author }}&lt;/td&gt;
    &lt;td&gt;{{ .Description }}&lt;/td&gt;
    &lt;td&gt; &lt;form action=&quot;/delete&quot;&gt;
          &lt;p class=&quot;navbar-form navbar-right&quot;&gt;&lt;button type=&quot;submit&quot; class=&quot;btn btn-danger&quot;&gt;Borrar&lt;/button&gt; &lt;/p&gt;
       &lt;/form&gt;&lt;/td&gt;
  &lt;/tr&gt;
{{ end  }}

huangapple
  • 本文由 发表于 2014年7月12日 19:29:10
  • 转载请务必保留本文链接:https://go.coder-hub.com/24712463.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定