Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #41625 > unrolled thread

Re: Multiple rows from MySQL query saved in an array structure

Started byDennis Lee Bieber <wlfraed@ix.netcom.com>
First post2013-03-20 19:38 -0400
Last post2013-03-20 19:38 -0400
Articles 1 — 1 participant

Back to article view | Back to comp.lang.python

This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by below is the oldest one visible, not the original post.


Contents

  Re: Multiple rows from MySQL query saved in an array structure Dennis Lee Bieber <wlfraed@ix.netcom.com> - 2013-03-20 19:38 -0400

#41625 — Re: Multiple rows from MySQL query saved in an array structure

FromDennis Lee Bieber <wlfraed@ix.netcom.com>
Date2013-03-20 19:38 -0400
SubjectRe: Multiple rows from MySQL query saved in an array structure
Message-ID<mailman.3577.1363822717.2939.python-list@python.org>
On Wed, 20 Mar 2013 12:52:40 +0000, Norah Jones <nh.jones01@gmail.com>
declaimed the following in gmane.comp.python.general:

> I  have this Python code:
> 
> 
>     self.lock_tables("read", ['nets_permissions as n', 'devices_permissions as d'])
>     usrs = self.db.get("SELECT n.user_id FROM nets_permissions as n \
>                         left join devices_permissions as d \
>                         on n.user_id = d.user_id \
>                         where d.user_id is null \
>                         and n.network_id=%s and n.perm3", netid)
>     self.unlock_tables()
>     
>     for usr in usrs:
>         self.lock_tables("write", ['devices_permissions'])
>         self.db.execute("INSERT devices_permissions SET \
>                          user_id=%s, network_id=%s, device_id=%s, perm=%s",\
>                          usr, netid, sensid, perm)
>         self.unlock_tables();
> 
> I first do a query to retrieve some user_id from two tables. I want save this user_id in one variable and after do a for loop to insert this records in another table...
> 
> 
>     This code doesn't work. I obtain this error:    
>     Exception: Multiple rows returned for Database.get() query
> 
> How can I retrieve this multiple rows and then process everyone of them at one time?

	What database adapter module are you using... The above sure doesn't
look like MySQLdb... Using MySQLdb's db-api functions the above would
probably look something like (ignoring that your code sample appears to
part of some method in a class):

import MySQLdb as db

con = db.connection(specify DB, user, password, etc.)

cur = con.cursor()

rslt = cur.execute("""select n.user_id from nets_permissions as n
						left join devices_permissions as d
							on n.user_id = d.user_id
					where d.user_id is null
						and n.network_id = %s and n.perm3""",
					netid)

***** UHM! "where d.user_id is null" implies (to me) that the join won't
find anything -- n.user_id = d.user_id suggests you want NULL ids on
both sides, but by definition NULL is never equal to NULL!

dta = cur.fetchall()
con.commit()	#end implicit transaction

for usr in dta:
	cur.execute("""insert devices_permissions 
						(user_id, network_id, device_id,
								perm)
					values (%s, %s, %s, %s)""",
					(usr, netid, sensid, perm)	)
con.commit()
-- 
	Wulfraed                 Dennis Lee Bieber         AF6VN
        wlfraed@ix.netcom.com    HTTP://wlfraed.home.netcom.com/

[toc] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web