Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 help, search in python mysql
#1
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
Quote
#2
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.
Quote
#3
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
Quote
#4
(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?
Quote
#5
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
Quote
#6
GREAT...
I HAVE A PROBLEM i must select... like variable....

so

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

...not work
Quote
#7
What does "not work" mean? What error do you get?
Quote
#8

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
Quote
#9
That syntax looks correct to me.
what is your error?
Quote
#10
(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
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  mysql db connection using python sunstar20 13 2,781 Aug-04-2018, 09:08 AM
Last Post: vishalhule
  A combination of Python and MySql xgrzeniu 2 1,075 Mar-28-2018, 06:50 AM
Last Post: xgrzeniu
  ssh + mysql connection python 3.4.3 desudesu 9 6,285 May-25-2017, 03:19 PM
Last Post: desudesu
  Python 3 mysql export to csv elliott0184 3 9,093 Feb-23-2017, 03:18 PM
Last Post: joe_anonimist

Forum Jump:


Users browsing this thread: 1 Guest(s)