Help with parsing a struct

agolangf · · 194 次点击    
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
<p>I have a struct, User, shown below:</p> <pre><code> type User struct { DbID string DbUsername string DbPasswordHash string DbPasswordSalt string } </code></pre> <p>I query my database, like so:</p> <pre><code> rows, err := db.Query(&#34;SELECT id FROM users WHERE username = $1&#34;, sender) defer rows.Close() if err != nil { return err } users := make([]*User, 0) for rows.Next() { user := new(User) err := rows.Scan(&amp;user.DbID) if err != nil { log.Fatal(&#34;boop: &#34;, err) return err } users = append(users, user) } </code></pre> <p>Now I have to parse the value stored in the User struct to insert somewhere else in the database. This is my issue:</p> <pre><code> // There has to be a better way to do this... for _, user := range users { sqlStatement := `INSERT INTO message (creator_id, message_body, create_date) VALUES ($1, $2, $3)` _, err = db.Exec(sqlStatement, user.DbID, message, timestamp) } </code></pre> <p>How can I do this in a flat manner? Or just some better way. My issue with this is later down I have to iterate over a User struct and a Message struct, so obviously this does not work </p> <hr/>**评论:**<br/><br/>Killing_Spark: <pre><p>I have some fundamental questions: you look in your database for id(s) for some username. I mean sure there can be more then one user with the same name, but then you save the message that was sent for every one of these users? Maybe Im not seeing something, but I think that isnt right? </p> <p>So i&#39;d suggest instead of passing the username, pass the id to your method, then you dont need to query for the id and you dont need the for loop. (you will need to give the users unique ids anyways so you can simply use the dbID) </p> <p>Correct me please if that is wrong in some way :) </p></pre>ashishduhh1: <pre><p>I assume you&#39;re using MySQL. I suggest you bypass memory altogether and just do a <a href="" rel="nofollow">INSERT...SELECT statement</a>.</p></pre>Idontlikecold: <pre><p>Postgres but I&#39;ll check it out, thanks </p></pre>ashishduhh1: <pre><p><a href="" rel="nofollow"></a></p></pre>Idontlikecold: <pre><p>Thank you this looks prefect :) </p></pre>FUZxxl: <pre><p>I wonder why they call it “INSERT ... SELECT,” it&#39;s just a normal <code>INSERT</code> statements with a <code>SELECT</code> subclause instead of a <code>VALUES</code> subclause.</p></pre>joushou: <pre><p>Probably because &#34;<code>INSERT</code> ... <code>SELECT</code>&#34; is shorter than &#34;A normal <code>INSERT</code> statement with a <code>SELECT</code> subclause instead of a <code>VALUES</code> subclause&#34;.</p></pre>EliAscent: <pre><p>Is it wise to store a hash and salt like that?</p></pre>jerf: <pre><p>The salt must be stored to work, because it has to be fed back to the hash algorithm to check it. Can&#39;t do that if you&#39;ve forgotten what it is.</p> <p>What&#39;s a bad sign is that they are being stored manually separated, when all the good password algorithms already store the salt in their default string serialization. It probably means the hash is md5 or something dangerously unsafe instead of bcrypt or scrypt or something safe.</p></pre>Idontlikecold: <pre><p>Actually they aren&#39;t separate, the salt in the struct is old code before I looked at go&#39;s hashing func. Everything is squeaky clean don&#39;t worry :) </p></pre>EliAscent: <pre><p>Ok great :]</p></pre>jerf: <pre><p>Acknowledged :)</p></pre>kemitche: <pre><p>Based on the later part of your question, it <em>sounds</em> like your main question is &#34;how do I insert multiple messages into the database with a single db.Exec?&#34;</p> <p>I&#39;d recommend using a tool like <a href="" rel="nofollow">squirrel</a> to help build the queries. It lets you do something like:</p> <pre><code>query = sq.Insert(&#34;messages&#34;).Columns(&#34;creator_id&#34;, &#34;message_body&#34;, &#34;create_date&#34;) for _, user := range users { query = query.Values(user.DbId, message, time.Now()) } sql, args, err := query.ToSql() if err != nil { panic(&#34;I build a bad query&#34;) } db.Exec(sql, args...) </code></pre></pre>

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

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