Python and MySQL server

Bruno Desthuilliers bdesth.quelquechose at free.quelquepart.fr
Thu Jul 14 19:50:33 EDT 2005


Unknown a écrit :
> Python 2.4
> Linux kernel 2.6.12
> 
> Hi,
> 
> 1. How do I make the following statement to search for all Strings I
> input from console?
> 
> for example, with the code below I need to enter %hello world% (yeah,
> including the % symbols) to find all entries for hello world on the
> tableName. But I want to set the % symbols on the code itself so I don't
> have to input manually the % at the prompt.
> 
> searchWhat = raw_input ('Search for : ')
<q_and_d>
searchWhat = "%%%s%%" % raw_input ('Search for : ')

You have to double the % symbol to escape it.
</q_and_d>

You'd better be *very* careful with user input, specially when you use 
it like this:
> cursor.execute('select * from tableName where contentField like
> %s',(searchWhat))

Please consider checking and cleaning user inputs before using'em in a 
query.


> 2. I'm entering data by copying and pasting. Much of the text is in
> multiple lines and some formated sections such as paragraphs,
> indentations, double lines and what not.
> 
> How do I enter keep the formated text intact if entering from console? 

Launch a text-mode editor with a temp file name and read back that file 
if it exists once the editor is closed. Pseudo code:

fname = create_a_name_that_dont_exists_yet()
openEditor() # block until the editor is closed
if os.path.file_exists(fname):
   text_to_insert = open(fname).read()
   # ...


My 2 cents...







More information about the Python-list mailing list