same code same database but the not same insert result

19 views
Skip to first unread message

Frank Liou

unread,
Jul 9, 2014, 2:30:07 AM7/9/14
to sqlal...@googlegroups.com
i use flask connect to postgres

def post_insert(username):
    conn = engine.connect()
    encoded = base64.b64encode(username.encode('utf-8'))
    puresql = sqla.text("INSERT INTO friends(name) VALUES(:encoded)")
    conn.execute(puresql,encoded = encoded)


i insert username encode with base64 to my database

but

one i got 

12345678

anoter i got

\x4d54497a4e4455324e7a673d



what 's happen??


Jonathan Vanasco

unread,
Jul 9, 2014, 11:42:22 AM7/9/14
to sqlal...@googlegroups.com
Postgres can handle UTF-8 encoded data.   you would probably be better off not encoding stuff into b64 in python and just using postgres to store the utf8 data.

The string "12345678" is not base64 encoded.  That was probably an entry inserted from another bit of code.

    >>> print base64.b64encode("12345678")
    MTIzNDU2Nzg=

These strings can't be base64 decoded :
    >>> print base64.b64decode("\x4d54497a4e4455324e7a673d")
    >>> print base64.b64decode("x4d54497a4e4455324e7a673d")

This one can :
    >>> print base64.b64decode("4d54497a4e4455324e7a673d")


Frank Liou

unread,
Jul 9, 2014, 9:56:14 PM7/9/14
to sqlal...@googlegroups.com
but i mean

i encoded 12345678  that should be   MTIzNDU2Nzg but \x4d54497a4e4455324e7a673d

the same code 

why get not the same result?

Jonathan Vanasco

unread,
Jul 10, 2014, 11:28:09 AM7/10/14
to sqlal...@googlegroups.com


On Wednesday, July 9, 2014 9:56:14 PM UTC-4, Frank Liou wrote:
why get not the same result?


There is no reason why the example you gave would insert that value.  

You need to make a standalone script that shows this error happening in SqlAlchemy.  

If you can't reproduce the error, then no one can help you.

Look at this test script I made for your example:


It generates this SQL transaction, and shows everything working as-expected :

2014-07-10 11:20:29,707 INFO sqlalchemy.engine.base.Engine COMMIT
2014-07-10 11:20:29,707 INFO sqlalchemy.engine.base.Engine BEGIN (implicit)
2014-07-10 11:20:29,708 INFO sqlalchemy.engine.base.Engine INSERT INTO friends(name) VALUES(?)
2014-07-10 11:20:29,708 INFO sqlalchemy.engine.base.Engine ('MTIzNDU2Nw==',)
2014-07-10 11:20:29,708 INFO sqlalchemy.engine.base.Engine ROLLBACK


Reply all
Reply to author
Forward
0 new messages