当前位置:网站首页>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 .
边栏推荐
- Smart fan system based on stm32f407
- D30:color tunnels (color tunnels, translation)
- Arc135 partial solution
- How to prevent malicious crawling of information by one-to-one live broadcast source server
- C # basic knowledge (3)
- Sword finger offer day 4 (Sword finger offer 03. duplicate numbers in the array, sword finger offer 53 - I. find the number I in the sorted array, and the missing numbers in sword finger offer 53 - ii
- D29:post Office (post office, translation)
- How to quickly build high availability of service discovery
- Text replacement demo
- 股票开户最低佣金炒股开户免费,网上开户安全吗
猜你喜欢
Ningde times and BYD have refuted rumors one after another. Why does someone always want to harm domestic brands?
Unsafe and CAS principle
Weekly leetcode - nc9/nc56/nc89/nc126/nc69/nc120
What is the Valentine's Day gift given by the operator to the product?
2022.02.13
Smart fan system based on stm32f407
Amway by head has this project management tool to improve productivity in a straight line
Hcip 13th day notes
[note] IPC traditional interprocess communication and binder interprocess communication principle
Interpretation of corolla sub low configuration, three cylinder power configuration, CVT fuel saving and smooth, safety configuration is in place
随机推荐
Powerful blog summary
Pyqt5 sensitive word detection tool production, operator's Gospel
Get current JVM data
D28:maximum sum (maximum sum, translation)
Exclusive download! Alibaba cloud native brings 10 + technical experts to bring "new possibilities of cloud native and cloud future"
D27:mode of sequence (maximum, translation)
Qtoolbutton available signal
Hcip day 14 notes
股票开户最低佣金炒股开户免费,网上开户安全吗
finalize finalization finally final
JDBC Technology
D25:sequence search (sequence search, translation + problem solving)
How to make recv have a little temper?
ThreadLocal function, scene and principle
How to prevent malicious crawling of information by one-to-one live broadcast source server
How to understand the gain bandwidth product operational amplifier gain
IO flow principle and classification
Pyqt5 sensitive word detection tool production, operator's Gospel
Gossip about redis source code 82
Ningde times and BYD have refuted rumors one after another. Why does someone always want to harm domestic brands?