0

How to use illegal names for MySQL with SQLObject?

In pure SQL it is possible to use backquotes, say:

    SELECT `select from` FROM table1 WHERE 1;

...can be used to select the field called select from. Is it possible to tell SQLObject to utilize backquotes?

ax.
  • 58,560
  • 8
  • 81
  • 72
Alex
  • 43,191
  • 44
  • 96
  • 127
  • It depends on your definition of 'pure SQL'; the backticks in MySQL are as non-standard as the square brackets in MS SQL Server. The SQL standard requires such identifiers to be delimited - written inside double quotes. – Jonathan Leffler Sep 13 '09 at 02:25
  • @Jonathan Leffler: OK, double quotes might be fine as well. I'm looking for a way to access an existent DB with illegal names through SQLObject or another ORM. – Alex Sep 13 '09 at 02:31
  • If you turn on ANSI or ANSI_QUOTES in SQL_MODE you'll be able to use " in MySQL too. – bobince Sep 14 '09 at 02:19

1 Answers1

1
CREATE TABLE table1 (
  id INT(11),
  `select from` VARCHAR(255),
  PRIMARY KEY (id)
);
INSERT INTO table1 VALUES(1, 'test value');

to access select from from SQLObject, declare the column with backticks:

>>> class Table1(SQLObject):
...     myIllegallyNamedColumn = Col(dbName="`select from`")
... 
>>> list(Table1.select())
[<Table1 0 myIllegallyNamedColumn='test value'>]
ax.
  • 58,560
  • 8
  • 81
  • 72
  • I'd be wary about relying on this: apart from being MySQL-only, it seems like a bug that SQLObject is not properly escaping schema names already. Really this should be fixed in the library (at which point the above might break!) – bobince Sep 14 '09 at 02:22