How would I query an entire table from Postgres?

agolangf · · 441 次点击    
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
<p>Currently looking at the pq standard library. Trying to find a way around needing to declare a variable for every column that you desire like in the example below.</p> <p><code> func (p *product) getProduct(db *sql.DB) error { return db.QueryRow(&#34;SELECT name, price FROM products WHERE id=$1&#34;, p.ID).Scan(&amp;p.Name, &amp;p.Price) </code> I have a thought of querying for all the table names and creating a single string like </p> <p><code> SELECT (column1, column2, ....columnN) FROM table </code></p> <p>But that just seems silly.</p> <hr/>**评论:**<br/><br/>kerakk19: <pre><p>If you know how many colums are there you can use somtehing like this:</p> <pre><code>const numOfCols = your_nums_of_cols type Data struct{ Items[]interface{} `json:&#34;items&#34;` } func (p *product) getProduct(db *sql.DB) (Data, error){ row := db.QueryRow(&#34;SELECT name, price FROM products WHERE id=$1&#34;, p.ID) var data Data data.Items = make([]interface{}, numOfCols) for i := range data.Items { data.Items[i] = &amp;data.Items[i] } err = row.Scan(data.Items...) if err != nil { return data, err } return data, nil } </code></pre> <p>If you don&#39;t know number of columns, you can use some kind of query to count it and base your numOfCols on it :)</p></pre>icholy: <pre><p>You can get the number of columns using the <code>(*Row).Columns()</code> method.</p></pre>forgiveangel: <pre><p>Wow thanks! Way more helpful then stackoverflow</p></pre>forgiveangel: <pre><blockquote> <p>Items[]interface{} <code>json:&#34;items&#34;</code></p> </blockquote> <p>Do you mind explaining what this is? or the terminology so I could google it?</p> <p>EDIT: I think I figured it, out. it&#39;s for JSON or to create a map?</p></pre>icholy: <pre><p>It&#39;s called a struct tag</p></pre>forgiveangel: <pre><p>How do I view the contents of the data? I was to print out the values from the table.</p></pre>kerakk19: <pre><p>You can range over the slice and do switch on interface types. </p> <p>Still, i don&#39;t think this it is good practice to scan data from your queries like that. </p> <p>The only way where i found it handy was when i had like 12 columns of the SAME type, so i could just run over slice without fear about data types.</p></pre>forgiveangel: <pre><p>Yeah... I realized that my initial project parameters were off</p></pre>kpurdon: <pre><p>With the database/sql binding libraries you must use <code>.Scan()</code> and pull into each variable separately. You may want to look into sqlx, specifically something like <a href="https://godoc.org/github.com/jmoiron/sqlx#StructScan" rel="nofollow">https://godoc.org/github.com/jmoiron/sqlx#StructScan</a>.</p></pre>natefinch: <pre><p>Or generate that sql code with Gnorm: Gnorm.org</p></pre>

入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889

441 次点击  
加入收藏 微博
暂无回复
添加一条新回复 (您需要 登录 后才能回复 没有账号 ?)
  • 请尽量让自己的回复能够对别人有帮助
  • 支持 Markdown 格式, **粗体**、~~删除线~~、`单行代码`
  • 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
  • 图片支持拖拽、截图粘贴等方式上传