去吧,没有参数 $1。

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

Go , there is no parameter $1

问题

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

结构:

  1. type App struct{
  2. Title string
  3. Author string
  4. Description string
  5. }

函数:

  1. func myappsHandler(w http.ResponseWriter, r *http.Request){
  2. db, err := sql.Open("postgres"," user=postgres dbname=lesson4 host=localhost password=1234 sslmode=disable")
  3. if err != nil{
  4. log.Fatal(err)
  5. }
  6. rows, err := db.Query(`SELECT title, author, description FROM apps
  7. WHERE title ILIKE $1
  8. OR author ILIKE $1
  9. OR description ILIKE $1`)
  10. defer rows.Close()
  11. if err != nil{
  12. log.Fatal(err)
  13. }
  14. apps := []App{}
  15. for rows.Next(){
  16. b := App{}
  17. err := rows.Scan(&b.Title, &b.Author, &b.Description)
  18. if err != nil{
  19. log.Fatal(err)
  20. }
  21. apps = append(apps, b)
  22. }
  23. render(w, "myapps.html", map[string]interface{}{"apps" : apps})
  24. db.Close()
  25. }

我在HTML中这样读取:

  1. {{ range .apps}}
  2. <tr>
  3. <td>{{ .Title }}</td>
  4. <td>{{ .Author }}</td>
  5. <td>{{ .Description }}</td>
  6. <td> <form action="/delete">
  7. <p class="navbar-form navbar-right"><button type="submit" class="btn btn-danger">Borrar</button> </p>
  8. </form></td>
  9. </tr>
  10. {{ 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:

  1. type App struct{
  2. Title string
  3. Author string
  4. Description string
  5. }

Function:

  1. func myappsHandler(w http.ResponseWriter, r *http.Request){
  2. db, err := sql.Open(&quot;postgres&quot;,&quot; user=postgres dbname=lesson4 host=localhost password=1234 sslmode=disable&quot;)
  3. if err != nil{
  4. log.Fatal(err)
  5. }
  6. rows, err := db.Query(`SELECT title, author, description FROM apps
  7. WHERE title ILIKE $1
  8. OR author ILIKE $1
  9. OR description ILIKE $1`)
  10. defer rows.Close()
  11. if err != nil{
  12. log.Fatal(err)
  13. }
  14. apps := []App{}
  15. for rows.Next(){
  16. b := App{}
  17. err := rows.Scan(&amp;b.Title, &amp;b.Author, &amp;b.Description)
  18. if err != nil{
  19. log.Fatal(err)
  20. }
  21. apps = append(apps, b)
  22. }
  23. render(w, &quot;myapps.html&quot;, map[string]interface{}{&quot;apps&quot; : apps})
  24. db.Close()
  25. }

I read like this in the HTML:

  1. {{ range .apps}}
  2. &lt;tr&gt;
  3. &lt;td&gt;{{ .Title }}&lt;/td&gt;
  4. &lt;td&gt;{{ .Author }}&lt;/td&gt;
  5. &lt;td&gt;{{ .Description }}&lt;/td&gt;
  6. &lt;td&gt; &lt;form action=&quot;/delete&quot;&gt;
  7. &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;
  8. &lt;/form&gt;&lt;/td&gt;
  9. &lt;/tr&gt;
  10. {{ end }}

The error is : there is no parameter $1.

Thank you in advance.

答案1

得分: 7

你需要传递以下参数:

  1. title := '标题'
  2. author := '约翰'
  3. description := '一本书'
  4. rows, err := db.Query(`
  5. SELECT title, author, description
  6. FROM apps
  7. WHERE title ILIKE $1
  8. OR author ILIKE $2
  9. OR description ILIKE $3`
  10. , title, author, description
  11. )

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

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

  1. title := &#39;%the title%&#39;
  2. author := &#39;%John%&#39;
  3. description := &#39;%a book%&#39;
  4. rows, err := db.Query(`
  5. SELECT title, author, description
  6. FROM apps
  7. WHERE title ILIKE $1
  8. OR author ILIKE $2
  9. OR description ILIKE $3`
  10. , title, author, description
  11. )

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。

我将发布工作代码:

结构:

  1. type App struct{
  2. Title string
  3. Author string
  4. Description string
  5. }

应用程序处理程序:

  1. db := SetupDB()
  2. rows, err := db.Query(`SELECT title, author, description FROM apps`)
  3. PanicIf(err)
  4. defer rows.Close()
  5. apps := []App{}
  6. for rows.Next(){
  7. b := App{}
  8. err := rows.Scan(&b.Title, &b.Author, &b.Description)
  9. PanicIf(err)
  10. apps = append(apps, b)
  11. }
  12. render(w, "myapps.html", map[string]interface{}{"apps" : apps})
  13. db.Close()

以及HTML代码:

  1. {{ range .apps}}
  2. <tr>
  3. <td>{{ .Title }}</td>
  4. <td>{{ .Author }}</td>
  5. <td>{{ .Description }}</td>
  6. <td> <form action="/delete">
  7. <p class="navbar-form navbar-right"><button type="submit" class="btn btn-danger">Borrar</button> </p>
  8. </form></td>
  9. </tr>
  10. {{ end }}
英文:

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

I will post the working code:

Structure:

  1. type App struct{
  2. Title string
  3. Author string
  4. Description string
  5. }

Handler of Apps:

  1. db := SetupDB()
  2. rows, err := db.Query(`SELECT title, author, description FROM apps`)
  3. PanicIf(err)
  4. defer rows.Close()
  5. apps := []App{}
  6. for rows.Next(){
  7. b := App{}
  8. err := rows.Scan(&amp;b.Title, &amp;b.Author, &amp;b.Description)
  9. PanicIf(err)
  10. apps = append(apps, b)
  11. }
  12. render(w, &quot;myapps.html&quot;, map[string]interface{}{&quot;apps&quot; : apps})
  13. db.Close()

And the html:

  1. {{ range .apps}}
  2. &lt;tr&gt;
  3. &lt;td&gt;{{ .Title }}&lt;/td&gt;
  4. &lt;td&gt;{{ .Author }}&lt;/td&gt;
  5. &lt;td&gt;{{ .Description }}&lt;/td&gt;
  6. &lt;td&gt; &lt;form action=&quot;/delete&quot;&gt;
  7. &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;
  8. &lt;/form&gt;&lt;/td&gt;
  9. &lt;/tr&gt;
  10. {{ 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:

确定