SQL vs SQLObject

Study group dedicated to learning how to code in the Python language.

Moderators: snarkout, Patrick, dann

Post Reply
User avatar
Jza
Posts: 466
Joined: Sun Oct 30, 2005 7:01 pm
Location: Mexico
Contact:

SQL vs SQLObject

Post by Jza » Sat May 12, 2007 4:15 am

So SQLite was on my later post but for people that are more used to MySQL but could use postgresql too there is SQLObject which is more of a native Object oriented python form of interacting wih tthe db.

No SQL just objects and instructions, here is an example, SQL would be:

Code: Select all

CREATE TABLE person (
         id INT PRIMARY KEY AUTO_INCREMENT,
         first_name VARCHAR(100) NOT NULL,
         middle_initial CHAR(1),
         last_name VARCHAR(100) NOT NULL,
         last_contact TIMESTAMP NOT NULL
     );
Here is what will happen using SQLObjects:

Code: Select all

1  from sqlobject import *  
2	from datetime import datetime  
3	          
4	class Person(SQLObject):  
5	 
6	    firstName = StringCol(length=100)  
7	    middleInitial = StringCol(length=1, default=None)  
8	    lastName = StringCol(length=100)  
9	    lastContact = DateTimeCol(default=datetime.now)
So we import the SQLObject and the datetime, that right there will enable us to forget about the querky date timestamp that MySQL has.

The next step we create a class which will hold our table creation.

Finally we create an object oriented methodology where we have variables and values and this can be easily modiied later in the game withtout rewriting all the SQL again. Just reutilizing the object.
Alexandro COLORADO

User avatar
riddlebox
Posts: 86
Joined: Mon Jul 03, 2006 2:09 pm
Contact:

Post by riddlebox » Sat May 12, 2007 8:29 am

is that an add on module, that you have to download and install before you can use it?

User avatar
Jza
Posts: 466
Joined: Sun Oct 30, 2005 7:01 pm
Location: Mexico
Contact:

Post by Jza » Sat May 12, 2007 11:59 am

yeah although is one of the most popular :

Code: Select all

$ urpmq -i python-sqlobject
Name        : python-sqlobject
Version     : 0.7.0
Release     : 3mdv2007.1
Group       : Development/Python
Size        : 906059                       Architecture: noarch
Source RPM  : python-sqlobject-0.7.0-3mdv2007.1.src.rpm   Build Host: n5.mandriva.com
Packager    : Michael Scherer <misc@mandriva.org>
URL         : http://sqlobject.org/
Summary     : Object-Relational Manager, aka database wrapper for python
Description :
Classes created using SQLObject wrap database rows, presenting a
friendly-looking Python object instead of a database/SQL interface.
Emphasizes convenience.  Works with MySQL, PostgreSQL, SQLite, Firebird
Alexandro COLORADO

Post Reply