Questions tagged [sqlobject]

SQLObject is a free and open-source (LGPL) Python object-relational mapper. SQLObject supports a number of backends: MySQL/MariaDB (with a number of DB API drivers: MySQLdb, mysqlclient, mysql-connector, PyMySQL, mariadb), PostgreSQL (psycopg2, PyGreSQL), SQLite (builtin sqlite, pysqlite). Works under Python 2.7 and 3.4+.

What is SQLObject

SQLObject is a free and open-source (LGPL) Python object-relational mapper. Your database tables are described as classes, and rows are instances of those classes. SQLObject is meant to be easy to use and quick to get started with.

SQLObject supports a number of backends: MySQL/MariaDB (with a number of DB API drivers: MySQLdb, mysqlclient, mysql-connector, PyMySQL, mariadb), PostgreSQL (psycopg2, PyGreSQL, partially pg8000 and py-postgresql), SQLite (builtin sqlite, pysqlite, partially supersqlite); connections to other backends

  • Firebird, Sybase, MSSQL and MaxDB (also known as SAPDB) - are less debugged).

Python 2.7 or 3.4+ is required.

Where is SQLObject

Site: http://sqlobject.org

Download: https://pypi.org/project/SQLObject/

News and changes: http://sqlobject.org/News.html

Mailing lists: https://sourceforge.net/p/sqlobject/mailman/

Development: http://sqlobject.org/devel/

Developer Guide: http://sqlobject.org/DeveloperGuide.html

Example

Install:

$ pip install sqlobject

Create a simple class that wraps a table:

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

Use the object:

>>> p = Person(fname="John", lname="Doe")
>>> p
<Person 1 fname='John' mi=None lname='Doe'>
>>> p.fname
'John'
>>> p.mi = 'Q'
>>> p2 = Person.get(1)
>>> p2
<Person 1 fname='John' mi='Q' lname='Doe'>
>>> p is p2
True

Queries:

>>> p3 = Person.selectBy(lname="Doe")[0]
>>> p3
<Person 1 fname='John' mi='Q' lname='Doe'>
>>> pc = Person.select(Person.q.lname=="Doe").count()
>>> pc
1
73 questions
1
vote
1 answer

A more pythonic way to build a class based on a string (how not to use eval)

OK. So I've got a database where I want to store references to other Python objects (right now I'm using to store inventory information for person stores of beer recipe ingredients). Since there are about 15-20 different categories of ingredients…
tkone
  • 22,092
  • 5
  • 54
  • 78
1
vote
3 answers

Easier way to create a JSON object from an SQLObject

EDIT -- took the code from below and made it so it can handle ForiegnKeys, Decimal numbers (although i'm doing a very forced float conversion). It returns a dict now so it can be recursive. from sqlobject import SQLObject from decimal import…
tkone
  • 22,092
  • 5
  • 54
  • 78
1
vote
1 answer

Error returned when running imdbpy2sql.py with MySQL database

I am working on getting IMDbPY installed and working and I have come to a point where I am working on pulling in all of the data from the flat (text) files in to a (MySQL) database. When I run the appropriate command: imdbpy2sql.py -d /tmp/IMDB/ -u…
Cody
  • 11
  • 2
1
vote
1 answer

Print SQL generated by SQLObject

from sqlobject import * class Data(SQLObject): ts = TimeCol() val = FloatCol() Data.select().count() Fails with: AttributeError: No connection has been defined for this thread or process How do I get the SQL which would be generated,…
davetapley
  • 17,000
  • 12
  • 60
  • 86
1
vote
3 answers

web presenting framework for SQLObject/SQLAlchemy projects

I'm looking for the most graceful way of taking a non-web project based on either SQLObject or SQLAlchemy and overlay it with a decent web-framework. I've looked at Turbogears, and even though it looks like something I could use, the models seems to…
joveha
  • 2,599
  • 2
  • 17
  • 19
1
vote
2 answers

Mocking sqlobject function call for test db

I am trying to mock sqlbuilder.func for test cases with pytest I successfully mocked sqlbuilder.func.TO_BASE64 with correct output but when I tried mocking sqlbuilder.func.FROM_UNIXTIME I didn't get any error but the resulted output is incorrect…
Chandan
  • 11,465
  • 1
  • 6
  • 25
1
vote
1 answer

How to construct sql query with bitwise operation using sqlobject ORM in python3?

I am trying to create a query in python3 using sqlobject mapper. I have a requirement of filtering a custom field based on the output of bitise operation. Query looks like this: query = sqlobject.AND(query,dl..q.anomalyType &…
Rajat Doshi
  • 118
  • 7
1
vote
2 answers

trying to add data and query a mysql database using sqlobject and python 3

My code is raising an exception as shown below. I am pretty sure the problem is with my definition of the table relationship. I have tried multiple alternatives that give varying errors :-( I have a License that can have one or more MacAddresses…
jordanthompson
  • 888
  • 1
  • 12
  • 29
1
vote
1 answer

python3 with SQLObject class pass parameters

I am new to python3 and tring to build a sqlobject class which named whatever. Then I created a function to caculate the average of one column. Here are parts of the codes. class whatever(sqlobject.SQLObject): _connection = connection f1 =…
Ze Xu
  • 13
  • 2
1
vote
1 answer

How to translate this to SQLObject: SELECT DISTINCT columnname WHERE

I've been going through the sqlobject and sqlbuilder documentation and forums and I cannot seem to grasp the information there. I have a specific SQL query that I need: select distinct author from blogtable where keyword = "dust"; Multiple authors…
Alex Boschmans
  • 515
  • 2
  • 12
1
vote
1 answer

ConnectionURI MSSQL Python

I'm coding a little something in Python. I need to get some data from a MicrosoftSQL database and convert it to a JSONObject. And i think i have some problems with the ConnectionForURI. I'm using simplejson and sqlobject libraries. Im not sure…
1
vote
0 answers

JOIN table with array

I get a response from a JSON API which contains the following data. "data": [ { "num1": 1, "num2": 2, "txt1": "test3" }, { "num1": 4, "num2": 5, "txt1": "test6" } ] Next, I have a…
Mtihc
  • 191
  • 1
  • 12
1
vote
3 answers

Python: get escaped SQL string

When I have a cursor, I know I can safely execute a query as follows: cur.execute("SELECT * FROM foo WHERE foo.bar = %s", (important_variable,)) Is there any way to just get the string safely without executing the query? For example, if…
Claudiu
  • 224,032
  • 165
  • 485
  • 680
1
vote
4 answers

create temporary table from cursor

Is there any way, in PostgreSQL accessed from Python using SQLObject, to create a temporary table from the results of a cursor? Previously, I had a query, and I created the temporary table directly from the query. I then had many other queries…
Claudiu
  • 224,032
  • 165
  • 485
  • 680
1
vote
3 answers

Database change underneath SQLObject

I'm starting a web project that likely should be fine with SQLite. I have SQLObject on top of it, but thinking long term here -- if this project should require a more robust (e.g. able to handle high traffic), I will need to have a transition plan…
torial
  • 13,085
  • 9
  • 62
  • 89