Get the App
SLTechnology News&Howtos  ›  Database  › 

An example of mysql string escape function

Shulou Source: shulou.com Published: 2022-05-31 18:01:18 10月31日 Update

This article mainly introduces "mysql string escape function examples". In daily operation, I believe many people have doubts about mysql string escape function examples. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful for you to answer the doubts of "mysql string escape function examples". Next, please follow the editor to study!

Inserting a data table in mysql will cause syntax errors if there is a'\ "in the data. There is no need to explain this, you know, so you need to write an escape function. At first, you want to use string's replace directly, but this method needs to be called three times.

It feels inefficient to put it on the server, so I write one in C, which feels OK. The time complexity has nothing to do with the number of escape characters that need to be escaped. If the source string does not match, there is no need for copy operation.

/ / escape function, 'convert to\'

Bool ConverToDBStr (const char* src,char*dst)

{

Int I =-1 ram / record the location of the last match

Int dOffset = 0 position hand / target string cursor

Int nCopy = 0 copy / the number of bytes that need to be copy this time

Const char*p = src

While (* p)

{

If (* paired bands'\\'| | * paired frames'\'| | * paired fragments'"'")

{

NCopy = p-src-i-1

Memcpy (dst+dOffset,src+i+1,nCopy)

DOffset+=nCopy

* (dst+Offset) ='\\'

* (dst+dOffset+1) = * p

DOffset+=2

I = p-src

}

Pendant +

}

If (dOffset > 0) / / matches the last string of copy

{

Memcpy (dst+dOffset,src+i+1,p-src-i)

Return true

}

Return false

}

As you can see, replace can be implemented with a slight change to this function.

At this point, the study of "mysql string escape function instance" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

Tags: Function character escape string instance learning feeling data method more help complex practical next no number position complexity byte efficiency Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Huawei Shulou Technology Shulou Information Linux Redmi