Python Forum
help, search in python mysql
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
Reply
#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.
Reply
#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
Reply
#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?
Reply
#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
Reply
#6
GREAT...
I HAVE A PROBLEM i must select... like variable....

so

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

...not work
Reply
#7
What does "not work" mean? What error do you get?
Reply
#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
Reply
#9
That syntax looks correct to me.
what is your error?
Reply
#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
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Mysql and mysql.connector error lostintime 2 612 Oct-03-2023, 10:25 PM
Last Post: lostintime
  Mysql error message: Lost connection to MySQL server during query tomtom 6 15,686 Feb-09-2022, 09:55 AM
Last Post: ibreeden
Question Debian 11 Bullseye | Python 3.9.x | pip install mysql-connector-python-rf problems BrandonKastning 4 6,574 Feb-05-2022, 08:25 PM
Last Post: BrandonKastning
  Python and MySql ogautier 8 3,250 May-20-2021, 11:10 PM
Last Post: Pedroski55
  Python and MySQL Pedroski55 5 3,180 Mar-23-2021, 06:40 AM
Last Post: ndc85430
  Python MySQL ogautier 0 2,084 Sep-03-2020, 03:54 PM
Last Post: ogautier
  Insert into mysql through python LaKhWaN 0 1,922 Aug-26-2020, 04:54 AM
Last Post: LaKhWaN
  Python mysql query help please tduckman 4 4,245 Mar-13-2020, 03:42 PM
Last Post: Marbelous
  mysql db connection using python sunstar20 13 11,883 Aug-04-2018, 09:08 AM
Last Post: vishalhule
  A combination of Python and MySql xgrzeniu 2 3,929 Mar-28-2018, 06:50 AM
Last Post: xgrzeniu

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020