Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #58323 > unrolled thread
| Started by | Nick the Gr33k <nikos.gr33k@gmail.com> |
|---|---|
| First post | 2013-11-02 13:58 +0200 |
| Last post | 2013-11-05 09:39 +0100 |
| Articles | 20 on this page of 46 — 17 participants |
Back to article view | Back to comp.lang.python
How to add a current string into an already existing list Nick the Gr33k <nikos.gr33k@gmail.com> - 2013-11-02 13:58 +0200
Re: How to add a current string into an already existing list Andreas Perstinger <andipersti@gmail.com> - 2013-11-02 14:03 +0100
Re: How to add a current string into an already existing list Nick the Gr33k <nikos.gr33k@gmail.com> - 2013-11-02 18:25 +0200
Re: How to add a current string into an already existing list Denis McMahon <denismfmcmahon@gmail.com> - 2013-11-02 16:43 +0000
Re: How to add a current string into an already existing list rusi <rustompmody@gmail.com> - 2013-11-02 10:40 -0700
Re: How to add a current string into an already existing list Mark Lawrence <breamoreboy@yahoo.co.uk> - 2013-11-02 18:28 +0000
Re: How to add a current string into an already existing list Jussi Piitulainen <jpiitula@ling.helsinki.fi> - 2013-11-02 20:30 +0200
Re: How to add a current string into an already existing list Chris Angelico <rosuav@gmail.com> - 2013-11-03 08:50 +1100
Re: How to add a current string into an already existing list Roy Smith <roy@panix.com> - 2013-11-02 18:00 -0400
Re: How to add a current string into an already existing list Chris Angelico <rosuav@gmail.com> - 2013-11-03 09:13 +1100
Re: How to add a current string into an already existing list Walter Hurry <walterhurry@lavabit.com> - 2013-11-02 21:12 +0000
Re: How to add a current string into an already existing list Gregory Ewing <greg.ewing@canterbury.ac.nz> - 2013-11-03 19:06 +1300
Re: How to add a current string into an already existing list Antoon Pardon <antoon.pardon@rece.vub.ac.be> - 2013-11-03 10:56 +0100
Re: How to add a current string into an already existing list Roy Smith <roy@panix.com> - 2013-11-03 07:16 -0500
Re: How to add a current string into an already existing list Chris Angelico <rosuav@gmail.com> - 2013-11-04 00:13 +1100
Re: How to add a current string into an already existing list Nick the Gr33k <nikos.gr33k@gmail.com> - 2013-11-04 19:03 +0200
Re: How to add a current string into an already existing list Denis McMahon <denismfmcmahon@gmail.com> - 2013-11-04 22:46 +0000
Re: How to add a current string into an already existing list Nick the Gr33k <nikos.gr33k@gmail.com> - 2013-11-05 08:54 +0200
Re: How to add a current string into an already existing list Nick the Gr33k <nikos.gr33k@gmail.com> - 2013-11-05 10:07 +0200
Re: How to add a current string into an already existing list Chris Angelico <rosuav@gmail.com> - 2013-11-05 19:21 +1100
Re: How to add a current string into an already existing list Nick the Gr33k <nikos.gr33k@gmail.com> - 2013-11-05 10:53 +0200
Re: How to add a current string into an already existing list Nick the Gr33k <nikos.gr33k@gmail.com> - 2013-11-05 10:54 +0200
Re: How to add a current string into an already existing list "M.F." <morefool@gmail.com> - 2013-11-05 17:10 +0800
Re: How to add a current string into an already existing list Nick the Gr33k <nikos.gr33k@gmail.com> - 2013-11-05 11:34 +0200
Re: How to add a current string into an already existing list Nick the Gr33k <nikos.gr33k@gmail.com> - 2013-11-05 11:56 +0200
Re: How to add a current string into an already existing list Antoon Pardon <antoon.pardon@rece.vub.ac.be> - 2013-11-05 11:20 +0100
Re: How to add a current string into an already existing list Nick the Gr33k <nikos.gr33k@gmail.com> - 2013-11-05 12:33 +0200
Re: How to add a current string into an already existing list Antoon Pardon <antoon.pardon@rece.vub.ac.be> - 2013-11-05 11:59 +0100
Re: How to add a current string into an already existing list Dave Angel <davea@davea.name> - 2013-11-05 05:16 -0600
Re: How to add a current string into an already existing list Nick the Gr33k <nikos.gr33k@gmail.com> - 2013-11-05 18:20 +0200
Re: How to add a current string into an already existing list Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-11-05 11:49 +0000
Re: How to add a current string into an already existing list Nick the Gr33k <nikos.gr33k@gmail.com> - 2013-11-05 14:25 +0200
Re: How to add a current string into an already existing list Antoon Pardon <antoon.pardon@rece.vub.ac.be> - 2013-11-05 13:42 +0100
Re: How to add a current string into an already existing list Dave Angel <davea@davea.name> - 2013-11-05 07:15 -0600
Re: How to add a current string into an already existing list Nick the Gr33k <nikos.gr33k@gmail.com> - 2013-11-05 17:39 +0200
Re: How to add a current string into an already existing list Tim Chase <python.list@tim.thechases.com> - 2013-11-05 09:45 -0600
Re: How to add a current string into an already existing list Nick the Gr33k <nikos.gr33k@gmail.com> - 2013-11-05 17:50 +0200
Re: How to add a current string into an already existing list Dave Angel <davea@davea.name> - 2013-11-05 23:36 -0600
Re: How to add a current string into an already existing list Mark Lawrence <breamoreboy@yahoo.co.uk> - 2013-11-06 08:53 +0000
Re: How to add a current string into an already existing list Joel Goldstick <joel.goldstick@gmail.com> - 2013-11-05 10:44 -0500
Re: How to add a current string into an already existing list Dave Angel <davea@davea.name> - 2013-11-05 05:00 -0600
Re: How to add a current string into an already existing list Denis McMahon <denismfmcmahon@gmail.com> - 2013-11-05 17:48 +0000
Re: How to add a current string into an already existing list Antoon Pardon <antoon.pardon@rece.vub.ac.be> - 2013-11-05 11:07 +0100
Re: How to add a current string into an already existing list Larry Hudson <orgnut@yahoo.com> - 2013-11-05 20:52 -0800
Re: How to add a current string into an already existing list Chris Angelico <rosuav@gmail.com> - 2013-11-05 21:01 +1100
Re: How to add a current string into an already existing list Antoon Pardon <antoon.pardon@rece.vub.ac.be> - 2013-11-05 09:39 +0100
Page 1 of 3 [1] 2 3 Next page →
| From | Nick the Gr33k <nikos.gr33k@gmail.com> |
|---|---|
| Date | 2013-11-02 13:58 +0200 |
| Subject | How to add a current string into an already existing list |
| Message-ID | <l52pdo$k0f$1@dont-email.me> |
Trying to add the current filename into the existent 'downloads' column
Somehow i don't think i just use the plus sign into an existing column.
We don't try to add numbers here but add an extra string to an already
existing array of strings(list).
======================================================
# update specific torrent's download counter
cur.execute('''UPDATE files SET hits = hits + 1, host = %s, city = %s,
lastvisit = %s WHERE torrent = %s''', (host, city, lastvisit, filename) )
# update specific visitor's download record
cur.execute('''UPDATE visitors SET downloads = downloads + %s WHERE host
= %s''', (filename, host) )
======================================================
Retrieval time for displaying purposes:
======================================================
downloads = []
if cur.rowcount:
for torrent in data:
downloads = ', '.join( torrent )
else:
downloads = 'Κανένα κατέβασμα ταινίας'
# add this visitor entry into database (visits is unique)
cur.execute('''INSERT INTO visitors (counterID, refs, host, city,
useros, browser, visits, downloads) VALUES (%s, %s, %s, %s, %s, %s, %s,
%s)''', (cID, refs, host, city, useros, browser, visits, downloads) )
======================================================
Is this correct, personally i would just prefer:
for torrent in data:
downloads.append( torrent )
Can you tell me the differenced on these two ways?
Aren't the result of both of them a list?
[toc] | [next] | [standalone]
| From | Andreas Perstinger <andipersti@gmail.com> |
|---|---|
| Date | 2013-11-02 14:03 +0100 |
| Message-ID | <mailman.1951.1383398828.18130.python-list@python.org> |
| In reply to | #58323 |
On 02.11.2013 12:58, Nick the Gr33k wrote:
> Trying to add the current filename into the existent 'downloads' column
> Somehow i don't think i just use the plus sign into an existing column.
> We don't try to add numbers here but add an extra string to an already
> existing array of strings(list).
[SNIP]
> # update specific visitor's download record
> cur.execute('''UPDATE visitors SET downloads = downloads + %s WHERE host
> = %s''', (filename, host) )
> ======================================================
Well, when do you understand that your MySQL problems have nothing to do
with Python?
Everything inside the triple quotes is MySQL specific, so it's a MySQL
problem whether you can use + to "add an extra string to an already
existing array of strings(list)".
This list is not a MySQL support forum.
Bye, Andreas
[toc] | [prev] | [next] | [standalone]
| From | Nick the Gr33k <nikos.gr33k@gmail.com> |
|---|---|
| Date | 2013-11-02 18:25 +0200 |
| Message-ID | <l5391q$9in$1@dont-email.me> |
| In reply to | #58326 |
Στις 2/11/2013 3:03 μμ, ο/η Andreas Perstinger έγραψε:
> On 02.11.2013 12:58, Nick the Gr33k wrote:
>> Trying to add the current filename into the existent 'downloads' column
>> Somehow i don't think i just use the plus sign into an existing column.
>> We don't try to add numbers here but add an extra string to an already
>> existing array of strings(list).
>
> [SNIP]
>
>> # update specific visitor's download record
>> cur.execute('''UPDATE visitors SET downloads = downloads + %s WHERE host
>> = %s''', (filename, host) )
>> ======================================================
>
> Well, when do you understand that your MySQL problems have nothing to do
> with Python?
>
> Everything inside the triple quotes is MySQL specific, so it's a MySQL
> problem whether you can use + to "add an extra string to an already
> existing array of strings(list)".
>
> This list is not a MySQL support forum.
>
> Bye, Andreas
[code]
# find out if visitor had downloaded torrents in the past
cur.execute('''SELECT torrent FROM files WHERE host = %s''', host )
data = cur.fetchall()
downloads = []
if data:
for torrent in data:
downloads.append( torrent )
else:
downloads = 'None Yet'
# add this visitor entry into database (host && downloads are unique)
cur.execute('''INSERT INTO visitors (counterID, refs, host, city,
useros, browser, visits, downloads) VALUES (%s, %s, %s, %s, %s, %s, %s,
%s)''', (cID, refs, host, city, useros, browser, visits, downloads) )
[/code]
This works bit questios thas arises is what is its difference compare to:
downloads.append( torrent )
Are both these statements create a list?
But in the latter we get the famous:
pymysql.err.InternalError: (1241, 'Operand should contain 1 column(s)')
while in the join() we arent getting this.
I just want a mysql column type that can be eligible to store an array
of elements, a list that is, no need for having a seperate extra table
for that if we can have a column that can store a list of values.
--
What is now proved was at first only imagined! & WebHost
<http://superhost.gr>
[toc] | [prev] | [next] | [standalone]
| From | Denis McMahon <denismfmcmahon@gmail.com> |
|---|---|
| Date | 2013-11-02 16:43 +0000 |
| Message-ID | <l53a2q$7bf$2@dont-email.me> |
| In reply to | #58329 |
On Sat, 02 Nov 2013 18:25:31 +0200, Nick the Gr33k wrote: > I just want a mysql column type that can be eligible to store an array > of elements, a list that is, no need for having a seperate extra table > for that if we can have a column that can store a list of values. You'd better take that up with the mysql designers. -- Denis McMahon, denismfmcmahon@gmail.com
[toc] | [prev] | [next] | [standalone]
| From | rusi <rustompmody@gmail.com> |
|---|---|
| Date | 2013-11-02 10:40 -0700 |
| Message-ID | <c763566e-b735-4bfe-a6a9-8debfa8deee2@googlegroups.com> |
| In reply to | #58331 |
On Saturday, November 2, 2013 10:13:06 PM UTC+5:30, Denis McMahon wrote: > On Sat, 02 Nov 2013 18:25:31 +0200, Nick the Gr33k wrote: > > I just want a mysql column type that can be eligible to store an array > > of elements, a list that is, no need for having a seperate extra table > > for that if we can have a column that can store a list of values. > You'd better take that up with the mysql designers. That Codd... Should have studied some computer science [Ive a vague feeling I am repeating myself...]
[toc] | [prev] | [next] | [standalone]
| From | Mark Lawrence <breamoreboy@yahoo.co.uk> |
|---|---|
| Date | 2013-11-02 18:28 +0000 |
| Message-ID | <mailman.1956.1383416942.18130.python-list@python.org> |
| In reply to | #58337 |
On 02/11/2013 17:40, rusi wrote: > On Saturday, November 2, 2013 10:13:06 PM UTC+5:30, Denis McMahon wrote: >> On Sat, 02 Nov 2013 18:25:31 +0200, Nick the Gr33k wrote: > >>> I just want a mysql column type that can be eligible to store an array >>> of elements, a list that is, no need for having a seperate extra table >>> for that if we can have a column that can store a list of values. > >> You'd better take that up with the mysql designers. > > That Codd... > Should have studied some computer science > > [Ive a vague feeling I am repeating myself...] > A local lad as well, he's brought nothing but disgrace on the area. Apart from the fact that the vast majority of locals have never ever heard of him. -- Python is the second best programming language in the world. But the best has yet to be invented. Christian Tismer Mark Lawrence
[toc] | [prev] | [next] | [standalone]
| From | Jussi Piitulainen <jpiitula@ling.helsinki.fi> |
|---|---|
| Date | 2013-11-02 20:30 +0200 |
| Message-ID | <qotzjpmu04w.fsf@ruuvi.it.helsinki.fi> |
| In reply to | #58337 |
rusi writes: > On Saturday, November 2, 2013 10:13:06 PM UTC+5:30, Denis McMahon wrote: > > On Sat, 02 Nov 2013 18:25:31 +0200, Nick the Gr33k wrote: > > > > I just want a mysql column type that can be eligible to store an > > > array of elements, a list that is, no need for having a seperate > > > extra table for that if we can have a column that can store a > > > list of values. > > > You'd better take that up with the mysql designers. > > That Codd... > Should have studied some computer science > > [Ive a vague feeling I am repeating myself...] Date and Darwen think that SQL designers should have studied and implemented relational theory (Codd's database theory). This doesn't contradict you, of course - possibly Codd should have studied CS. Date and Darwen think also that structured values like sets and relations should be allowed in a database and supported by the query language. Maybe their work should be taken as what Codd might think today, while the implementors of SQL products go their own merry ways. I grepped through one SQL standard on the web once to see how it refers to relational theory. Every single occurrence of "relation" was a substring of "correlational value" or something like that. My take from that is that SQL doesn't even pretend to be an implementation of Codd's theory. Suppose a database allowed structured values like lists of strings, lists of numbers, or even lists of such lists and more. Then it would actually be a Python issue how best to support that database.
[toc] | [prev] | [next] | [standalone]
| From | Chris Angelico <rosuav@gmail.com> |
|---|---|
| Date | 2013-11-03 08:50 +1100 |
| Message-ID | <mailman.1959.1383429046.18130.python-list@python.org> |
| In reply to | #58341 |
On Sun, Nov 3, 2013 at 5:30 AM, Jussi Piitulainen <jpiitula@ling.helsinki.fi> wrote: > Suppose a database allowed structured values like lists of strings, > lists of numbers, or even lists of such lists and more. Then it would > actually be a Python issue how best to support that database. PostgreSQL supports some higher-level structures like arrays. Personally, though, I think the most general representation of a Python list in a database is either a varchar field with some form of structure (eg the repr of a list), or a separate table with a foreign key back to this one. ChrisA
[toc] | [prev] | [next] | [standalone]
| From | Roy Smith <roy@panix.com> |
|---|---|
| Date | 2013-11-02 18:00 -0400 |
| Message-ID | <roy-103C2B.18003002112013@news.panix.com> |
| In reply to | #58350 |
In article <mailman.1959.1383429046.18130.python-list@python.org>, Chris Angelico <rosuav@gmail.com> wrote: > On Sun, Nov 3, 2013 at 5:30 AM, Jussi Piitulainen > <jpiitula@ling.helsinki.fi> wrote: > > Suppose a database allowed structured values like lists of strings, > > lists of numbers, or even lists of such lists and more. Then it would > > actually be a Python issue how best to support that database. > > PostgreSQL supports some higher-level structures like arrays. > Personally, though, I think the most general representation of a > Python list in a database is either a varchar field with some form of > structure (eg the repr of a list), or a separate table with a foreign > key back to this one. When you say "database" here, you're really talking about relational databases. There are other kinds. In MongoDB, for example, storing a list of lists of strings is a perfectly reasonable and straight-forward thing to do. Anything which can be represented by bson (which is more or less the same as anything which can be represented by json) can be inserted directly.
[toc] | [prev] | [next] | [standalone]
| From | Chris Angelico <rosuav@gmail.com> |
|---|---|
| Date | 2013-11-03 09:13 +1100 |
| Message-ID | <mailman.1961.1383430391.18130.python-list@python.org> |
| In reply to | #58351 |
On Sun, Nov 3, 2013 at 9:00 AM, Roy Smith <roy@panix.com> wrote: > In article <mailman.1959.1383429046.18130.python-list@python.org>, > Chris Angelico <rosuav@gmail.com> wrote: > >> On Sun, Nov 3, 2013 at 5:30 AM, Jussi Piitulainen >> <jpiitula@ling.helsinki.fi> wrote: >> > Suppose a database allowed structured values like lists of strings, >> > lists of numbers, or even lists of such lists and more. Then it would >> > actually be a Python issue how best to support that database. >> >> PostgreSQL supports some higher-level structures like arrays. >> Personally, though, I think the most general representation of a >> Python list in a database is either a varchar field with some form of >> structure (eg the repr of a list), or a separate table with a foreign >> key back to this one. > > When you say "database" here, you're really talking about relational > databases. There are other kinds. Quite right, my apologies. I'm talking about the classic relational database accessed by SQL, which is what the OP's currently working with (MySQL). I've worked with other types of database, but was trying to stick as closely as possible to the question. But yes, the clarification is important here. ChrisA
[toc] | [prev] | [next] | [standalone]
| From | Walter Hurry <walterhurry@lavabit.com> |
|---|---|
| Date | 2013-11-02 21:12 +0000 |
| Message-ID | <l53pr6$vc4$1@news.albasani.net> |
| In reply to | #58337 |
On Sat, 02 Nov 2013 10:40:58 -0700, rusi wrote: > That Codd... > Should have studied some computer science > > [Ive a vague feeling I am repeating myself...] ROFL. Get thee into FNF!
[toc] | [prev] | [next] | [standalone]
| From | Gregory Ewing <greg.ewing@canterbury.ac.nz> |
|---|---|
| Date | 2013-11-03 19:06 +1300 |
| Message-ID | <bdm7fiF28rrU1@mid.individual.net> |
| In reply to | #58329 |
Nick the Gr33k wrote: > I just want a mysql column type that can be eligible to store an array > of elements, a list that is, no need for having a seperate extra table > for that if we can have a column that can store a list of values. Relational database systems typically don't provide any such type, because it's not the recommended way of storing that kind of data in a relational database. The recommended way is to use a secondary table, as has been pointed out. You're making things difficult for yourself by refusing to consider that solution. -- Greg
[toc] | [prev] | [next] | [standalone]
| From | Antoon Pardon <antoon.pardon@rece.vub.ac.be> |
|---|---|
| Date | 2013-11-03 10:56 +0100 |
| Message-ID | <mailman.1974.1383472582.18130.python-list@python.org> |
| In reply to | #58369 |
Op 03-11-13 07:06, Gregory Ewing schreef: > Nick the Gr33k wrote: >> I just want a mysql column type that can be eligible to store an array of elements, a list that is, no need for having a seperate extra table for that if we can have a column that can store a list of values. > > Relational database systems typically don't provide any > such type, because it's not the recommended way of storing > that kind of data in a relational database. > > The recommended way is to use a secondary table, as has > been pointed out. > > You're making things difficult for yourself by refusing > to consider that solution. > You are talking to Nikos! The person who choose code because he prefers how it looks over examples of working code. -- Antoon Pardon
[toc] | [prev] | [next] | [standalone]
| From | Roy Smith <roy@panix.com> |
|---|---|
| Date | 2013-11-03 07:16 -0500 |
| Message-ID | <roy-D613E2.07164103112013@news.panix.com> |
| In reply to | #58369 |
In article <bdm7fiF28rrU1@mid.individual.net>, Gregory Ewing <greg.ewing@canterbury.ac.nz> wrote: > Nick the Gr33k wrote: > > I just want a mysql column type that can be eligible to store an array > > of elements, a list that is, no need for having a seperate extra table > > for that if we can have a column that can store a list of values. > > Relational database systems typically don't provide any > such type, because it's not the recommended way of storing > that kind of data in a relational database. > > The recommended way is to use a secondary table, as has > been pointed out. Most SQL databases allow you to store arbitrary data as an opaque value (i.e. BLOB). So, one possibility would be to just serialize your list (pickle, json, whatever) and store it that way. I've seen databases that didn't use BLOB, but just stored json in a string field. The limitation, of course, is that the data is opaque as far as the database goes; you can't do queries against it. But, if all you need to do is store the list and be able to retrieve it, it's a perfectly reasonable thing to do, and a lot more efficient than doing a join on a secondary table. Normalization is for database weenies :-)
[toc] | [prev] | [next] | [standalone]
| From | Chris Angelico <rosuav@gmail.com> |
|---|---|
| Date | 2013-11-04 00:13 +1100 |
| Message-ID | <mailman.1982.1383484448.18130.python-list@python.org> |
| In reply to | #58392 |
On Sun, Nov 3, 2013 at 11:16 PM, Roy Smith <roy@panix.com> wrote: > The limitation, of course, is that the data is opaque as far as the > database goes; you can't do queries against it. But, if all you need to > do is store the list and be able to retrieve it, it's a perfectly > reasonable thing to do, and a lot more efficient than doing a join on a > secondary table. Yeah, that can be an effective way to store complex data - especially if the nesting level isn't fixed. (Normalization can handle a single-level list, but it's a lot messier for handling lists of lists, for instance.) I still think that the OP's task would be best suited to a separate table (one table of visitors, another of downloads, where the Downloads table has a foreign key to Visitors), but I'm reminded of XKCD 1027: the thing standing in the way of his code is that the person coding it... is him. And of course, this is all without getting into the non-code aspects of this proposal - as have been mentioned several times, like EU regulations on retaining this level of data. ChrisA
[toc] | [prev] | [next] | [standalone]
| From | Nick the Gr33k <nikos.gr33k@gmail.com> |
|---|---|
| Date | 2013-11-04 19:03 +0200 |
| Message-ID | <l58k1t$24a$1@dont-email.me> |
| In reply to | #58392 |
Στις 3/11/2013 2:16 μμ, ο/η Roy Smith έγραψε:
> In article <bdm7fiF28rrU1@mid.individual.net>,
> Gregory Ewing <greg.ewing@canterbury.ac.nz> wrote:
>
>> Nick the Gr33k wrote:
>>> I just want a mysql column type that can be eligible to store an array
>>> of elements, a list that is, no need for having a seperate extra table
>>> for that if we can have a column that can store a list of values.
>>
>> Relational database systems typically don't provide any
>> such type, because it's not the recommended way of storing
>> that kind of data in a relational database.
>>
>> The recommended way is to use a secondary table, as has
>> been pointed out.
>
> Most SQL databases allow you to store arbitrary data as an opaque value
> (i.e. BLOB). So, one possibility would be to just serialize your list
> (pickle, json, whatever) and store it that way. I've seen databases
> that didn't use BLOB, but just stored json in a string field.
>
> The limitation, of course, is that the data is opaque as far as the
> database goes; you can't do queries against it. But, if all you need to
> do is store the list and be able to retrieve it, it's a perfectly
> reasonable thing to do, and a lot more efficient than doing a join on a
> secondary table.
>
> Normalization is for database weenies :-)
>
Exactly my sentiments Roy!
Call me picky but even if they try to hit me hard i wll always get to
pick the simplest and better looking way.
I have managed to make my code work by:
create table visitors
(
counterID integer(5) not null,
host varchar(50) not null,
refs varchar(25) not null,
city varchar(20) not null,
userOS varchar(10) not null,
browser varchar(10) not null,
visits datetime not null,
hits integer(5) not null default 1,
downloads varchar(50) not null default '',
foreign key (counterID) references counters(ID),
unique index (visits)
)ENGINE = MYISAM;
=============================================
# add this visitor entry into database (hits && downloads are defaulted)
cur.execute('''INSERT INTO visitors (counterID, refs, host, city,
useros, browser, visits) VALUES (%s, %s, %s, %s, %s, %s, %s)''', (cID,
ref, host, city, useros, browser, lastvisit) )
=============================================
and later on ....
=============================================
torrents = []
# check if visitor has downloaded movies
for download in downloads:
if download != '':
torrents.append( download )
# present visitor's movie picks if any
if torrents:
print( '<td><select>' )
for n, torrent in enumerate( torrents ):
if n == 0:
op_selected = 'selected'
else:
op_selected = ''
print( '<option %s> %s </option>' % (op_selected, torrent) )
print( '</select></td>' )
else:
print( '<td><center><b><font color=white> Δεν πραγματοποίηθηκαν ακόμη!
</td>' )
break
=============================================
Please since this column you mentioned is able to store a Python's list
datatype could you tell me what needs alternation in:
1. MySQL's visitor's table definition time
2. python database cur.execute method
3. retrieval time
Thank you very much for anyone wishes to give me a hand here.
[toc] | [prev] | [next] | [standalone]
| From | Denis McMahon <denismfmcmahon@gmail.com> |
|---|---|
| Date | 2013-11-04 22:46 +0000 |
| Message-ID | <l59842$roj$1@dont-email.me> |
| In reply to | #58451 |
On Mon, 04 Nov 2013 19:03:58 +0200, Nick the Gr33k wrote: > Please since this column you mentioned is able to store a Python's list > datatype could you tell me what needs alternation in: We've already told you, there is NO mysql datatype that can store a python list directly. There are ways of storing lists in mysql, but you can't pass a list directly into a mysql update or insert query as a single record field (you may be able to pass a list of tuples in for a multi row update or insert, but that's a different issue). 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. Or you could store each element of the list in a "downloads" tables along with a reference to the associated record in the "users" table. These are two possible methods of storing a list that is associated with a user entry. Which method you use is up to you. There may be others. Pick a method and code it. There is no built in support in the python / mysql system for putting a list straight into a database, because mysql does not have a "collection" record type. -- Denis McMahon, denismfmcmahon@gmail.com
[toc] | [prev] | [next] | [standalone]
| From | Nick the Gr33k <nikos.gr33k@gmail.com> |
|---|---|
| Date | 2013-11-05 08:54 +0200 |
| Message-ID | <l5a4no$21s$1@dont-email.me> |
| In reply to | #58465 |
Στις 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.
[toc] | [prev] | [next] | [standalone]
| From | Nick the Gr33k <nikos.gr33k@gmail.com> |
|---|---|
| Date | 2013-11-05 10:07 +0200 |
| Message-ID | <l5a8vu$iaa$1@dont-email.me> |
| In reply to | #58480 |
Στις 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?
[toc] | [prev] | [next] | [standalone]
| From | Chris Angelico <rosuav@gmail.com> |
|---|---|
| Date | 2013-11-05 19:21 +1100 |
| Message-ID | <mailman.2041.1383639718.18130.python-list@python.org> |
| In reply to | #58481 |
On Tue, Nov 5, 2013 at 7:07 PM, Nick the Gr33k <nikos.gr33k@gmail.com> wrote: > How is ti possible for data to be none iterable? Do you know how to call a method in Python? If not, go back to the beginning of the tutorial and start reading. If so, look through your code and see where you have a 'method' object that you are trying to treat as iterable. It's really REALLY obvious, and you even have the line number to tell you. Nick, you *need* to learn how to read Python tracebacks. They are incredibly helpful. Be glad you don't just get "Segmentation fault" and a process termination (or, worse, a security hole). ChrisA
[toc] | [prev] | [next] | [standalone]
Page 1 of 3 [1] 2 3 Next page →
Back to top | Article view | comp.lang.python
csiph-web