Στις 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.
--
https://mail.python.org/mailman/listinfo/python-list

Reply via email to