The following works for me on Python 2&3

how are you generating an error ?



# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
- -
#
# Use this file to build your own SSCCE
# SSCCE = Short, Self Contained, Correct (Compatible) Example
# see http://sscce.org/
#
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
- -


# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
- -
# Standard imports

import sqlalchemy
import sqlalchemy.orm

from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy import Boolean, Integer, Column, Unicode, ForeignKey, String


# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
- -
# You probably don't need to overwrite this
Base = declarative_base()

# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
- -
# Define some models that inherit from Base

class Foo(Base):
    __tablename__ = 'foo'
    id = Column(Integer, primary_key=True)
    name = Column(Unicode)


# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
- -
# set the engine

engine = sqlalchemy.create_engine('sqlite:///:memory:', echo=True)
Base.metadata.create_all(engine)

# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
- -
# do a simple query to trigger the mapper error

sessionFactory = sqlalchemy.orm.sessionmaker(bind=engine)
s = sessionFactory()


# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
- -
# test

from sqlalchemy.sql import expression
from sqlalchemy.ext.compiler import compiles


class stripctrl(expression.FunctionElement):
    type = String()
    name = 'stripctrl'


@compiles(stripctrl)
def stripctrl_default(element, compiler, **kw):
    args = list(element.clauses)
    return "REPLACE(REPLACE(REPLACE(%s, CHR(9), ''), CHR(10), ''), CHR(13), 
'')" % (compiler.process(args[0]))


@compiles(stripctrl, 'sqlite')
def stripctrl_sqlite(element, compiler, **kw):
    args = list(element.clauses)
    return "replace(replace(replace(%s, char(9), ''), char(10), ''), 
char(13), '')" % (compiler.process(args[0]))


cases = (None, "Aaaa", 123, 'A\ta', 'B\rb', 'C\nc,')
for idx, case in enumerate(cases):
    f = Foo()
    f.id = idx
    f.name = case
    s.add(f)
s.commit()
    
print("--SELECT")
q = s.query(stripctrl(Foo.name)).all()
print (q)

print ("-- INSERT")
for idx, case in enumerate(cases):
    f = Foo()
    f.id = idx + 100
    f.name = stripctrl(case)
    s.add(f)
s.commit()



-- 
SQLAlchemy - 
The Python SQL Toolkit and Object Relational Mapper

http://www.sqlalchemy.org/

To post example code, please provide an MCVE: Minimal, Complete, and Verifiable 
Example.  See  http://stackoverflow.com/help/mcve for a full description.
--- 
You received this message because you are subscribed to the Google Groups 
"sqlalchemy" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at https://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/d/optout.

Reply via email to