当前位置:网站首页>It is forbidden to splice SQL in code
It is forbidden to splice SQL in code
2022-07-03 23:40:00 【hzp666】
1. String manipulation It's easier to make mistakes .
2. sql sentence It inevitably appears in the code , Can't sit on the separation of code and data . Code readability is reduced .
3. efficiency . In many cases, you need to execute the same sentence multiple times sql sentence , But the parameters are different . If you use PreparedStatement(Java), You only need to compile at the first execution sql sentence , After that, the execution efficiency can be improved .
4. If string operations are used in the code to splice sql sentence , Then it is impossible to find sql Statement error . If you use the method provided by the class library to set parameters , You can set the type of parameters at compile time .
5. If you want to modify it later sql sentence , such as where Add one more condition to the condition , Or to optimize performance, change the order of filter conditions , At this time, you will find that string splicing is a disaster , Especially in sql When the sentences are complicated ( Nested , Multiple tables intersect ).
6. Security . String splicing sql Statements are vulnerable to sql Injection attack .
SQL Injection is a common attack against database . In this way of attack , The attacker will insert some malicious code into the string . Then the string will be passed to SQLServer Analysis and execution in the instance of database . As long as the malicious code matches SQL Rules for statements , When the code is compiled and executed , Will not be found by the system . thus it can be seen SQL The harm of injection attack is great .
prevent SQL Inject :
On the whole , prevention and cure SQL There are two methods for injection attack , First, strengthen the inspection and verification of user input ; The second is to force the use of parameterized statements to pass user input .
For example, we used to use :
insert into test values('"+id+"','"+name+"');
Now it can be changed to :
string cmd = "insert into test values(@id,@name)";
MySqlParameter[] paras ={undefined
new MySqlParameter("@id",MySqlDbType.String),
new MySqlParameter("@name",MySqlDbType.String)
};
paras[0].Value = ID;
paras[1].Value = Name;
Parameterized sql The only disadvantage of the statement is the problem of occupying system resources , Because it is something that has been precompiled long ago , So the system is directly used when calling .
But compared with the direct splicing SQL sentence . The advantages outweigh the disadvantages .
7. It can be reconstructed later ? Why not change it now , The earlier the change, the less the cost .
=================
Simply put, it is to put ,
Put this +++++ Get rid of , Very irregular and unsafe , Switch to SqlParameter
=================
sql Injection is a popular vulnerability in the last century ,
Now there are sql Inject vulnerabilities , You will be laughed at by your peers Took a rough look , Variables are all numeric types , There should be no injection problem Or use it sqlparameter Well , Be normal , Don't worry about injection , Thorough solution .
in addition , I do not know! sqlserver What about? , stay oracle in , When it comes to performance . If parameterized sql, Every time it is sent to the database sql If it has been compiled , Then it will not be recompiled next time , Just pass different parameters to the database server ; And if you splice variable values into sql in , Then each time it is transmitted to the database server sql The probability of statement repetition is very low , The database server compiles every time , Cause performance degradation .
边栏推荐
- ADB command to get XML
- Yyds dry goods inventory [practical] simply encapsulate JS cycle with FP idea~
- Selenium check box
- X Opencv feature point detection and matching
- D27:mode of sequence (maximum, translation)
- Apple released a supplementary update to MacOS Catalina 10.15.5, which mainly fixes security vulnerabilities
- Gossip about redis source code 83
- Arc135 partial solution
- Bufferpool caching mechanism for executing SQL in MySQL
- Ningde times and BYD have refuted rumors one after another. Why does someone always want to harm domestic brands?
猜你喜欢

Bufferpool caching mechanism for executing SQL in MySQL

MLX90614 driver, function introduction and PEC verification

In VS_ In 2019, scanf and other functions are used to prompt the error of unsafe functions

Hcip day 15 notes

Sort merge sort

Design of logic level conversion in high speed circuit

Research Report on the scale prediction of China's municipal engineering industry and the prospect of the 14th five year plan 2022-2028

How to write a good title of 10w+?

Report on the construction and development mode and investment mode of sponge cities in China 2022-2028

Hcip day 12 notes
随机推荐
SPI based on firmware library
Research Report on the scale prediction of China's municipal engineering industry and the prospect of the 14th five year plan 2022-2028
Gossip about redis source code 81
How to prevent malicious crawling of information by one-to-one live broadcast source server
EPF: a fuzzy testing framework for network protocols based on evolution, protocol awareness and coverage guidance
Text replacement demo
D23:multiple of 3 or 5 (multiple of 3 or 5, translation + solution)
A treasure open source software, cross platform terminal artifact tabby
Is the controller a single instance or multiple instances? How to ensure the safety of concurrency
33 restrict the input of qlineedit control (verifier)
D27:mode of sequence (maximum, translation)
Ningde times and BYD have refuted rumors one after another. Why does someone always want to harm domestic brands?
D29:post Office (post office, translation)
China standard gas market prospect investment and development feasibility study report 2022-2028
MLX90614 driver, function introduction and PEC verification
Amway by head has this project management tool to improve productivity in a straight line
QT creator source code learning note 05, how does the menu bar realize plug-in?
Unsafe and CAS principle
Ningde times and BYD have refuted rumors one after another. Why does someone always want to harm domestic brands?
Pandaoxi's video