Dismiss
Announcing Stack Overflow Documentation

We started with Q&A. Technical documentation is next, and we need your help.

Whether you're a beginner or an experienced developer, you can contribute.

Sign up and start helping → Learn more about Documentation →

I have a MySql query, everything works fine, but I want to make an array of each row to be used out of the query

Here is my code:

db = MySQLdb.connect(host="127.0.0.1", user="root", passwd="", db="seliso")

cursor = db.cursor()

cursor.execute("SELECT * FROM usuarios WHERE contador = " + "'" + userContador + "'")

results = cursor.fetchall()

for row in results:
    rfc = row[1]
    clave = row[2]

Example of I want to do:

db = MySQLdb.connect(host="127.0.0.1", user="root", passwd="", db="seliso")

cursor = db.cursor()

cursor.execute("SELECT * FROM usuarios WHERE contador = " + "'" + userContador + "'")

results = cursor.fetchall()

for row in results:
    rfc = row[1]
    clave = row[2]

$arrayRFC = ["rfc1", "rfc2", "rfc3"]
$arrayClave = ["clave1", "clave2", "clave3"]

How can this be acheived?

Thanks in advance.

share|improve this question
up vote 0 down vote accepted

You can get that with:

for i in zip(*results):
    print(list(i))

Further, you should not use string concatenation like that for reasons of SQL injection. Instead do:

cursor.execute("SELECT * FROM usuarios WHERE contador = %s",(userContador,))

Where %s is the parameter style per: http://mysql-python.sourceforge.net/MySQLdb.html

share|improve this answer
    
Thank you so much, you are the best! :) – gustavo serna Jul 14 at 3:29
    
Cheers, mate. Happy coding to you :-) – bernie Jul 14 at 3:30
    
but how can I use it out of "for:" – gustavo serna Jul 14 at 4:03
    
bro sorry, that isn't what I need. I need to use de "row" array out of "for:" – gustavo serna Jul 14 at 4:05
    
I'm not sure what you mean?... – bernie Jul 14 at 4:17

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.