Insert Struct MySQL

agolangf · · 786 次点击    
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
<p>This is the only way I can insert values into MySQL. How do I insert an struct so I do not that to explicitly tell it the variables/ values? Thanks</p> <pre><code>stmtIns, err := db.Prepare(&#34;INSERT INTO Data VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)&#34;) if err != nil { panic(err.Error()) } defer stmtIns.Close() _, err = stmtIns.Exec(101, &#34;yoyo&#34;, 14, 67, 10745, 9592, 295, sql.NullInt64{Int64: 42, Valid: true}, sql.NullInt64{Int64: 42, Valid: true}, sql.NullInt64{Int64: 42, Valid: true}, sql.NullInt64{Int64: 42, Valid: true}, sql.NullInt64{Int64: 42, Valid: true}, sql.NullInt64{Int64: 42, Valid: true}, sql.NullInt64{Int64: 42, Valid: true}, sql.NullInt64{Int64: 42, Valid: true}, sql.NullInt64{Int64: 42, Valid: true}, sql.NullInt64{Int64: 42, Valid: true}, sql.NullInt64{Int64: 42, Valid: true}, sql.NullInt64{Int64: 42, Valid: true}) if err != nil { panic(err.Error()) } </code></pre> <hr/>**评论:**<br/><br/>floatdouble: <pre><p>This library works pretty well for what you want:</p> <p><a href="https://github.com/jinzhu/gorm" rel="nofollow">https://github.com/jinzhu/gorm</a></p></pre>jmoiron: <pre><p>with <a href="https://github.com/jmoiron/sqlx" rel="nofollow">sqlx</a>:</p> <pre><code>type Foo struct{ ID int Name string // etc } v := Foo{ID: 1, Name: &#34;Ardie Savea&#34;} db, err := sqlx.Open(...) // same as sql.Open stmt, err := db.PrepareNamed(`INSERT INTO data VALUES (:id, :name)`) _, err = stmt.Exec(v) </code></pre></pre>fnetma: <pre><p>This is the error I am getting <a href="https://ibb.co/htH9hF" rel="nofollow">https://ibb.co/htH9hF</a></p> <pre><code>v := models.Data{102, &#34;yoyo&#34;, 14, 67, 10745, 9592, 295, sql.NullInt64{Int64: 42, Valid: true}, sql.NullInt64{Int64: 42, Valid: true}, sql.NullInt64{Int64: 42, Valid: true}, sql.NullInt64{Int64: 42, Valid: true}, sql.NullInt64{Int64: 42, Valid: true}, sql.NullInt64{Int64: 42, Valid: true}, sql.NullInt64{Int64: 42, Valid: true}, sql.NullInt64{Int64: 42, Valid: true}, sql.NullInt64{Int64: 42, Valid: true}, sql.NullInt64{Int64: 42, Valid: true}, sql.NullInt64{Int64: 42, Valid: true}, sql.NullInt64{Int64: 42, Valid: true}, sql.NullInt64{Int64: 42, Valid: true}} db, err := sqlx.Open(&#34;mysql&#34;, &#34;----------------&#34;) // same as sql.Open if err != nil { panic(err.Error()) } stmt, err := db.PrepareNamed(`INSERT INTO data VALUES (:id, :created, :team_id, :status, :acceleration, :position, :velocity, :battery_voltage, :battery_current, :battery_temperature,:pod_temperature, :stripe_count, :pod_pressure, :switch_states ,:pr_p1, :pr_p2, :br_p1, :br_p2, :br_p3, :br_p4)`) _, err = stmt.Exec(v) if err != nil { panic(err.Error()) } </code></pre> <p>Nothing is being sent to MySQL and is exiting. Not sure what to do. Any help would be great. Thanks</p></pre>jmoiron: <pre><p>How is <code>models.Data</code> defined? There is some <a href="http://jmoiron.github.io/sqlx/#namedParams" rel="nofollow">documentation about named queries</a> that may be helpful as well.</p></pre>KevBurnsJr: <pre><p>Here&#39;s how I have typically done mysql inserts in the past</p> <p><a href="https://gist.github.com/KevBurnsJr/9abdeb0bfa0cf32cff4839da15e8c2a8" rel="nofollow">https://gist.github.com/KevBurnsJr/9abdeb0bfa0cf32cff4839da15e8c2a8</a></p></pre>

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

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