How to process the string for storing in MySQL db?

polaris · 2015-10-21 11:34:55 · 805 次点击    
这是一个分享于 2015-10-21 11:34:55 的资源,其中的信息可能已经有所发展或是发生改变。

Hi,

In this moment I writing a little application for web at Golang. I wish to save a string from HTML form to MySQL database but I have worry about special characters and escape sequences. I wrote a little function with regex but I think that it is not enough for a production ("really world" :wink: ). Maybe you know something about frameworks or library for Golang for safety string processing? (like as mysql_real_escape_string in PHP)

Thank you :wink:


评论:

brokedown:

Those escapes are really only needed if you're creating a query string by hand with data, which you should never do with dynamic arguments.

In Go, you would do something like:

_,err:=db.Exec("insert into foo (fieldA, fieldB) values (?, ?)", valueA, valueB)
Brasilikum:

Citing a stack overflow answer:

As long as you're using Prepare or Query, you're safe.

// this is safe db.Query("SELECT name FROM users WHERE age=?", req.FormValue("age")) // this allows sql injection. db.Query("SELECT name FROM users WHERE age=" + req.FormValue("age"))

whitedruid:

Thanks! I try to use both solutions and I have no feel a diffrent but I'm newbie at software development at Go ;) ..... I found this: https://astaxie.gitbooks.io/build-web-application-with-golang/content/en/09.4.html

Brasilikum:

In the first case you are concatenating everything to one string and then pass it to the db-package. If it contains illegal characters, they are evaluated. If you pass many strings, the dB package may check every single one for illegal characters

3264128256:

As others have said if you're worried about SQL injection, prepared statements will get rid of those.

If you're worried about displaying the said HTML on a browser, it's a different issue. Go's template/htmloutputs HTML which is safe against code injection. Or if you're using something else you could process it through HTMLEscapeString or HTMLEscape


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

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