Python Forum

Full Version: help, search in python mysql
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2
hi i have a raspberry pi3.

I install php mysql et...

Now i want search if value is present in table.

helpppp!

my code is
import time
import MySQLdb

val = 'BBBBB'

db = MySQLdb.connect(host="localhost", user="", passwd="", db="")
        #create a cursor for the select
cursor = db.cursor()


# if val exist in tabella1 then insert in tabella2

if 
sql = "select * from tabella1 where valore = 'BBBBB' "

.....
....
anyone help me please
You are going to have to learn basic SQL.

One excellent tutorial is available here

It's easy to learn, and you'll be doing basic queries in a short time.
What error do you get?

what is the actual code that you are using?
The indentation on the snippet you show is wrng, and would give an error.

What is the exact traceback that you are getting? please post (verbatim)


The syntax to select a particular field for a table would be
SELECT field FROM table WHERE otherfield = 'whatever';
Also you are not showing enough code, setting a string named sql is part of it,
but that string must be executed with the cursor
(Dec-15-2016, 10:53 PM)Larz60+ Wrote: [ -> ]What error do you get?

what is the actual code that you are using?
The indentation on the snippet you show is wrng, and would give an error.

What is the exact traceback that you are getting? please post (verbatim)


The syntax to select a particular field for a table would be
SELECT field FROM table WHERE otherfield = 'whatever';
Also you are not showing enough code, setting a string named sql is part of it,
but that string must be executed with the cursor
wonderful!!
works!!
great!
Ok  i find value.
Now... if found value  ...
How check that the search give result?
You can put your query into an try except loop,
then set whatever you want to be your fail indicator in the except part

See answer 1 here: http://stackoverflow.com/questions/23599...uery-fails
GREAT...
I HAVE A PROBLEM i must select... like variable....

so

SELECT valore from table WHERE valore LIKE ('%variable%');

...not work
What does "not work" mean? What error do you get?
import time
import MySQLdb
var = 'AAAA'

db = MySQLdb.connect(host="localhost", user="root", passwd="", db="db")


cursor = db.cursor()



sql = "select valore from tab where valore LIKE '%$var%' "

number_of_rows = cursor.execute(sql)

print(cursor.fetchone())
db.close()
result: None
That syntax looks correct to me.
what is your error?
(Dec-19-2016, 11:16 PM)Larz60+ Wrote: [ -> ]That syntax looks correct to me.
what is your error?

AAA value is in the tab of db
Pages: 1 2