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
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',)
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
Build the test before inserting into the SQL statement like:
qvar = '%{}%'.format(var)
sql = "select valore from tab where valore LIKE qvar"
(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?
if var + 1 = var will always be true
# 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..
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'
# 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
You need to start using your noggin.
var =  '29'
oldvar = 0
Pages: 1 2