How do efficiently integrate MySQL into my project?

xuanbao · · 419 次点击    
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
<p>For reference, my current project is here: <a href="https://github.com/Jake-Ruston/a-level-cw/tree/master/src" rel="nofollow">https://github.com/Jake-Ruston/a-level-cw/tree/master/src</a></p> <p>So in <code>model/User.go</code> I have my <code>Register</code> function, which is supposed to validate inputs &amp; then insert that data into the database.</p> <p>In <code>handler/register.go</code> I have the lines</p> <pre><code>if err := user.Register(uType, cpWord); err != nil { fmt.Println(&#34;ERROR Register:&#34;, err) return } </code></pre> <p>which just calls that function. </p> <p>I don&#39;t know how to properly make use of connecting to MySQL and executing a query. Lines <code>22&gt;24</code> in <code>User.go</code> is where I would put the query, which I assumed would be something along the lines of</p> <pre><code>if _, e := db.Exec(&#34;INSERT INTO login SET user_type=&#39;?&#39;, username=&#39;?&#39;, password=&#39;?&#39;&#34;, uType, u.Username, hashedPassword); e != nil { fmt.Println(&#34;ERROR Exec:&#34;, err) return e } </code></pre> <p>but where do I put the <code>sql.Open(&#34;mysql&#34;, &#34;root:redacted@/exam-management&#34;)</code> line? I&#39;m going to need to query the database in multiple files so it doesn&#39;t make sense to keep opening it everywhere.</p> <hr/>**评论:**<br/><br/>nstratos: <pre><p>Please have a look at this thread. I think the answer will help you: </p> <p><a href="https://forum.golangbridge.org/t/best-practice-to-use-go-sql-driver-mysql-package/8028" rel="nofollow">https://forum.golangbridge.org/t/best-practice-to-use-go-sql-driver-mysql-package/8028</a></p></pre>irene634: <pre><p>I would recommend using this: <a href="https://github.com/jinzhu/gorm" rel="nofollow">https://github.com/jinzhu/gorm</a></p></pre>

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

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