a newbie use

12 views
Skip to first unread message

pingfanr...@gmail.com

unread,
Jan 8, 2019, 10:06:30 AM1/8/19
to python-sql
I have read the example in https://pypi.org/project/python-sql/#description.


'The flavor of python-sql allows you to use almost any database using any
PEP 249 python library.'

But I still don't know how to use it in mysql.

conn = MySQLdb.Connect(unix_socket = config.mysql_sock,
user = config.mysql_user,
passwd = secret,
connect_timeout=10,
charset="utf8",
cursorclass = MySQLdb.cursors.DictCursor)
cursor = conn.cursor()

user = Table('user')
select = user.select()
select.where = user.EnglishName=='testname'

How to excute it ?
cursor.execute(select) ?
cursor.execute(tuple(select) ?

Cédric Krier

unread,
Jan 8, 2019, 10:29:04 AM1/8/19
to python-sql
On 2019-01-08 06:55, pingfanr...@gmail.com wrote:
> 'The flavor of python-sql allows you to use almost any database using any
> PEP 249 python library.'
>
> But I still don't know how to use it in mysql.
>
> conn = MySQLdb.Connect(unix_socket = config.mysql_sock,
> user = config.mysql_user,
> passwd = secret,
> connect_timeout=10,
> charset="utf8",
> cursorclass = MySQLdb.cursors.DictCursor)
> cursor = conn.cursor()
>
> user = Table('user')
> select = user.select()
> select.where = user.EnglishName=='testname'
>
> How to excute it ?
> cursor.execute(select) ?
> cursor.execute(tuple(select) ?

You must run:

cursor.execute(*select)

The query object is iterable and return the query string follwed by the
params.

Or if you prefer you can also run:

cursor.execute(str(select), select.params)


--
Cédric Krier - B2CK SPRL
Email/Jabber: cedric...@b2ck.com
Tel: +32 472 54 46 59
Website: http://www.b2ck.com/
Message has been deleted

pingfanr...@gmail.com

unread,
Jan 8, 2019, 10:27:14 PM1/8/19
to python-sql
Thanks very much.It works.
In my MySQL ,I need to set SQL_MODE like:

sql> SET SESSION SQL_MODE=ANSI_QUOTES;

In python:
conn.set_sql_mode('ANSI_QUOTES')
Reply all
Reply to author
Forward
0 new messages