Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

while loop in python sql query

Hi i have try this query in php which is running fine and i have to do this same in python

   $select=mysql_query("SELECT DISTINCT A.entity_id AS entity_id ,A.email AS email,A.catquizid AS style_quiz_score ,A.catquizquesans AS style_quiz_answer,A.created_at AS date_joined,A.is_active AS is_active ,B.attribute_id AS attribute_id,B.value AS info FROM `customer_entity` AS A inner join  `customer_entity_varchar` AS B on A.entity_id=B.entity_id WHERE B.`attribute_id` IN  (1,2) limit 10",$conn);

    $arr=array();

    while($result= mysql_fetch_assoc($select))
        { 
            if(!isset($arr[$result['entity_id']]['lastname'])){
                $arr[$result['entity_id']]['firstname'] = $result['info'];
            }
            $arr[$result['entity_id']]['lastname'] = $result['info'];
            $arr[$result['entity_id']]["email"]=$result['email'];
            $arr[$result['entity_id']]["style_quiz_score"]=$result['style_quiz_score'];
            $arr[$result['entity_id']]["style_quiz_answer"]=$result['style_quiz_answer'];
            $arr[$result['entity_id']]["date_joined"]=$result['date_joined'];
            $arr[$result['entity_id']]["is_active"]=$result['is_active'];

            $arr[$result['entity_id']]["username"]=normalize_str($result['email']);
        }

and in python i have tried this

def customer_migrate(request):
    cursor = connections['migration'].cursor()
    cursor.execute("SELECT DISTINCT A.entity_id AS entity_id ,A.email AS email,A.catquizid AS style_quiz_score ,A.catquizquesans AS style_quiz_answer,A.created_at AS date_joined,A.is_active AS is_active ,B.attribute_id AS attribute_id,B.value AS info FROM customer_entity AS A inner join  customer_entity_varchar AS B on A.entity_id=B.entity_id WHERE B.attribute_id limit 4 ")
    row = cursor.fetchall()

how can i use the while loop in the python query ,

like image 200
Rohit Goel Avatar asked Apr 12 '26 04:04

Rohit Goel


1 Answers

Use fetchone() or just iterate through the cursor:

row = cursor.fetchone()
while row is not None:
    print row
    row = cursor.fetchone()

or

for row in cursor:
    print row
like image 138
Lysergia25 Avatar answered Apr 13 '26 16:04

Lysergia25



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!