How to add a current string into an already existing list
Nick the Gr33k
nikos.gr33k at gmail.com
Tue Nov 5 03:07:28 EST 2013
Στις 5/11/2013 8:54 πμ, ο/η Nick the Gr33k έγραψε:
> Στις 5/11/2013 12:46 πμ, ο/η Denis McMahon έγραψε:
>> On Mon, 04 Nov 2013 19:03:58 +0200, Nick the Gr33k wrote:
>
>> There is no built in support in the python / mysql system for puttinga
>> list straight into a database, because mysql does not have"collection"
>> record type.
>
> Does postgresql has this 'collection' record type
>
>
>> You could convert the python list into a storable entity, for example
>> imploding a list of strings with some arbitrary separator to create a
>> long string, store the long string, then when you read it from the
>> database explode it back into a list.
>> Which method you use is up to you. There may be others.
>> Pick a method and code it.
>
> Okey here is my attempt to code your solution as best as i can get my
> head around it:
>
> This is the part that is responsible to do the database insertion
> converting scalars to lists and backwards.
>
> =====================================
> try:
> # 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''', cID )
> data = cur.fetchone
>
> ref = data[0]
> visit = data[1]
> download = data[2]
>
> # retrieve long strings and convert them into lists respectively
> refs = ref.split()
> visits = visit.split()
> downloads = download.split()
>
> # add current strings to the each list respectively
> refs.appends( ref )
> visits.appends( visit )
> downloads.appends( download )
>
> # convert lists back to longstrings
> refs = ', '.join( refs )
> visits = ', '.join( visits )
> downloads = ', '.join( downloads )
>
> # add this visitor entry into database (hits && downloads are
> defaulted)
> 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) )
>
> con.commit()
> except pymysql.ProgrammingError as e:
> print( repr(e) )
> con.rollback()
> sys.exit(0)
> ===================================
>
> Please tell me if this logic is correct, for some reason it doesn't do
> what i need it to do.
>
> Thank you.
Better version for it, i think, but still none working:
=========================================
# 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 = (SELECT ID FROM counters WHERE url = %s) ORDER BY visits
DESC''', page )
data = cur.fetchall
for row in data:
(refs, visits, downloads) = row
# 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.appends( ref )
visits.appends( visit )
downloads.appends( download )
# convert lists back to longstrings
refs = ', '.join( refs )
visits = ', '.join( visits )
downloads = ', '.join( downloads )
# add this visitor entry into database (hits && downloads are defaulted)
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 10:06:57 2013] [error] [client 176.92.96.218] Traceback
(most recent call last):
[Tue Nov 05 10:06:57 2013] [error] [client 176.92.96.218] File
"/home/nikos/public_html/cgi-bin/metrites.py", line 267, in <module>
[Tue Nov 05 10:06:57 2013] [error] [client 176.92.96.218] for row in
data:
[Tue Nov 05 10:06:57 2013] [error] [client 176.92.96.218] TypeError:
'method' object is not iterable
How is ti possible for data to be none iterable?
More information about the Python-list
mailing list