C Vora | 5 Jan 23:23
Picon

Traversing a table

Hi

I'm somewhat new to Python and SQLObject (but have worked with ORM Wrappers before with Java). I have inherited a project which uses the above extensively. I have a somewhat novice question but couldn't find obvious answers so am posting here.

So the code I have basically does a .select() on a merge table and creates an output file (Comma separated say) with columns I'm interested in . I want to now additionally mark in this file -

a. duplicates that occur in succession only
b. objects that have certain values for a column

So for eg, if row X and row Y are duplicates, I want to have in the file a single row with the letter D at the end to mark Duplicate.

Whats the best way of doing this?

I thought of this pseudo-code:

for x in merge_table.select()
    retrieve next row y    ->> (how??)
    if x==y       //duplicate
       add x to file & mark appropriately in file
    else
       add x and y to file
       reposition cursor correctly to next unread row (if needed, how?)

I was trying to find  a way to do this with SQLObject but couldn't easily figure out. I would like to avoid complicated cursor logic if I can help it.

Any pointers would be appreciated.

TIA,
CV

------------------------------------------------------------------------------
_______________________________________________
sqlobject-discuss mailing list
sqlobject-discuss <at> lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/sqlobject-discuss
Petr Jakeš | 6 Jan 00:10
Picon

Re: Traversing a table

Hi,
you are not telling us about columns. Are you trying to check if all collumns in the row are identical with some other row in the table? If yes, I would try to sort columns in the select. Than something like this can work (untested):

counter=0
lastRow=""
for actualRow in merge_table.select(orderBy = merge_table.q.firstColumn,orderBy = merge_table.q.secondColumn):
    if actualRow is lastRow:
        counter+=1
    else:
        if counter:
            write actual columns + counter+"D" to the file
            counter = 0
    lastRow=actualRow

HTH

Petr Jakeš

On Mon, Jan 5, 2009 at 11:23 PM, C Vora <to.chetanvora <at> gmail.com> wrote:
Hi

I'm somewhat new to Python and SQLObject (but have worked with ORM Wrappers before with Java). I have inherited a project which uses the above extensively. I have a somewhat novice question but couldn't find obvious answers so am posting here.

So the code I have basically does a .select() on a merge table and creates an output file (Comma separated say) with columns I'm interested in . I want to now additionally mark in this file -

a. duplicates that occur in succession only
b. objects that have certain values for a column

So for eg, if row X and row Y are duplicates, I want to have in the file a single row with the letter D at the end to mark Duplicate.

Whats the best way of doing this?

I thought of this pseudo-code:

for x in merge_table.select()
    retrieve next row y    ->> (how??)
    if x==y       //duplicate
       add x to file & mark appropriately in file
    else
       add x and y to file
       reposition cursor correctly to next unread row (if needed, how?)

I was trying to find  a way to do this with SQLObject but couldn't easily figure out. I would like to avoid complicated cursor logic if I can help it.

Any pointers would be appreciated.

TIA,
CV
------------------------------------------------------------------------------

_______________________________________________
sqlobject-discuss mailing list
sqlobject-discuss <at> lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/sqlobject-discuss


------------------------------------------------------------------------------
_______________________________________________
sqlobject-discuss mailing list
sqlobject-discuss <at> lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/sqlobject-discuss
Zoran Bošnjak | 7 Jan 09:40
Picon

table ralations

Hello,
I have 2 simple tables:
- block table (set of blocks)
- connection tables (connections between blocks)

I would like to setup a database to prevent creating connection to/from non-existant blocks.
For example: If I start with empty database, the script below should fail (but instead... the connection
between nonexistant blocks is created).
What's wrong?

If I delete a block, all connection related to this block should be deleted automaticaly. This works fine
with "cascade=True" option.

Thanks,
Zoran

------
#! /usr/bin/env python
# -*- coding: utf-8 -*-

import sys, os
from sqlobject import *

class Block(SQLObject):
    name = StringCol()

class Connection(SQLObject):
    src = ForeignKey('Block', cascade=True)
    dst = ForeignKey('Block', cascade=True)

db_filename = os.path.abspath(sys.argv[1]) print db_filename

# load database
connection_string = 'sqlite:' + db_filename connection = connectionForURI(connection_string)
sqlhub.processConnection = connection

# create tables
Block.createTable(ifNotExists=True)
Connection.createTable(ifNotExists=True)

# create dumb connection
# it should not work, because there is no blocks!!!
Connection(src=150, dst=250)

----

Dump database just created:

sqlite> .dump
BEGIN TRANSACTION;
CREATE TABLE block (
    id INTEGER PRIMARY KEY,
    name TEXT
);
CREATE TABLE connection (
    id INTEGER PRIMARY KEY,
    src_id INT CONSTRAINT src_id_exists REFERENCES block(id) ON DELETE CASCADE,
    dst_id INT CONSTRAINT dst_id_exists REFERENCES block(id) ON DELETE CASCADE ); 
INSERT INTO "connection" VALUES(1,150,250); COMMIT;
sqlite> 

------------------------------------------------------------------------------
Check out the new SourceForge.net Marketplace.
It is the best place to buy or sell services for
just about anything Open Source.
http://p.sf.net/sfu/Xq1LFB
Petr Jakeš | 7 Jan 09:51
Picon

Re: table ralations

# create dumb connection
# it should not work, because there is no blocks!!!
Connection(src=150, dst=250)

Why do you think this should not work? AFIK above mentioned just insert one row (record) in your table Connection. Exactly as the SQL below describes.

INSERT INTO "connection" VALUES(1,150,250); COMMIT;
sqlite>

Petr

------------------------------------------------------------------------------
Check out the new SourceForge.net Marketplace.
It is the best place to buy or sell services for
just about anything Open Source.
http://p.sf.net/sfu/Xq1LFB
_______________________________________________
sqlobject-discuss mailing list
sqlobject-discuss <at> lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/sqlobject-discuss
Zoran Bošnjak | 7 Jan 10:17
Picon

Re: table ralations

Sorry for the confusion...
 
I don't want to have a "Connection" in a database unless the connection is between existing "Blocks".
 
Please correct my class definition for Block and Connection so, that it won't be even possible to create such connection.
 
Zoran
 

From: petr.jakes.tpc <at> gmail.com [mailto:petr.jakes.tpc <at> gmail.com] On Behalf Of Petr Jakeš
Sent: Wednesday, January 07, 2009 9:51 AM
To: Zoran Bošnjak
Cc: sqlobject-discuss <at> lists.sourceforge.net
Subject: Re: [SQLObject] table ralations

# create dumb connection
# it should not work, because there is no blocks!!!
Connection(src=150, dst=250)

Why do you think this should not work? AFIK above mentioned just insert one row (record) in your table Connection. Exactly as the SQL below describes.

INSERT INTO "connection" VALUES(1,150,250); COMMIT;
sqlite>

Petr

------------------------------------------------------------------------------
Check out the new SourceForge.net Marketplace.
It is the best place to buy or sell services for
just about anything Open Source.
http://p.sf.net/sfu/Xq1LFB
_______________________________________________
sqlobject-discuss mailing list
sqlobject-discuss <at> lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/sqlobject-discuss
GeorgeFLeCompte | 7 Jan 18:30
Picon

Please remove me from this mailing list

george <at> lecompte.org
GeorbeLeCompte <at> comcast.net

------------------------------------------------------------------------------
Check out the new SourceForge.net Marketplace.
It is the best place to buy or sell services for
just about anything Open Source.
http://p.sf.net/sfu/Xq1LFB
inhahe | 7 Jan 18:32
Picon

how to get all records in a table?

hi, can somebody tell me how to select or get all records in a table?
i can't seem to find it in the documentation.  thanks

------------------------------------------------------------------------------
Check out the new SourceForge.net Marketplace.
It is the best place to buy or sell services for
just about anything Open Source.
http://p.sf.net/sfu/Xq1LFB
Petr Jakeš | 7 Jan 19:41
Picon

Re: how to get all records in a table?

Can't believe you can not find it. Anyway.... HTH

from sqlobject import *
sqlhub.processConnection = connectionForURI('sqlite:/:memory:')
class Person(SQLObject):
fname = StringCol()
mi = StringCol(length=1, default=None)
lname = StringCol()

Person.createTable()
Person(fname="John", lname="Doe")
Person(fname="Inhahe", lname="Noname")

allRows = Person.select()
HTH.

Petr Jakeš


On Wed, Jan 7, 2009 at 6:32 PM, inhahe <inhahe <at> gmail.com> wrote:
hi, can somebody tell me how to select or get all records in a table?
i can't seem to find it in the documentation.  thanks

------------------------------------------------------------------------------
Check out the new SourceForge.net Marketplace.
It is the best place to buy or sell services for
just about anything Open Source.
http://p.sf.net/sfu/Xq1LFB
_______________________________________________
sqlobject-discuss mailing list
sqlobject-discuss <at> lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/sqlobject-discuss

------------------------------------------------------------------------------
Check out the new SourceForge.net Marketplace.
It is the best place to buy or sell services for
just about anything Open Source.
http://p.sf.net/sfu/Xq1LFB
_______________________________________________
sqlobject-discuss mailing list
sqlobject-discuss <at> lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/sqlobject-discuss
Petr Jakeš | 7 Jan 23:54
Picon

Re: Traversing a table

I don't think such a duplicity (to keep data in the database and some of rows in the text file) is the way to go.

Try to use SQL distinct commands (http://www.sqlobject.org/SelectResults.html#distinct) instead.

It will offer unique rows from the database to your audience.

Petr

------------------------------------------------------------------------------
Check out the new SourceForge.net Marketplace.
It is the best place to buy or sell services for
just about anything Open Source.
http://p.sf.net/sfu/Xq1LFB
_______________________________________________
sqlobject-discuss mailing list
sqlobject-discuss <at> lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/sqlobject-discuss
Petr Jakeš | 8 Jan 01:54
Picon

Re: how to get all records in a table?

On Thu, Jan 8, 2009 at 12:31 AM, inhahe <inhahe <at> gmail.com> wrote:

Oops, I thought I had tried that and that it didn't work (and I didn't
see the documentation explicitly mention calling select with no
parameters), but it just didn't work due to something I had missed
elsewhere.  Thanks.

====== 8< ==========
from sqlobject import *
from sqlobject.sqlbuilder import *
connInMem = connectionForURI('sqlite:/:memory:')

sqlhub.processConnection = connectionForURI('sqlite:/:memory:')
class Person(SQLObject):
    fname = StringCol()
    mi = StringCol(length=1, default=None)
    lname = StringCol()

Person.createTable()
Person(fname="John", lname="Doe")
Person(fname="John", lname="Doe")
Person(fname="Inhahe", lname="Noname")
Person(fname="Inhahe", lname="Noname")

Person._connection.debug = True
returnedValues = connInMem.queryAll("SELECT DISTINCT person.fname, person.mi, person.lname from person")
print returnedValues
====== 8< ==========

Following code generates valid SQL query, but because id column is included, it returns all rows from the database (id is unique for each row).

for row in Person.select().distinct():
    print row

I do not know, if it is possible to force SQLObject not to include person.id column in to the select query, but I guess no (maybe Oleg can help).

Regards

Petr
------------------------------------------------------------------------------
Check out the new SourceForge.net Marketplace.
It is the best place to buy or sell services for
just about anything Open Source.
http://p.sf.net/sfu/Xq1LFB
_______________________________________________
sqlobject-discuss mailing list
sqlobject-discuss <at> lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/sqlobject-discuss

Gmane