sql - Python/SQLite3 escaping in WHERE-Clause -


How do I survive in Python for SQLite3?

If I have many questions for this (Google search or Stackoverflow) and every time the feedback is something like this:

  dbcursor.execute ("SELECT * FROM` foo 'Where's the' bar '? ", [" Phobar "])  

This helps against SQL-injection, and if I only comperations with" = " So enough, but it certainly does not use wildcards.

So if I set

  cursor.exicute (u) "cookies set" cookies, then `counter` =? WHERE 'Empty Name' iii? ", (Cookies, names))  

Some users may supply"% "for a nickname and will replace all cookie entries with one line. I can (ie ... maybe I'll forget at least one of those Wildcards), I can use lowercase on nick and nickname and I can change "IIL" with "=", but I actually What I would like to do with some of the lines:

  foo = sqlesc Ape (nick) + "%" cursor.extech (u) "set cookies" set `calculation` =? Where` nickname 'iii?", (Cookies, foo))  

You have saved full code injection using parametric queries. Now it seems that you are trying to match a pattern with the data given by the user, but you want that the portion provided by the user of the data will not be considered as real data (hence no wildcard). You have several options:

  1. Simply filter the input as the wildcard in the form of SQLite's like only % and _ , so it is very difficult to get it wrong just to ensure the input input filter (my preferred method: filter before creating the query, not when user input is read). Typically, removal of the "whitelist" approach is considered to be more safe and easy than the typical dangerous character, its string (and any "less-known wildcard" , As you say) instead of deleting % and _ , scan your string and only keep the characters that you want. For example, if your "alias" contains ASCII characters, digits, "-" and "." , It can be cleared in such a way:

      name = re.sub ("[^ A This solution is specific to the particle area that you are matching And works well for major areas and other identifiers. If I had to search with  RLIKE  then I would definitely do it in a manner that accepts full regular expressions, so its There are too many characters. 

  2. If you do not want users If the client can supply the wildcard, why would you use like in your query? If your questions come from many places in the input code (or you may be writing a library too) ), You can make your query secure if you can completely avoid LIKE :

    • here:

      < Pre> SELECT * FROM ... WHERE name = 'any' COLLATE NOCASE
    • In your example, you can specify prefix matching (" sqlescape ( Nick) + "%" "). How to do it with exact search:

        size = lane (nick) cursor.extech (u)" cookies "set` calculation` =? WHERE substr (`nickname`, 1,?) =? ", (Cookies, size, nick))  

Comments

Popular posts from this blog

import - Python ImportError: No module named wmi -

Editing Python Class in Shell and SQLAlchemy -

c# - MySQL Parameterized Select Query joining tables issue -