python - 42000 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server -


i'm having trouble python odbc code. don't following code work:

temp=process_query("select fname, lname employee ssn='%s'" %i)  known_hours=process_query("select distinct coalesce(hours,0)  works_on essn='%s'" %i)   temp.append(known_hours) 

where process_query takes form:

def process_query(query):     cursor1.execute(str(query)) 

(process_query continues more merely printing surposes, when i've searched web problem seems problem lies within how call execute function omitted rest of function here).

the error receive when i'm trying execute program is:

pyodbc.programmingerror: ('42000', "[42000] [mysql][odbc 5.1 driver][mysqld-5.1.66-0+squeeze1-log]you have error in sql syntax; check manual corresponds mysql server version right syntax use near 'john', [decimal('32.5'), decimal('7.5')], 'yes']'' @ line 1 (1064) (sqlexecdirectw)") 

would grateful if me out!

ps. if knows how omit "decimal" when i'm printing , instead have instance 32.5 appreciated with.

also know has been several topics regarding this, yet see , understand problem have select statement.

edit:

regarding how "i" implemented following:

i have initial list called thelist contains relevant social security numbers, ssn, "loop" through this:

for in thelist:      temp=process_query("select fname, lname employee ssn='%s'" %i)      known_hours=process_query("select distinct sum(coalesce(hours,0)) works_on essn='%s'" %i)      temp.append(known_hours)     unknown_hours=process_query("select distinct count(*) works_on isnull(hours) , essn='%s'" %i)      temp.append(unknown_hours) 

edit: thank answering!

i've changed beargle suggested. run error namely since loop through (the social secuirity numbers) in thelist have define these before.

hence use line

thelist=process_query('select distinct ssn employee', none)

where i've updated process_query to:

def process_query(query, parameters):

if(parameters none):      cursor1.execute(query)  else:      cursor1.execute(query, parameters)  (*)  n=0  lista = []  while 1:      row = cursor1.fetchone()              if not row:          break      lista.append(row[0])      n = n+1  if n==0:      print "no tuples matching given query found."  return lista     

the trouble program complains @ second cursor1.execute (marked asterix, *) ('the sql contains 1 parameter markers, 4 parameters supplied', 'hy000') believe stems social secuirity number , not single digit integer, cannot understand how fix issue.

i call as:

temp=process_query('select fname, lname employee ssn= ?', i)  known_hours=process_query('select distinct sum(coalesce(hours,0)) works_on essn=?', i) 

the i's in thelist identified before loop defining temp , such follows:

thelist=process_query('select distinct ssn employee', none)

which removed other errors got me new 1 stated previously. also, tried convert "i" int(i) , map(int,i) without getting release error.

use query parameters in cursor.execute() call of process_query function. handle escaping issues (protecting code against sql injection) , promotes statement preparation.

change process_query function accept 2 parameters, 1 sql string (containing parameter value placeholder) , 1 parameter values:

def process_query(sql, params):     cursor1.execute(sql, params) 

the for loop change to:

for in thelist:     temp=process_query('select fname, lname employee ssn=?', i)     known_hours=process_query('select distinct sum(coalesce(hours,0)) works_on essn=?', i)     temp.append(known_hours)     unknown_hours=process_query('select distinct count(*) works_on isnull(hours) , essn=?', i)     temp.append(unknown_hours) 

if doesn't resolve syntax problem, update question query string causing problem.


Comments

Popular posts from this blog

c++ - Creating new partition disk winapi -

Android Prevent Bluetooth Pairing Dialog -

php - joomla get content in onBeforeCompileHead function -