bruce | 1 Jan 02:41
Picon
Favicon

sqlobject - _init question....

hi...

more playing around with sqlobject..!

i'm playing (or trying to) around with sqlobject, and trying to create a class/obj, so that everytime the
class is instantiated, a field in the class is set to a given value...

is this possible?

thanks

-------------------------------------------------------------------------
This SF.net email is sponsored by: Microsoft
Defy all challenges. Microsoft(R) Visual Studio 2005.
http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/
Petr Jakeš | 1 Jan 02:48
Picon

Re: sqlobject - _init question....

i'm playing (or trying to) around with sqlobject, and trying to create a class/obj, so that everytime the class is instantiated, a field in the class is set to a given value...

is this possible?

Do you mean something like this?

class Book(SQLObject):
    name=StringCol(length=20, default="Bruce")
    addr=StringCol(length=20, default="Earthlink")
      
Book.createTable(ifNotExists=True)
Book()
print Book.get(1).name


Happy New Year


Petr Jakes
-------------------------------------------------------------------------
This SF.net email is sponsored by: Microsoft
Defy all challenges. Microsoft(R) Visual Studio 2005.
http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/
_______________________________________________
sqlobject-discuss mailing list
sqlobject-discuss <at> lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/sqlobject-discuss
bruce | 1 Jan 03:25
Picon
Favicon

Re: sqlobject - _init question....

hi petr...

something like this...

#!/usr/bin/python

#
# test sqlobject
#
#
from sqlobject import *
import sys, os

print "foo \n"
t1 = 'mysql://lab:lab <at> localhost/jfrank3'
sqlhub.processConnection = connectionForURI(t1)

print "end foo \n"

class foo(SQLObject):
	class sqlmeta:
		table = 'test'
		idName='id'
		idType=str
	aa =StringCol(length=5)
	id2 =StringCol(length=20)

	def _set_id(self):
		d = 'tikdsljg'
		self._SO_set_id(d)

tmp=foo(aa='qqq', id2='ffff')

tmp2=foo.selectBy(aa='qqq')
qq=tmp2[0]
print qq.id, "-- ", qq.aa 

print "mmmmmm \n"

with a test db schema of...
/*
#
# test the sqlobject app
#
*/
drop database if exists jfrank3;
create database jfrank3;
use jfrank3;

DROP TABLE IF EXISTS test;
CREATE TABLE test (
  id varchar(15) NOT NULL default '',
  aa varchar(5) default '',
  id2 varchar (20)  default '',
  PRIMARY KEY  (id)
) TYPE=MyISAM DEFAULT CHARSET=latin1;

DROP TABLE IF EXISTS test2;
CREATE TABLE test2 (
  id varchar(15) NOT NULL default '',
  aa2 varchar(5) default '',
  primary key(id)
) TYPE=MyISAM DEFAULT CHARSET=latin1;

........
this is a continuation of my previous/initial test.

right now, when i run the test script.. i get the following traceback/err from the python interpreter..

[root <at> laptop2 /]# ./sql2.py
foo

end foo

Traceback (most recent call last):
  File "./sql2.py", line 29, in ?
    tmp=foo(aa='qqq', id2='ffff')
  File
"/usr/lib/python2.4/site-packages/SQLObject-0.10dev_r3184-py2.4.egg/sqlobject/declarative.py",
line 89, in _wrapper
    return fn(self, *args, **kwargs)
  File
"/usr/lib/python2.4/site-packages/SQLObject-0.10dev_r3184-py2.4.egg/sqlobject/main.py",
line 1181, in __init__
    self._create(id, **kw)
  File
"/usr/lib/python2.4/site-packages/SQLObject-0.10dev_r3184-py2.4.egg/sqlobject/main.py",
line 1212, in _create
    self._SO_finishCreate(id)
  File
"/usr/lib/python2.4/site-packages/SQLObject-0.10dev_r3184-py2.4.egg/sqlobject/main.py",
line 1239, in _SO_finishCreate
    self._init(id)
  File
"/usr/lib/python2.4/site-packages/SQLObject-0.10dev_r3184-py2.4.egg/sqlobject/main.py",
line 904, in _init
    self.id = id
TypeError: _set_id() takes exactly 1 argument (2 given)
[root <at> laptop2 /]#

-----Original Message-----
From: petr.jakes.tpc <at> gmail.com [mailto:petr.jakes.tpc <at> gmail.com]On Behalf Of Petr Jakeš
Sent: Monday, December 31, 2007 5:49 PM
To: bruce
Cc: sqlobject-discuss <at> lists.sourceforge.net
Subject: Re: [SQLObject] sqlobject - _init question....

i'm playing (or trying to) around with sqlobject, and trying to create a class/obj, so that everytime the
class is instantiated, a field in the class is set to a given value... 

is this possible?

Do you mean something like this?

class Book(SQLObject):
    name=StringCol(length=20, default="Bruce")
    addr=StringCol(length=20, default="Earthlink")

Book.createTable(ifNotExists=True) 
Book()
print Book.get(1).name

Happy New Year

Petr Jakes

-------------------------------------------------------------------------
This SF.net email is sponsored by: Microsoft
Defy all challenges. Microsoft(R) Visual Studio 2005.
http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/
_______________________________________________
sqlobject-discuss mailing list
sqlobject-discuss <at> lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/sqlobject-discuss
Petr Jakeš | 1 Jan 04:39
Picon

Re: sqlobject - _init question....

1) still do not understand why you using the SQL when you can create/drop tables from Python using SQLobject:

MyTable.dropTable(ifExists=True)
MyTable.createTable(ifNotExists=True)

2) I can't see the purpose of your code. Can you explain why are you trying to set id that way? I would rather set it from the code:

foo.createTable(ifNotExists=True)
tmp=foo(id="tikdsljg",  aa='qqq', id2='ffff')

Petr

PS: you are using some "strange" indentation in your code. Please try to follow:
http://www.python.org/doc/essays/styleguide.html and use 4 spaces indentation.

-------------------------------------------------------------------------
This SF.net email is sponsored by: Microsoft
Defy all challenges. Microsoft(R) Visual Studio 2005.
http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/
_______________________________________________
sqlobject-discuss mailing list
sqlobject-discuss <at> lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/sqlobject-discuss
bruce | 1 Jan 05:58
Picon
Favicon

Re: sqlobject - _init question....

hi petr...

the indentation is simply what the tab was/is set to... trivial.

i already have an sql schema.. so i don't need sqlobject to create the table... i'm interfacing with a live mysql/db.

while it's easy/trivial to add the 'id' via calling the class, and setting the 'id' just like the other
fields... my real goal/task was to see if you can set a field in the "set" method. the "set" method works for
any field that i've tried, except the "id" issue!

the docs imply that you should be able to have the constructor be able to set a field within the constructor.

so.. this is the goal...

".....
It's a little more complicated if you want to override the behavior of an database column attribute. For
instance, imagine there's special code you want to run whenever someone's name changes. In many systems
you'd do some custom code, then call the superclass's code. But the superclass (SQLObject) doesn't know
anything about the column in your subclass. It's even worse with properties.

SQLObject creates methods like _set_lastName for each of your columns, but again you can't use this, since
there's no superclass to reference (and you can't write SQLObject._set_lastName(...), because the
SQLObject class doesn't know about your class's columns). You want to override that _set_lastName
method yourself.

To deal with this, SQLObject creates two methods for each getter and setter, for example: _set_lastName
and _SO_set_lastName. So to intercept all changes to lastName:

"....

-----Original Message-----
From: petr.jakes.tpc <at> gmail.com [mailto:petr.jakes.tpc <at> gmail.com]On Behalf Of Petr Jakeš
Sent: Monday, December 31, 2007 7:39 PM
To: bruce
Cc: sqlobject-discuss <at> lists.sourceforge.net
Subject: Re: [SQLObject] sqlobject - _init question....

1) still do not understand why you using the SQL when you can create/drop tables from Python using SQLobject:

MyTable.dropTable(ifExists=True)
MyTable.createTable(ifNotExists=True)

2) I can't see the purpose of your code. Can you explain why are you trying to set id that way? I would rather set
it from the code:

foo.createTable(ifNotExists=True)
tmp=foo(id="tikdsljg",  aa='qqq', id2='ffff') 

Petr

PS: you are using some "strange" indentation in your code. Please try to follow:
http://www.python.org/doc/essays/styleguide.html and use 4 spaces indentation. 

-------------------------------------------------------------------------
This SF.net email is sponsored by: Microsoft
Defy all challenges. Microsoft(R) Visual Studio 2005.
http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/
_______________________________________________
sqlobject-discuss mailing list
sqlobject-discuss <at> lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/sqlobject-discuss
Petr Jakeš | 1 Jan 15:56
Picon

Re: sqlobject - _init question....

the indentation is simply what the tab was/is set to... trivial.
Great :-)

i already have an sql schema.. so i don't need sqlobject to create the table... i'm interfacing with a live mysql/db.

It  was just my reaction to the SQL code in your posting. I had a feeling you are creating/dropping the table using this code.

while it's easy/trivial to add the 'id' via calling the class, and setting the 'id' just like the other fields... my real goal/task was to see if you can set a field in the "set" method. the "set" method works for any field that i've tried, except the "id" issue!

the docs imply that you should be able to have the constructor be able to set a field within the constructor.

so.. this is the goal...

It is out of my current knowledge, I am working with SQLobject for about 2-3 weeks. But this technique looks interesting. I was trying to follow your example and following does not work for me. Can you please help me?

class foo(SQLObject):
    class sqlmeta:
        idName='id'
        idType=str
    aa =StringCol(length=5)
    id2 =StringCol(length=20)
 
    def _set_aa():
        d = 'tikdsljg'
        _SO_set_aa(d)

foo.createTable(ifNotExists=True)
tmp=foo(id="25", id2='ffff')

Anyway, where do you see the benefit of this approach? I think it is not a big difference if I control the id value outside of the table/class definition and then I insert the row values by calling the table object as usually.
Petr
-------------------------------------------------------------------------
This SF.net email is sponsored by: Microsoft
Defy all challenges. Microsoft(R) Visual Studio 2005.
http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/
_______________________________________________
sqlobject-discuss mailing list
sqlobject-discuss <at> lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/sqlobject-discuss
Glenn MacGregor | 2 Jan 22:52
Picon

Newbe Question

Hi All,

I am very new to SQLObject and Python. I am trying to use SQLObject to represent an existing MySQL schema. I have the following code:

...

class Temp(SQLObject):
    class sqlmeta:
        table = "temp"

t = Temp()
tt = t.get(1)



When I hit the 't = Temp()' line a new  empty" entry is put into that table. All I want to do is read the table into this object, is this possible?

Thanks

 Glenn

-------------------------------------------------------------------------
This SF.net email is sponsored by: Microsoft
Defy all challenges. Microsoft(R) Visual Studio 2005.
http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/
_______________________________________________
sqlobject-discuss mailing list
sqlobject-discuss <at> lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/sqlobject-discuss
Oleg Broytmann | 2 Jan 23:15
X-Face
Picon
Favicon

Re: Newbe Question

On Wed, Jan 02, 2008 at 04:52:27PM -0500, Glenn MacGregor wrote:
> I am very new to SQLObject and Python.

   Welcome!

> class Temp(SQLObject):
>     class sqlmeta:
>         table = "temp"
> 
> t = Temp()
> tt = t.get(1)

tt = Temp.get(1)

   .get() is a class method - a method that is called on class, not on an
instance; so you don't need to create the 't' instance to call .get().

> When I hit the 't = Temp()' line a new  empty" entry is put into that table.

   Yes, upon creating a new instance SQLObject inserts a new row to the
table. This is how you populate tables with SQLObject.

Oleg.
--

-- 
     Oleg Broytmann            http://phd.pp.ru/            phd <at> phd.pp.ru
           Programmers don't die, they just GOSUB without RETURN.

-------------------------------------------------------------------------
This SF.net email is sponsored by: Microsoft
Defy all challenges. Microsoft(R) Visual Studio 2005.
http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/
Glenn MacGregor | 3 Jan 02:25
Picon

Re: Newbe Question

Oleg,

Thanks for your help.

On Jan 2, 2008 5:15 PM, Oleg Broytmann <phd <at> phd.pp.ru> wrote:
On Wed, Jan 02, 2008 at 04:52:27PM -0500, Glenn MacGregor wrote:
> I am very new to SQLObject and Python.

  Welcome!

> class Temp(SQLObject):
>     class sqlmeta:
>         table = "temp"
>
> t = Temp()
> tt = t.get(1)

tt = Temp.get(1)

  .get() is a class method - a method that is called on class, not on an
instance; so you don't need to create the 't' instance to call .get().

Thanks.


> When I hit the 't = Temp()' line a new  empty" entry is put into that table.

  Yes, upon creating a new instance SQLObject inserts a new row to the
table. This is how you populate tables with SQLObject.

Is there a way to read the current table in with out inserting anything?


Oleg.
--
    Oleg Broytmann             http://phd.pp.ru/            phd <at> phd.pp.ru
          Programmers don't die, they just GOSUB without RETURN.

-------------------------------------------------------------------------
This SF.net email is sponsored by: Microsoft
Defy all challenges. Microsoft(R) Visual Studio 2005.
http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/
_______________________________________________
sqlobject-discuss mailing list
sqlobject-discuss <at> lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/sqlobject-discuss
Petr Jakeš | 3 Jan 02:50
Picon

Re: Newbe Question

  Yes, upon creating a new instance SQLObject inserts a new row to the
table. This is how you populate tables with SQLObject.

Is there a way to read the current table in with out inserting anything?

If you go:
print Temp.select()

you will see the SQL select command. All rows, which this command returns you can get this way:

for tempRow in Temp.select():
    print tempRow

HTH

Petr
-------------------------------------------------------------------------
This SF.net email is sponsored by: Microsoft
Defy all challenges. Microsoft(R) Visual Studio 2005.
http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/
_______________________________________________
sqlobject-discuss mailing list
sqlobject-discuss <at> lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/sqlobject-discuss

Gmane