Python and MySQL server

U

Unknown

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 : ')
cursor.execute('select * from tableName where contentField like
%s',(searchWhat))

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? Now
it loses all formatting and I have to copy and paste text line by line
because it won't take the multiple lines.

insertEntryId = raw_input('New Entry ID: ')
insertEntryContent = raw_input('New Entry Content: ')
insertEntryCategory = raw_input('New Category: ')

cursor.execute('insert into tableName values (%s,%s,%s)',
(insertEntryId,insertEntryContent,insertEntryCategory))

Thanks, Any help is appreciate it.

Mr. Frodo
 
B

Bruno Desthuilliers

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...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,262
Messages
2,571,056
Members
48,769
Latest member
Clifft

Latest Threads

Top