I use the following package for working with databases: https://github.com/ziutek/mymysql
There is such a function:
func new_query(Query string) { db := mysql.New("tcp", "", "127.0.0.1:3306", "root", "12345", "test") err := db.Connect() defer db.Close() if err != nil { log.Println("Не удалось подключиться к базе данных") return } stmt, err := db.Prepare(Query) _, err = stmt.Run() if err != nil { log.Println("Не удалось выполнить запрос") return } }
There is a code on golang:
Name string="Название" CreatedBy int=777 query string ="insert into table(Name,CreatedBy) values (?,?)" //???? new_query(query)
I need to get query ="insert into table(Name,CreatedBy) values ("Название",777)"
I tried to substitute %s
, but I won’t figure it out. The official site suggests first doing stmt, err := db.Prepare("insert into X values (?, ?)")
, But I want to make a universal function that you give sql text, and it performs it. And here, in fact, there is no db.Prepare
- I just passed the text, which can be with sql injection ...
If you pass a request to the function ("with inserts", where will it be necessary to substitute) and an array with parameters, but each request can have its own number of parameters - how can I iterate through them?
And also, is it possible with the help of regular expressions to somehow remove special characters from string
type or to somehow escape it, that there would not be sql injections?
Something seems to me at all, I reinvent the wheel)))