How to add a current string into an already existing list

N

Nick the Gr33k

Στις 5/11/2013 11:10 πμ, ο/η M.F. έγÏαψε:
data = cur.fetchall()
That is what the stack trace and Christ tried to inform you.

I see, but because of the traceback not being to express it more easily
i was under the impression that data wasn't what i expected it to be.
 
N

Nick the Gr33k

Στις 5/11/2013 11:34 πμ, ο/η Nick the Gr33k έγÏαψε:
Στις 5/11/2013 11:10 πμ, ο/η M.F. έγÏαψε:

I see, but because of the traceback not being to express it more easily
i was under the impression that data wasn't what i expected it to be.


Still similar error here:

=================================
# if first time for webpage; create new record( primary key is
automatic, hit is defaulted ), if page exists then update record
cur.execute('''INSERT INTO counters (url) VALUES (%s) ON DUPLICATE KEY
UPDATE hits = hits + 1''', page )
cID = cur.lastrowid

# fetch those columns that act as lists but are stored as strings
cur.execute('''SELECT refs, visits, downloads FROM visitors WHERE
counterID = %s and host = %s''', (cID, host) )
data = cur.fetchone()

# unpack data into variables
(ref, visit, download) = data

# retrieve long strings and convert them into lists respectively
refs = ref.split()
visits = visit.split()
downloads = download.split()

# add current strings to each list respectively
refs.append( ref )
visits.append( visit )
downloads.append( download )

# convert lists back to longstrings
refs = ', '.join( refs )
visits = ', '.join( visits )
downloads = ', '.join( downloads )

# save this visit as an entry into database
cur.execute('''INSERT INTO visitors (counterID, refs, host, city,
useros, browser, visits, hits = hits + 1, downloads) VALUES (%s, %s, %s,
%s, %s, %s, %s, %s, %s)''',
(cID, refs, host, city, useros, browser, visits, hits, downloads) )
================================


[Tue Nov 05 11:55:21 2013] [error] [client 176.92.96.218] File
"/home/nikos/public_html/cgi-bin/metrites.py", line 268, in <module>
[Tue Nov 05 11:55:21 2013] [error] [client 176.92.96.218] (ref,
visit, download) = data
[Tue Nov 05 11:55:21 2013] [error] [client 176.92.96.218] TypeError:
'NoneType' object is not iterable


Now i have the parenthesis around fetchone().
How the data cant be properly unpacked?
 
A

Antoon Pardon

Op 05-11-13 10:10, M.F. schreef:
data = cur.fetchall()
That is what the stack trace and Christ tried to inform you.

And now you have depraved Nikos of the opportunity to really learn
something. I'm willing to bet that Nikos will encouter a similar
problem within a year. And because he didn't need to learn how
to find the bug now, he will have no idea how to track the bug
then and will come here again with the expectation that someone
here will just spoon feed him the answer he needs.

And so continues the endless Nikos cycle.
 
A

Antoon Pardon

Op 05-11-13 10:56, Nick the Gr33k schreef:
Στις 5/11/2013 11:34 πμ, ο/η Nick the Gr33k έγÏαψε:
Στις 5/11/2013 11:10 πμ, ο/η M.F. έγÏαψε:

I see, but because of the traceback not being to express it more easily
i was under the impression that data wasn't what i expected it to be.


Still similar error here:

=================================
# if first time for webpage; create new record( primary key is
automatic, hit is defaulted ), if page exists then update record
cur.execute('''INSERT INTO counters (url) VALUES (%s) ON
DUPLICATE KEY UPDATE hits = hits + 1''', page )
cID = cur.lastrowid

# fetch those columns that act as lists but are stored as strings
cur.execute('''SELECT refs, visits, downloads FROM visitors
WHERE counterID = %s and host = %s''', (cID, host) )
data = cur.fetchone()

# unpack data into variables
(ref, visit, download) = data

# retrieve long strings and convert them into lists respectively
refs = ref.split()
visits = visit.split()
downloads = download.split()

# add current strings to each list respectively
refs.append( ref )
visits.append( visit )
downloads.append( download )

# convert lists back to longstrings
refs = ', '.join( refs )
visits = ', '.join( visits )
downloads = ', '.join( downloads )

# save this visit as an entry into database
cur.execute('''INSERT INTO visitors (counterID, refs, host,
city, useros, browser, visits, hits = hits + 1, downloads) VALUES (%s,
%s, %s, %s, %s, %s, %s, %s, %s)''',
(cID, refs, host, city, useros, browser, visits,
hits, downloads) )
================================


[Tue Nov 05 11:55:21 2013] [error] [client 176.92.96.218] File
"/home/nikos/public_html/cgi-bin/metrites.py", line 268, in <module>
[Tue Nov 05 11:55:21 2013] [error] [client 176.92.96.218] (ref,
visit, download) = data
[Tue Nov 05 11:55:21 2013] [error] [client 176.92.96.218] TypeError:
'NoneType' object is not iterable


Now i have the parenthesis around fetchone().
How the data cant be properly unpacked?
Did you read the documentation of fetchone?
 
N

Nick the Gr33k

Στις 5/11/2013 12:20 μμ, ο/η Antoon Pardon έγÏαψε:
Did you read the documentation of fetchone?



fetchone is like fetchall except from the fact that the former returned
a row of data while the latter returned a list of rows of data.

I dont know why it copmains about:
TypeError: 'NoneType' object is not iterable

what object is supposed to have benn of None type?
how do i check for it?
 
A

Antoon Pardon

Op 05-11-13 11:33, Nick the Gr33k schreef:
Στις 5/11/2013 12:20 μμ, ο/η Antoon Pardon έγÏαψε:




fetchone is like fetchall except from the fact that the former returned
a row of data while the latter returned a list of rows of data.
From this answer it seems you didn't read the documentation very carefully.
I dont know why it copmains about:
TypeError: 'NoneType' object is not iterable

what object is supposed to have benn of None type?
how do i check for it?

This is realy rather basic python knowledge. But maybe things
become clear after you read the documentation of fetchone more
carefully.
 
D

Dave Angel

I see, but because of the traceback not being to express it more easily
i was under the impression that data wasn't what i expected it to
be.

Exactly. So why didn't you act on that impression?

Your error message told you that data was a method, and that a method
is not iterable. The previous line is where you assigned it.

Try debugging this:

infile = open("myfile.txt")
data = infile.readlines
for line in data:
print(line)

Not on a server. On your own machine.
 
D

Dave Angel

Στις 5/11/2013 12:20 μμ, ο/η Antoon Pardon έγÏαψε:



fetchone is like fetchall except from the fact that the former returned
a row of data while the latter returned a list of rows of data.

That's not the only difference. See the word None there in one of the
descriptions?
TypeError: 'NoneType' object is not iterable

Examine the statement it's complaining about. It's obvious which item
it's trying to iterate over.
 
S

Steven D'Aprano

Στις 5/11/2013 12:20 μμ, ο/η Antoon Pardon έγÏαψε:




fetchone is like fetchall except from the fact that the former returned
a row of data while the latter returned a list of rows of data.

Read the documentation of fetchone again:

http://docs.python.org/3/library/sqlite3.html#sqlite3.Cursor.fetchone

Take careful note of what it does when there is no more data to fetch.


I dont know why it copmains about:
TypeError: 'NoneType' object is not iterable

what object is supposed to have benn of None type? how do i check for
it?

Does the name "NoneType" give you a hint? Repeat after me:

"The type of **** is NoneType."

Take a guess what the **** should be. Then test it, in the interactive
interpreter:

type(****) # replace the stars with the object


and see what is printed.
 
N

Nick the Gr33k

Στις 5/11/2013 1:49 μμ, ο/η Steven D'Aprano έγÏαψε:
Read the documentation of fetchone again:

http://docs.python.org/3/library/sqlite3.html#sqlite3.Cursor.fetchone

Take careful note of what it does when there is no more data to fetch.




Does the name "NoneType" give you a hint? Repeat after me:

"The type of **** is NoneType."

Take a guess what the **** should be. Then test it, in the interactive
interpreter:

type(****) # replace the stars with the object


and see what is printed.


# fetch those columns that act as lists but are stored as strings
cur.execute('''SELECT refs, visits, downloads FROM visitors WHERE
counterID = %s and host = %s''', (cID, host) )
data = cur.fetchone()

print( type(data) )
sys.exit(0)

i tried inserting a type function to notify me of the datatype of 'data'
but that didnt help too.

Still:
[Tue Nov 05 14:22:32 2013] [error] [client 176.92.96.218] File
"/home/nikos/public_html/cgi-bin/metrites.py", line 268, in <module>
[Tue Nov 05 14:22:32 2013] [error] [client 176.92.96.218] (ref,
visit, download) = data
[Tue Nov 05 14:22:32 2013] [error] [client 176.92.96.218] TypeError:
'NoneType' object is not iterable

Unfortunately i still miss your point.
 
A

Antoon Pardon

Op 05-11-13 13:25, Nick the Gr33k schreef:
# fetch those columns that act as lists but are stored as strings
cur.execute('''SELECT refs, visits, downloads FROM visitors WHERE
counterID = %s and host = %s''', (cID, host) )
data = cur.fetchone()

print( type(data) )
sys.exit(0)

i tried inserting a type function to notify me of the datatype of 'data'
but that didnt help too.

Still:
[Tue Nov 05 14:22:32 2013] [error] [client 176.92.96.218] File
"/home/nikos/public_html/cgi-bin/metrites.py", line 268, in <module>
[Tue Nov 05 14:22:32 2013] [error] [client 176.92.96.218] (ref,
visit, download) = data
[Tue Nov 05 14:22:32 2013] [error] [client 176.92.96.218] TypeError:
'NoneType' object is not iterable

Unfortunately i still miss your point.

*Read* the documentation of fetchone! The information you need is there.
 
D

Dave Angel

i tried inserting a type function to notify me of the datatype of 'data'
but that didnt help too.

What did that print show ? In what way didn't it help?

It said the type was Charles
It didn'tprint anything
It gave some other error
It formattedmy drive

How far did you get on the exercise I suggested?
 
N

Nick the Gr33k

Στις 5/11/2013 3:15 μμ, ο/η Dave Angel έγÏαψε:
What did that print show ? In what way didn't it help?

It said the type was Charles
It didn'tprint anything
It gave some other error
It formattedmy drive

How far did you get on the exercise I suggested?



(e-mail address removed) [~]# who
nikos pts/0 Nov 5 09:57 (176.92.96.218)
(e-mail address removed) [~]# who > myfile.txt
(e-mail address removed) [~]# cat myfile.txt
nikos pts/0 Nov 5 09:57 (176.92.96.218)
(e-mail address removed) [~]# python
Python 3.3.2 (default, Aug 26 2013, 06:41:42)
[GCC 4.4.7 20120313 (Red Hat 4.4.7-3)] on linux
Type "help", "copyright", "credits" or "license" for more information..... print( line )
File "<stdin>", line 2
print( line )
^
IndentationError: expected an indented block.... print( line )
....
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'builtin_function_or_method' object is not iterable

You said that i should try it locally but on my 8.1 i do not have python
installed i test and run everything on server.

So, i tried it at the interpreter and it gave me the same error.
 
J

Joel Goldstick

As I read this thread, the original question was how to stuff
multiple values in a single sql column. Several people pointed out
that the proper way to handle multiple values related to the original
table is to use a second table or perhaps a many to many relationship
with and intermediate join table. Some people, including the original
poster seem to want to shoe horn the data into a single field. --
blobs, strings, even python lists, etc. Although there is nothing
stopping a coder from doing that (except lack of skill ;)), it is a
bad idea. SQL, whether a complete implementation of Codd stuff, or a
reasonable facsimile is a system that makes it possible to make
complex queries on data stored properly. If you break the normal
forms, you always end up not being able to use sql statements to get
everything out that you put in.

So, some people here seem to think that's ok. It may get the poster
past his first step more quickly than taking the time to understand
the tools he is using. But eventually, there will be a big cost to
extending the code. And we will endure another thread like this.
Personally, I think it demeans the group.

Once again we get the pattern:

How do I do this?

1. Here's how

No, I don't like that way

2. Here's a bad way

Oh good, will you write it for me.
 
T

Tim Chase

You're assigning it to the bound function rather than calling the
function. Use the "call" operator:

data = infile.readlines()

-tkc
 
N

Nick the Gr33k

Στις 5/11/2013 5:45 μμ, ο/η Tim Chase έγÏαψε:
You're assigning it to the bound function rather than calling the
function. Use the "call" operator:

data = infile.readlines()

-tkc



--
.... print( line )
....
nikos pts/0 Nov 5 09:57 (176.92.96.218)

Thanks Tim.
So i see here 2 lines, first being the file contenat's themselves and
the last one being an empty line.

I can't relate this to the NoneType error iam having in my script.
 
N

Nick the Gr33k

Στις 5/11/2013 1:16 μμ, ο/η Dave Angel έγÏαψε:
That's not the only difference. See the word None there in one of the
descriptions?


Examine the statement it's complaining about. It's obvious which item
it's trying to iterate over.


--

So perhaps 'data' coudlnt retrive any values from the database that why
it cannot unpack them to the 3 variables?

if so i altered the code to:

# fetch those columns that act as lists but are stored as strings
cur.execute('''SELECT refs, visits, downloads FROM visitors WHERE
counterID = %s and host = %s''', (cID, host) )
data = cur.fetchone()

ref = visit = download = []
if cur.rowcount:
# unpack data into variables
(ref, visit, download) = data

# retrieve long strings and convert them into lists respectively
refs = ref.split()
visits = visit.split()
downloads = download.split()
else:
# initiate these values
ref = ref
visit = lastvisit
download = ''

# add current strings to each list respectively
refs.append( ref )
visits.append( visit )
downloads.append( download )

# convert lists back to longstrings
refs = ', '.join( refs )
visits = ', '.join( visits )
downloads = ', '.join( downloads )

# save this visit as an entry into database
cur.execute('''INSERT INTO visitors (counterID, refs, host, city,
useros, browser, visits, hits = hits + 1, downloads) VALUES (%s, %s, %s,
%s, %s, %s, %s, %s, %s)''',
(cID, refs, host, city, useros, browser, visits, hits, downloads) )
=======================

but this also fail to run :( :( :(
 
D

Denis McMahon

I see, but because of the traceback not being to express it more easily
i was under the impression that data wasn't what i expected it to be.

data wasn't what you expected it to be.

The problem was that you didn't understand that the reason data wasn't
what you expected it to be was that you had assigned data to be the
fetchall method of the object cur, when what you wanted to do was assign
data to be the results of executing the method fetchall on the object cur.

Both of these are legal assignments:

data = cur.fetchall
data = cur.fetchall()

However the following is only valid if data is iterable:

for row in data:

So when it reaches the the line:

for row in data:

and discovers that data is not an iterable type, it gives an error
message. If you can't decipher the error message to get back to the fact
that in this case data isn't a y that you can use "for x in y:" to
iterate over, and then debug intelligently to determine how any why that
error message occurred, then as has been suggested many times in the
past, you should stop trying to write code.

Seriously, how many other people do you see repeatedly posting "I don't
understand the error, help" messages here that have been caused by such
simple coding mistakes?

Most of us can decipher these error messages ourselves and would be
embarrassed to post asking for help.
 
D

Dave Angel

You're assigning it to the bound function rather than calling the
function. Use the "call" operator:
data = infile.readlines()

Thanks for spoiling the lesson. Nicks needs to learn how to debug 4
line programs without someone giving him the answer.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,599
Members
45,175
Latest member
Vinay Kumar_ Nevatia
Top