I have to include in the text to insert a single quote but I can not use double single quote to escape it.
For example if I have:
'it has single 'quote
I cant not use:
insert into value ( field1) values ( 'it has single ''quote' )
It is because I have to keep the database values identically to the original value.
My code is golang and the value is contained in a variable that is used in a bulk insert like this:
sqlStr := "INSERT INTO test(n1, n2, n3) VALUES "
vals := []interface{}{}
for _, row := range data {
sqlStr += "(?, ?, ?),"
vals = append(vals, row["v1"], row["v2"], row["v3"])
}
sqlStr = strings.TrimSuffix(sqlStr, ",")
sqlStr = ReplaceSQL(sqlStr, "?")
stmt, _ := db.Prepare(sqlStr)
res, _ := stmt.Exec(vals...)
What kind of solution is better in this case where the text that contains a single quote to escape is contained in a variable to insert in postgres database ?