Python Forum
help, search in python mysql
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
help, search in python mysql
#11
If i use no a variable but a value

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 '%AA%' "
 
number_of_rows = cursor.execute(sql)
 
print(cursor.fetchone())
db.close()
result is ok

('AAAA',)
Reply
#12
WINNNNNN

WORK
......

sql = """select valore from tab where valore LIKE '%s' """ % ("%" +var+ "%")
print (%cursor.fetchone())

now i have other problem...

my cycle (have  to do cycle one time even if var not change):

var=1


if var != oldvar
oldvar=var
do.....
else
Reply
#13
Build the test before inserting into the SQL statement like:
qvar = '%{}%'.format(var)
sql = "select valore from tab where valore LIKE qvar"
Reply
#14
(Dec-20-2016, 04:35 PM)Larz60+ Wrote: Build the test before inserting into the SQL statement like:
qvar = '%{}%'.format(var)
sql = "select valore from tab where valore LIKE qvar"

sorry for my bad english i would say;

my cycle have to do if actual var is different from prev var
do it
if var+1 != var
can u help me?
Reply
#15
if var + 1 = var will always be true
Reply
#16
# var is a value for ex temperature of sensor 
# FOR EXAMPLE 
var = '29'

if var != oldvar
oldvar = var
do.....
else
The problem is when first loop of cycle start: if the first temperature is x....
How: oldvar = var ?
HELP..
Reply
#17
why is your indentation all messed up?
You should cut and paste
If using PyCharm (and perhaps some other IDE's) you may have to use ctrl-shift-v

It looks like you are trying to use oldvar before it has been defined
to start, use:
var = oldvar = '29'
Reply
#18
# var is a value for ex temperature of sensor 
# FOR EXAMPLE 
var =  oldvar  = '29'

if var != oldvar
      
       do.....
else
if i write this:

1. first temperature detected is 29
2. assig to oldvar 29
3.cyle never start
i need that cycle start for first time in any case and at th 2nd time  when the 2nd..3...4...5.. etc.. temperature detected is different only from previus
Reply
#19
You need to start using your noggin.
var =  '29'
oldvar = 0
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Mysql and mysql.connector error lostintime 2 681 Oct-03-2023, 10:25 PM
Last Post: lostintime
  Mysql error message: Lost connection to MySQL server during query tomtom 6 16,063 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,687 Feb-05-2022, 08:25 PM
Last Post: BrandonKastning
  Python and MySql ogautier 8 3,362 May-20-2021, 11:10 PM
Last Post: Pedroski55
  Python and MySQL Pedroski55 5 3,241 Mar-23-2021, 06:40 AM
Last Post: ndc85430
  Python MySQL ogautier 0 2,106 Sep-03-2020, 03:54 PM
Last Post: ogautier
  Insert into mysql through python LaKhWaN 0 1,945 Aug-26-2020, 04:54 AM
Last Post: LaKhWaN
  Python mysql query help please tduckman 4 4,316 Mar-13-2020, 03:42 PM
Last Post: Marbelous
  mysql db connection using python sunstar20 13 11,996 Aug-04-2018, 09:08 AM
Last Post: vishalhule
  A combination of Python and MySql xgrzeniu 2 3,985 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