473,698 Members | 2,556 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

[newbie] Right way to access item in array?

Hello

I'd like to know what the right way is to access an item in a row as
returned by a database:

=====
import apsw

connection=apsw .Connection("te st.sqlite")
cursor=connecti on.cursor()

rows=cursor.exe cute("SELECT isbn,price FROM books WHERE price IS
NULL")
for row in rows:

#Is this right?
for isbn in row:

if isbn:
print "Found price = " + price

connection.clos e(True)
=====

Thank you.
Oct 28 '08 #1
5 1173
Gilles Ganault wrote:
Hello

I'd like to know what the right way is to access an item in a row as
returned by a database:

=====
import apsw

connection=apsw .Connection("te st.sqlite")
cursor=connecti on.cursor()

rows=cursor.exe cute("SELECT isbn,price FROM books WHERE price IS
NULL")
for row in rows:

#Is this right?
for isbn in row:
No. This will iterate though all columns, not just the isbn.

You can use tuple-unpacking in such cases:

for row in rows:
isbn, price = row
...
Diez
Oct 28 '08 #2
Diez B. Roggisch wrote:
Gilles Ganault wrote:
>Hello

I'd like to know what the right way is to access an item in a row as
returned by a database:

=====
import apsw

connection=aps w.Connection("t est.sqlite")
cursor=connect ion.cursor()

rows=cursor.ex ecute("SELECT isbn,price FROM books WHERE price IS
NULL")
for row in rows:

#Is this right?
for isbn in row:

No. This will iterate though all columns, not just the isbn.

You can use tuple-unpacking in such cases:

for row in rows:
isbn, price = row
...
You can do it even in one step with APSW (and pysqlite, and others):

for isbn, price in cur.execute("se lect isbn, price ..."):
....

-- Gerhard

Oct 28 '08 #3
Gilles Ganault wrote:
Hello

I'd like to know what the right way is to access an item in a row as
returned by a database:

=====
import apsw

connection=apsw .Connection("te st.sqlite")
cursor=connecti on.cursor()

rows=cursor.exe cute("SELECT isbn,price FROM books WHERE price IS
NULL")
If you are dealing with a DB API-compliant module then the return value
from the cursor's execute method is undefined, and you need to call one
of the "fetch" methods to extract the retrieved data.

So you would want something like

cursor.execute( "SELECT isbn,price FROM books WHERE price IS NULL")
rows = cursor.fetchall ()
for isbn, price in rows:
print isbn, ":", price

Once you get that working you can do your own computations with isbn and
price. Note, however, that the specific query you use guarantees that
the value of "proce" will be None, since you only retrieve the rows
where price is NULL!
for row in rows:

#Is this right?
for isbn in row:

if isbn:
print "Found price = " + price

connection.clos e(True)
regards
Steve
--
Steve Holden +1 571 484 6266 +1 800 494 3119
Holden Web LLC http://www.holdenweb.com/

Oct 28 '08 #4
On Tue, 28 Oct 2008 12:12:26 +0100, Gerhard Häring <gh@ghaering.de >
wrote:
>You can do it even in one step with APSW (and pysqlite, and others):

for isbn, price in cur.execute("se lect isbn, price ..."):
Thanks much guys. For those interested, here's some working code:

======
import apsw

connection=apsw .Connection("my books.sqlite")
cursor=connecti on.cursor()

for id, isbn in list(cursor.exe cute("SELECT id,isbn FROM books WHERE
price IS NULL")):

print isbn

connection.clos e(True)
======

HTH,
Oct 28 '08 #5
On Tue, 28 Oct 2008 09:56:11 -0400, Steve Holden <st***@holdenwe b.com>
wrote:
>If you are dealing with a DB API-compliant module then the return value
from the cursor's execute method is undefined, and you need to call one
of the "fetch" methods to extract the retrieved data.
Thanks for pointing it out. I read that calling list() on the dataset
is the way to go with this module:

for id, isbn in list(cursor.exe cute("SELECT id,isbn FROM books WHERE
price IS NULL")):
Oct 28 '08 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
2573
by: Terry Olsen | last post by:
How can I catch a right-click on a DropDownMenuItem?
8
15436
by: jh | last post by:
I'd like to copy/paste into a listbox during runtime. I can do this for a textbox but can't figure out how to accomplish this for a listbox. Any help? Thanks.
52
7807
by: marc | last post by:
Hello, Is it possible to right pad with "0" (or other character != blank) ? for example : 1 , length 10 ="1000000000" I've tried with sprintf but I can only left pad with "0" or right pad with blanks => "0000000001" "1 "
24
2949
by: joeldault | last post by:
Question For Microsoft Access Data Base -------------------------------------------------------------------------------- I am Trying to create a single formula that would do the following: If A1 is less then or equal to 499, deliver B1*1, if A1 is equal to or less than 999 but more than and including 500, deliver B1*2, If A1 is equal to or less than 1499 but more than and including 1000, deliver B1*3, AND SO ON..... I have read the...
0
8683
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8609
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9170
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8871
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7739
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6528
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5862
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4622
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2007
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.