Hello , i have two tables ,animal ,which has name,familie and weight and table food which has feed.
I want to assure that every animal has at least one food.
I did this :

import mysql.connector
from database import login_info

db=mysql.connector.Connect(**login_info)
cursor=db.cursor()

data=[('Geo','Elephant',['hay',peanuts']),
      ('Gerald','Gnu',['leaves','shoots']),
      ('Leo','Leopard',['meat'])
       ......................   ]

for name,familie,feed in data:
    cursor.execute("""SELECT name,feed FROM animal JOIN food ON animal.id=food.anid WHERE name=%s and feed=%s""",(name,feed))

    for feed in data:
        cursor.execute("""SELECT COUNT(*) feed FROM food""")
    food=cursor.fetchall()
    db.commit()
    for i in range(len(food)):
         if food[i]>=1:
            print("{0} "'has food'" {1}".format(name,food[i]))
         else:
            print("{0} "'has not food'"".format(name))

It gives me error 'failed processing format parameters' where i have 'for name,familie,feed'

You at least are missing ' before peanuts.

And before , you have ) without starting (

You at least are missing ' before peanuts.

Thanks ,but its not that

Yes it is. Do you get the error on the next line in the program? We don't know since you didn't include the entire error message, but it appears that the compiler has gone to the next line looking for the missing apostrophe.

## This works without any errors
data=[('Geo','Elephant',['hay','peanuts']),
      ('Gerald','Gnu',['leaves','shoots']),
      ('Leo','Leopard',['meat'])]

for name,familie,feed in data:
    print name, familie, feed

Also, the following line does nothing that I can see, and you only use commit() to commit any changes to the database, not after a query.

cursor.execute("""SELECT name,feed FROM animal JOIN food ON animal.id=food.anid WHERE name=%s and feed=%s""",(name,feed)) 
#
# Do you want "for name,familie,feed in data:" or "for feed in data:"
for name,familie,feed in data:
    cursor.execute("""SELECT name,feed FROM animal JOIN food ON animal.id=food.anid WHERE name=%s and feed=%s""",(name,feed))
 
    for feed in data:

P.S. For future reference, if you want some one to assist you with your code, at least try the suggestions before you say that that isn't it, and explain what happens after the change/correction. We will try them, so we know if they are correct or not.

Ok, sorry about that ,but it seems the '(' wasn't in my code originally.

I am getting the following error:

Traceback (most recent call last):
File "~/workspace/animals_foods/src/verify.py", line 14, in <module>
cursor.execute("""SELECT name,feed FROM animal JOIN food ON animal.id=food.anid WHERE name=%s and feed=%s""",(name,feed))

File "/usr/local/lib/python2.7/dist-packages/mysql/connector/cursor.py", line 307, in execute
stmt = operation % self._process_params(params)
File "/usr/local/lib/python2.7/dist-packages/mysql/connector/cursor.py", line 229, in _process_params
"Failed processing format-parameters; %s" % e)
mysql.connector.errors.ProgrammingError: Failed processing format-parameters; <type 'list'>

Thanks!

Yes it is. Do you get the error on the next line in the program? We don't know since you didn't include the entire error message, but it appears that the compiler has gone to the next line looking for the missing apostrophe.

## This works without any errors
data=[('Geo','Elephant',['hay','peanuts']),
      ('Gerald','Gnu',['leaves','shoots']),
      ('Leo','Leopard',['meat'])]

for name,familie,feed in data:
    print name, familie, feed

Also, the following line does nothing that I can see, and you only use commit() to commit any changes to the database, not after a query.

cursor.execute("""SELECT name,feed FROM animal JOIN food ON animal.id=food.anid WHERE name=%s and feed=%s""",(name,feed)) 
#
# Do you want "for name,familie,feed in data:" or "for feed in data:"
for name,familie,feed in data:
    cursor.execute("""SELECT name,feed FROM animal JOIN food ON animal.id=food.anid WHERE name=%s and feed=%s""",(name,feed))
 
    for feed in data:

P.S. For future reference, if you want some one to assist you with your code, at least try the suggestions before you say that that isn't it, and explain what happens after the change/correction. We will try them, so we know if they are correct or not.

I want 'for feed in data',but i use the previous for in order to extract and the name.
I want finally to have ,' Geo has food ('hay','peanuts') '

You have to iterate over the list.

data_list=[('Geo','Elephant',['hay','peanuts']),
      ('Gerald','Gnu',['leaves','shoots']),
      ('Leo','Leopard',['meat'])]
 

for name,familie,feed in data_list:
    print name, familie
    for each_item in feed:
        print "     ", each_item

Generator with join is of course alternative

>>> print '\n'.join(' '.join((name, species, ':', ', '.join(feeds))) for name, species,feeds in data_list)
Geo Elephant : hay, peanuts
Gerald Gnu : leaves, shoots
Leo Leopard : meat

String formatting operation is % not ,

print('%s, %s' % (1, 'a'))

Database version of this kind of operation can be seen in this snippet from documentation (? instead of %s and ,):

import sqlite3

con = sqlite3.connect("mydb")

cur = con.cursor()

who = "Yeltsin"
age = 72

cur.execute("select name_last, age from people where name_last=? and age=?", (who, age))
print cur.fetchone()

http://docs.python.org/release/2.5.2/lib/sqlite3-Cursor-Objects.html

There is also named version.

Yes, you were right (both of you).
Thank you for your help.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.