×

Discussion Board

Results 1 to 7 of 7

Thread: Creating tables

  1. #1
    Registered User
    Join Date
    Feb 2008
    Posts
    4

    Creating tables

    How can I create a table in a Database?
    Is there another way besides SQL CREATE TABLE?

    Thanks

  2. #2
    Nokia Developer Champion
    Join Date
    Feb 2008
    Location
    Ahmedabad, Gujarat, India
    Posts
    3,852

    Re: Creating tables

    Quote Originally Posted by andmart View Post
    How can I create a table in a Database?
    Is there another way besides SQL CREATE TABLE?

    Thanks
    hi andmart welcome to the python dibo
    plz check at the wiki
    and give feedback
    thanks

  3. #3
    Regular Contributor
    Join Date
    Dec 2007
    Location
    India
    Posts
    133

    Re: Creating tables

    Quote Originally Posted by andmart View Post
    How can I create a table in a Database?
    Is there another way besides SQL CREATE TABLE?

    Thanks
    Do you want to do it for an S60 application?

    Or are you talking about python on Pc?
    Kandyfloss

    V 7.0642.0
    18-10-06
    RH-51
    Nokia 7610

  4. #4
    Registered User
    Join Date
    Feb 2008
    Posts
    4

    Re: Creating tables

    kandyfloss:

    I´m asking about pys60 apps.

  5. #5
    Regular Contributor
    Join Date
    Dec 2007
    Location
    India
    Posts
    133

    Re: Creating tables

    Quote Originally Posted by andmart View Post
    kandyfloss:

    I´m asking about pys60 apps.
    Hi,

    May be this code may help you.

    Here's the mother of all your classes that map to database tables.

    Code:
    class Mapper(object):
        def __init__(self, id=None, **kw):
            if id is None:
                self.id = self._insert(**kw)
            else:
                self.id = id
        def _insert(self, **kw):
            names = ','.join(kw.keys())
            values = ','.join([self.quote(k,v) for k,v in kw.items()])
            tablename = self.__class__.__name__
            q = u"INSERT INTO %s(%s) VALUES (%s)" % (tablename, names, values)
            db.execute(q)
            # get last insert ID
            dbv.prepare(db, u'SELECT id FROM '+tablename+' ORDER BY id DESC')
            dbv.first_line()
            dbv.get_line()
            return dbv.col(1)
        def __getattr__(self, name):
            if name in self.mapping.__dict__:
                q = 'SELECT '+name+' FROM '+self.__class__.__name__
                q += ' WHERE id='+str(self.id)
                dbv.prepare(db, unicode(q))
                dbv.first_line()
                dbv.get_line()
                return dbv.col(1)
            else:
                return self.__dict__[name]
        def __repr__(self):
            return '<%s id=%d>' % (self.__class__.__name__, self.id)
        def quote(self, name, value):
            if self.mapping.__dict__[name].coltype == String:
                return "'%s'" % value.replace("'", "''")  # encode single quote
            else:
                return str(value)
        def __setattr__(self, name, value):
            if name in self.mapping.__dict__:
                q = 'UPDATE '+self.__class__.__name__+' SET '+name+'='
                q += self.quote(name, value) + " WHERE id=" + str(self.id)
                db.execute(unicode(q))
            else:
                self.__dict__[name] = value
        def set(self, **kw):
            q = "UPDATE "+self.__class__.__name__+" SET "
            for k, v in kw.items():
                q += k+'='+self.quote(k,v)+','
            q = q[:-1]+" WHERE id=%s" % self.id
            db.execute(unicode(q))
        def delete(self):
            q = 'DELETE FROM '+self.__class__.__name__+" WHERE id=" + str(self.id)
            db.execute(unicode(q))
            self.id = None
        def dict(self):
            names = [k for k in self.mapping.__dict__.keys() if not k.startswith('__')]
            q = 'SELECT '+','.join(names)+' FROM '+self.__class__.__name__
            q += ' WHERE id=' + str(self.id)
            dbv.prepare(db, unicode(q))
            dbv.first_line()
            dbv.get_line()
            dct = {'id': self.id}
            for i in range(dbv.col_count()):
                dct[names[i]] = dbv.col(i+1)
            return dct
        def select(cls, where=None, orderby=None):
            q = 'SELECT id FROM '+cls.__name__
            if where:
                q += ' WHERE '+where
            if orderby:
                q += ' ORDER BY '+orderby
            dbv = e32db.Db_view()  # need its own cursor
            dbv.prepare(db, unicode(q))
            dbv.first_line()
            for i in range(dbv.count_line()):
                dbv.get_line()
                yield cls(dbv.col(1))
                dbv.next_line()
        select = classmethod(select)

    And here is how you can create the class

    Code:
    class Person(Mapper):
        class mapping:
            # doesn't need id = column(Integer)
            name = column(String)
            age = column(Integer)
    Kandyfloss

    V 7.0642.0
    18-10-06
    RH-51
    Nokia 7610

  6. #6
    Registered User
    Join Date
    Feb 2008
    Posts
    4

    Re: Creating tables

    This code is great and I think it can handle all crud operations only extending mapper class. But seems to me that it don't create the physical table. is it right?
    Thanks

  7. #7
    Registered User
    Join Date
    Nov 2007
    Posts
    26

    Re: Creating tables

    Hi im working with a database i have but am not too familiar with selecting what i want from the database, and i know pys60 has some restricted sql syntax..

    I can use following code
    Code:
    WebAddr = select_all('Select preference from ' + str(Category) + ' where time = ' + str(x))
    but i want to have another filter..

    how can i say the above only i want to say 'where time = str(x) and where date = str(y)

    Can i do this with python i tried but kept getting errors.

    Can i have two things in the where clause.
    any examples?

    Thanks a lot

Similar Threads

  1. Replies: 0
    Last Post: 2008-01-29, 12:53
  2. Problem in build with S60 1FP
    By Manuelito_ in forum Symbian Tools & SDKs
    Replies: 14
    Last Post: 2007-09-18, 12:08
  3. Difference between creating View and Window
    By thilsen_06 in forum Symbian
    Replies: 0
    Last Post: 2007-02-09, 10:00
  4. abld command
    By kollar in forum Symbian Tools & SDKs
    Replies: 3
    Last Post: 2003-02-03, 07:51
  5. Problem with bldmake bldfiles for Series 60 SDK 6.1
    By dml in forum Symbian Tools & SDKs
    Replies: 3
    Last Post: 1970-01-01, 02:00

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
×