Python论坛  - 讨论区

标题:[python-chinese] python中怎样表示嵌入字符串中的变量啊?

2007年07月11日 星期三 17:11

longlong asfnuts在gmail.com
星期三 七月 11 17:11:49 HKT 2007

×Ö·û´®ÖеıäÁ¿Ìæ»»

PHP 4 Ìí¼ÓÁËÒ»¸öеĻúÖÆÒÔÌæ»»×Ö·û´®ÖеıäÁ¿¡£ÄúÏÖÔÚ¿ÉÒÔ·ÃÎÊ×Ö·û´®ÖеĶÔÏó³ÉÔ±±äÁ¿ºÍ¶àάÊý×éÔªËØ¡£

Èç¹ûÄúÏëÕâô×ö£¬ÄúÐèҪʹÓôóÀ¨ºÅ½«Ò»¸öÃÀÔª·ûºÅºÍ±äÁ¿ÃûÀ¨ÆðÀ´£¬¾ÍÏñÕâÑù£º{$...}¡£

ÒªÔÚ×Ö·û´®ÖÐǶÈë¶ÔÏó³ÉÔ±±äÁ¿£¬Ö»Òª¼òµ¥µÄд "text {$obj->member} text"¡£¶øÔÚ PHP 3 ÖУ¬ÄúµÃʹÓà "text
".$obj->member." text"¡£

Õâ¸ö¸Ä½ø¿ÉÒÔʹ´úÂë¸üÒ׶Á£¬µ«Õâ¿ÉÄÜ»áÆÆ»µÓà PHP 3 дµÄ´úÂë¡£µ«ÊÇÄúºÜÈÝÒ×ÐÞÕýËü£ºÔÚ´úÂëÖвéÕÒ×Ö·û´®ÖÐµÄ {$£¬Ê¹ÓÃËÑË÷Ìæ»»¹¤¾ß½«ËüÌ滻Ϊ \{$¡£



ÉÏÃæÊÇphpµÄ·½Ê½¡£
-------------- 下一部分 --------------
Ò»¸öHTML¸½¼þ±»ÒƳý...
URL: http://python.cn/pipermail/python-chinese/attachments/20070711/dff2369b/attachment.htm 

[导入自Mailman归档:http://www.zeuux.org/pipermail/zeuux-python]

2007年07月11日 星期三 17:26

沈崴 wileishn在gmail.com
星期三 七月 11 17:26:29 HKT 2007

Ruby Sucks, PEP292 says.

在 07-7-11,longlong<asfnuts在gmail.com> 写道:
> 字符串中的变量替换
>
>  PHP 4 添加了一个新的机制以替换字符串中的变量。您现在可以访问字符串中的对象成员变量和多维数组元素。
>
>  如果您想这么做,您需要使用大括号将一个美元符号和变量名括起来,就像这样:{$...}。
>
>  要在字符串中嵌入对象成员变量,只要简单的写 "text {$obj->member} text"。而在 PHP 3 中,您得使用 "text
> ".$obj->member." text"。
>
>  这个改进可以使代码更易读,但这可能会破坏用 PHP 3 写的代码。但是您很容易修正它:在代码中查找字符串中的 {$,使用搜索替换工具将它替换为
> \{$。
>
>
>
>
> 上面是php的方式。
>
> _______________________________________________
> python-chinese
> Post: send python-chinese在lists.python.cn
> Subscribe: send subscribe to
> python-chinese-request在lists.python.cn
> Unsubscribe: send unsubscribe to
> python-chinese-request在lists.python.cn
> Detail Info:
> http://python.cn/mailman/listinfo/python-chinese
>

匿名学习 Python 中 ...

[导入自Mailman归档:http://www.zeuux.org/pipermail/zeuux-python]

2007年07月13日 星期五 14:48

Xell Zhang xellzhang在gmail.com
星期五 七月 13 14:48:11 HKT 2007

ÊDz»ÊÇ
print 'hello, %s' % person.name
Õâ¸öÑù×Ó£¿

On 7/11/07, ÉòáË <wileishn在gmail.com> wrote:
>
> Ruby Sucks, PEP292 says.
>
> ÔÚ 07-7-11£¬longlong<asfnuts在gmail.com> дµÀ£º
> > ×Ö·û´®ÖеıäÁ¿Ìæ»»
> >
> >  PHP 4 Ìí¼ÓÁËÒ»¸öеĻúÖÆÒÔÌæ»»×Ö·û´®ÖеıäÁ¿¡£ÄúÏÖÔÚ¿ÉÒÔ·ÃÎÊ×Ö·û´®ÖеĶÔÏó³ÉÔ±±äÁ¿ºÍ¶àάÊý×éÔªËØ¡£
> >
> >  Èç¹ûÄúÏëÕâô×ö£¬ÄúÐèҪʹÓôóÀ¨ºÅ½«Ò»¸öÃÀÔª·ûºÅºÍ±äÁ¿ÃûÀ¨ÆðÀ´£¬¾ÍÏñÕâÑù£º{$...}¡£
> >
> >  ÒªÔÚ×Ö·û´®ÖÐǶÈë¶ÔÏó³ÉÔ±±äÁ¿£¬Ö»Òª¼òµ¥µÄд "text {$obj->member} text"¡£¶øÔÚ PHP 3 ÖУ¬ÄúµÃʹÓà "text
> > ".$obj->member." text"¡£
> >
> >  Õâ¸ö¸Ä½ø¿ÉÒÔʹ´úÂë¸üÒ׶Á£¬µ«Õâ¿ÉÄÜ»áÆÆ»µÓà PHP 3 дµÄ´úÂë¡£µ«ÊÇÄúºÜÈÝÒ×ÐÞÕýËü£ºÔÚ´úÂëÖвéÕÒ×Ö·û´®ÖÐµÄ {$£¬Ê¹ÓÃËÑË÷Ìæ»»¹¤¾ß½«ËüÌ滻Ϊ
> > \{$¡£
> >
> >
> >
> >
> > ÉÏÃæÊÇphpµÄ·½Ê½¡£
> >
> > _______________________________________________
> > python-chinese
> > Post: send python-chinese在lists.python.cn
> > Subscribe: send subscribe to
> > python-chinese-request在lists.python.cn
> > Unsubscribe: send unsubscribe to
> > python-chinese-request在lists.python.cn
> > Detail Info:
> > http://python.cn/mailman/listinfo/python-chinese
> >
>
> ÄäÃûѧϰ Python ÖÐ ...
> _______________________________________________
> python-chinese
> Post: send python-chinese在lists.python.cn
> Subscribe: send subscribe to python-chinese-request在lists.python.cn
> Unsubscribe: send unsubscribe to  python-chinese-request在lists.python.cn
> Detail Info: http://python.cn/mailman/listinfo/python-chinese




-- 
Zhang Xiao

Engineer, Web development

Ethos Tech.
http://www.ethos.com.cn
-------------- 下一部分 --------------
Ò»¸öHTML¸½¼þ±»ÒƳý...
URL: http://python.cn/pipermail/python-chinese/attachments/20070713/1c09b6b3/attachment.html 

[导入自Mailman归档:http://www.zeuux.org/pipermail/zeuux-python]

2007年07月13日 星期五 14:59

longlong asfnuts在gmail.com
星期五 七月 13 14:59:39 HKT 2007

ÓÐʱÔÚpythonÒªÖ´ÐÐsqlÓï¾ä£¬²ÎÊýÀàÐÍÊÇ×Ö·û´®£¬ÎÒÏëÔÚÕâ¸ö×Ö·û´®ÖмÓÈë±äÁ¿£¬Ôõô×ö£¿
ÔÚ07-7-13£¬Xell Zhang <xellzhang在gmail.com> дµÀ£º
>
> ÊDz»ÊÇ
> print 'hello, %s' % person.name
> Õâ¸öÑù×Ó£¿
>
> On 7/11/07, ÉòáË <wileishn在gmail.com > wrote:
> >
> > Ruby Sucks, PEP292 says.
> >
> > ÔÚ 07-7-11£¬longlong< asfnuts在gmail.com> дµÀ£º
> > > ×Ö·û´®ÖеıäÁ¿Ìæ»»
> > >
> > >  PHP 4 Ìí¼ÓÁËÒ»¸öеĻúÖÆÒÔÌæ»»×Ö·û´®ÖеıäÁ¿¡£ÄúÏÖÔÚ¿ÉÒÔ·ÃÎÊ×Ö·û´®ÖеĶÔÏó³ÉÔ±±äÁ¿ºÍ¶àάÊý×éÔªËØ¡£
> > >
> > >  Èç¹ûÄúÏëÕâô×ö£¬ÄúÐèҪʹÓôóÀ¨ºÅ½«Ò»¸öÃÀÔª·ûºÅºÍ±äÁ¿ÃûÀ¨ÆðÀ´£¬¾ÍÏñÕâÑù£º{$...}¡£
> > >
> > >  ÒªÔÚ×Ö·û´®ÖÐǶÈë¶ÔÏó³ÉÔ±±äÁ¿£¬Ö»Òª¼òµ¥µÄд "text {$obj->member} text"¡£¶øÔÚ PHP 3 ÖУ¬ÄúµÃʹÓÃ
> > "text
> > > ".$obj->member." text"¡£
> > >
> > >  Õâ¸ö¸Ä½ø¿ÉÒÔʹ´úÂë¸üÒ׶Á£¬µ«Õâ¿ÉÄÜ»áÆÆ»µÓà PHP 3 дµÄ´úÂë¡£µ«ÊÇÄúºÜÈÝÒ×ÐÞÕýËü£ºÔÚ´úÂëÖвéÕÒ×Ö·û´®ÖеÄ
> > {$£¬Ê¹ÓÃËÑË÷Ìæ»»¹¤¾ß½«ËüÌ滻Ϊ
> > > \{$¡£
> > >
> > >
> > >
> > >
> > > ÉÏÃæÊÇphpµÄ·½Ê½¡£
> > >
> > > _______________________________________________
> > > python-chinese
> > > Post: send python-chinese在lists.python.cn
> > > Subscribe: send subscribe to
> > > python-chinese-request在lists.python.cn
> > > Unsubscribe: send unsubscribe to
> > > python-chinese-request在lists.python.cn
> > > Detail Info:
> > > http://python.cn/mailman/listinfo/python-chinese
> > >
> >
> > ÄäÃûѧϰ Python ÖÐ ...
> > _______________________________________________
> > python-chinese
> > Post: send python-chinese在lists.python.cn
> > Subscribe: send subscribe to python-chinese-request在lists.python.cn
> > Unsubscribe: send unsubscribe to  python-chinese-request在lists.python.cn
> > Detail Info: http://python.cn/mailman/listinfo/python-chinese
>
>
>
>
> --
> Zhang Xiao
>
> Engineer, Web development
>
> Ethos Tech.
> http://www.ethos.com.cn
> _______________________________________________
> python-chinese
> Post: send python-chinese在lists.python.cn
> Subscribe: send subscribe to python-chinese-request在lists.python.cn
> Unsubscribe: send unsubscribe to  python-chinese-request在lists.python.cn
> Detail Info: http://python.cn/mailman/listinfo/python-chinese
>
-------------- 下一部分 --------------
Ò»¸öHTML¸½¼þ±»ÒƳý...
URL: http://python.cn/pipermail/python-chinese/attachments/20070713/7b10bd99/attachment.htm 

[导入自Mailman归档:http://www.zeuux.org/pipermail/zeuux-python]

2007年07月13日 星期五 15:31

Xell Zhang xellzhang在gmail.com
星期五 七月 13 15:31:50 HKT 2007

ÎÒ×Ô¼º·â×°ÁËÒ»¸öÀàÓÃÀ´´¦Àísql¡£
class MySQLConnector:
    ''' MySQLConnector holds a connection with a MySQL database

    MySQLConnector instance creates a connection by using
    parameters passed. The connection will be hold by this
    instance. When a sql is run this instance will generate
    a cursor and use it to query. After query cursor will
    be closed.'''

    def __init__(self, host, port, user, passwd, db):
        self.con = MySQLdb.connect(
            host = host,
            port = port,
            user = user,
            passwd = passwd,
            db = db
        )
        self.lock = threading.Lock ()

    def query(self, sql, args = ()):
        self.lock.acquire()
        cursor = self.con.cursor()
        cursor.execute(sql, args)
        result = cursor.fetchall()
        cursor.execute ('commit')
        cursor.close()
        self.lock.release()
        return result

¿ÉÒÔÕâÑùʹÓãº
dbCon = MySQLConnector('yourhost', 3306, 'username', 'passwd', 'dbname')
dbCon.query('SELECT * FROM user WHERE username = %s', ('xzhang'))
dbCon.query('INSERT INTO user (username, age) VALUES (%s, %s)', ('xzhang',
23))

ÎÒ¸Õ¿ªÊ¼Ñ§Ï°£¬²»ÖªµÀÎÒµÄ×ö·¨ÊÇ·ñºÃ¡£ÐèҪעÒâµÄÊDz»¹ÜÄã´«ÈëµÄÊý¾ÝÊÇʲôÀàÐÍ£¬¶¼ÓÃ%s¡£

On 7/13/07, longlong <asfnuts在gmail.com> wrote:
>
> ÓÐʱÔÚpythonÒªÖ´ÐÐsqlÓï¾ä£¬²ÎÊýÀàÐÍÊÇ×Ö·û´®£¬ÎÒÏëÔÚÕâ¸ö×Ö·û´®ÖмÓÈë±äÁ¿£¬Ôõô×ö£¿
> ÔÚ07-7-13£¬Xell Zhang <xellzhang在gmail.com> дµÀ£º
> >
> > ÊDz»ÊÇ
> > print 'hello, %s' % person.name
> > Õâ¸öÑù×Ó£¿
> >
> > On 7/11/07, ÉòáË <wileishn在gmail.com > wrote:
> > >
> > > Ruby Sucks, PEP292 says.
> > >
> > > ÔÚ 07-7-11£¬longlong< asfnuts在gmail.com> дµÀ£º
> > > > ×Ö·û´®ÖеıäÁ¿Ìæ»»
> > > >
> > > >  PHP 4 Ìí¼ÓÁËÒ»¸öеĻúÖÆÒÔÌæ»»×Ö·û´®ÖеıäÁ¿¡£ÄúÏÖÔÚ¿ÉÒÔ·ÃÎÊ×Ö·û´®ÖеĶÔÏó³ÉÔ±±äÁ¿ºÍ¶àάÊý×éÔªËØ¡£
> > > >
> > > >  Èç¹ûÄúÏëÕâô×ö£¬ÄúÐèҪʹÓôóÀ¨ºÅ½«Ò»¸öÃÀÔª·ûºÅºÍ±äÁ¿ÃûÀ¨ÆðÀ´£¬¾ÍÏñÕâÑù£º{$...}¡£
> > > >
> > > >  ÒªÔÚ×Ö·û´®ÖÐǶÈë¶ÔÏó³ÉÔ±±äÁ¿£¬Ö»Òª¼òµ¥µÄд "text {$obj->member} text"¡£¶øÔÚ PHP 3 ÖУ¬ÄúµÃʹÓÃ
> > > "text
> > > > ".$obj->member." text"¡£
> > > >
> > > >  Õâ¸ö¸Ä½ø¿ÉÒÔʹ´úÂë¸üÒ׶Á£¬µ«Õâ¿ÉÄÜ»áÆÆ»µÓà PHP 3 дµÄ´úÂë¡£µ«ÊÇÄúºÜÈÝÒ×ÐÞÕýËü£ºÔÚ´úÂëÖвéÕÒ×Ö·û´®ÖеÄ
> > > {$£¬Ê¹ÓÃËÑË÷Ìæ»»¹¤¾ß½«ËüÌ滻Ϊ
> > > > \{$¡£
> > > >
> > > >
> > > >
> > > >
> > > > ÉÏÃæÊÇphpµÄ·½Ê½¡£
> > > >
> > > > _______________________________________________
> > > > python-chinese
> > > > Post: send python-chinese在lists.python.cn
> > > > Subscribe: send subscribe to
> > > > python-chinese-request在lists.python.cn
> > > > Unsubscribe: send unsubscribe to
> > > > python-chinese-request在lists.python.cn
> > > > Detail Info:
> > > > http://python.cn/mailman/listinfo/python-chinese
> > > >
> > >
> > > ÄäÃûѧϰ Python ÖÐ ...
> > > _______________________________________________
> > > python-chinese
> > > Post: send python-chinese在lists.python.cn
> > > Subscribe: send subscribe to python-chinese-request在lists.python.cn
> > > Unsubscribe: send unsubscribe to  python-chinese-request在lists.python.cn
> > > Detail Info: http://python.cn/mailman/listinfo/python-chinese
> >
> >
> >
> >
> > --
> > Zhang Xiao
> >
> > Engineer, Web development
> >
> > Ethos Tech.
> > http://www.ethos.com.cn
> > _______________________________________________
> > python-chinese
> > Post: send python-chinese在lists.python.cn
> > Subscribe: send subscribe to python-chinese-request在lists.python.cn
> > Unsubscribe: send unsubscribe to
> > python-chinese-request在lists.python.cn
> > Detail Info: http://python.cn/mailman/listinfo/python-chinese
> >
>
>
> _______________________________________________
> python-chinese
> Post: send python-chinese在lists.python.cn
> Subscribe: send subscribe to python-chinese-request在lists.python.cn
> Unsubscribe: send unsubscribe to  python-chinese-request在lists.python.cn
> Detail Info: http://python.cn/mailman/listinfo/python-chinese
>



-- 
Zhang Xiao

Engineer, Web development

Ethos Tech.
http://www.ethos.com.cn
-------------- 下一部分 --------------
Ò»¸öHTML¸½¼þ±»ÒƳý...
URL: http://python.cn/pipermail/python-chinese/attachments/20070713/8c5af5aa/attachment.html 

[导入自Mailman归档:http://www.zeuux.org/pipermail/zeuux-python]

2007年07月13日 星期五 16:10

longlong asfnuts在gmail.com
星期五 七月 13 16:10:15 HKT 2007

ѧϰÁË

ÔÚ07-7-13£¬Xell Zhang <xellzhang在gmail.com> дµÀ£º
>
> ÎÒ×Ô¼º·â×°ÁËÒ»¸öÀàÓÃÀ´´¦Àísql¡£
> class MySQLConnector:
>     ''' MySQLConnector holds a connection with a MySQL database
>
>     MySQLConnector instance creates a connection by using
>     parameters passed. The connection will be hold by this
>     instance. When a sql is run this instance will generate
>     a cursor and use it to query. After query cursor will
>     be closed.'''
>
>     def __init__(self, host, port, user, passwd, db):
>         self.con = MySQLdb.connect(
>             host = host,
>             port = port,
>             user = user,
>             passwd = passwd,
>             db = db
>         )
>         self.lock = threading.Lock ()
>
>     def query(self, sql, args = ()):
>         self.lock.acquire()
>         cursor = self.con.cursor()
>         cursor.execute(sql, args)
>         result = cursor.fetchall()
>         cursor.execute ('commit')
>         cursor.close()
>         self.lock.release()
>         return result
>
> ¿ÉÒÔÕâÑùʹÓãº
> dbCon = MySQLConnector('yourhost', 3306, 'username', 'passwd', 'dbname')
> dbCon.query('SELECT * FROM user WHERE username = %s', ('xzhang'))
> dbCon.query('INSERT INTO user (username, age) VALUES (%s, %s)', ('xzhang',
> 23))
>
> ÎÒ¸Õ¿ªÊ¼Ñ§Ï°£¬²»ÖªµÀÎÒµÄ×ö·¨ÊÇ·ñºÃ¡£ÐèҪעÒâµÄÊDz»¹ÜÄã´«ÈëµÄÊý¾ÝÊÇʲôÀàÐÍ£¬¶¼ÓÃ%s¡£
>
> On 7/13/07, longlong <asfnuts在gmail.com> wrote:
> >
> > ÓÐʱÔÚpythonÒªÖ´ÐÐsqlÓï¾ä£¬²ÎÊýÀàÐÍÊÇ×Ö·û´®£¬ÎÒÏëÔÚÕâ¸ö×Ö·û´®ÖмÓÈë±äÁ¿£¬Ôõô×ö£¿
> > ÔÚ07-7-13£¬Xell Zhang <xellzhang在gmail.com> дµÀ£º
> > >
> > > ÊDz»ÊÇ
> > > print 'hello, %s' % person.name
> > > Õâ¸öÑù×Ó£¿
> > >
> > > On 7/11/07, ÉòáË <wileishn在gmail.com > wrote:
> > > >
> > > > Ruby Sucks, PEP292 says.
> > > >
> > > > ÔÚ 07-7-11£¬longlong< asfnuts在gmail.com> дµÀ£º
> > > > > ×Ö·û´®ÖеıäÁ¿Ìæ»»
> > > > >
> > > > >  PHP 4 Ìí¼ÓÁËÒ»¸öеĻúÖÆÒÔÌæ»»×Ö·û´®ÖеıäÁ¿¡£ÄúÏÖÔÚ¿ÉÒÔ·ÃÎÊ×Ö·û´®ÖеĶÔÏó³ÉÔ±±äÁ¿ºÍ¶àάÊý×éÔªËØ¡£
> > > > >
> > > > >  Èç¹ûÄúÏëÕâô×ö£¬ÄúÐèҪʹÓôóÀ¨ºÅ½«Ò»¸öÃÀÔª·ûºÅºÍ±äÁ¿ÃûÀ¨ÆðÀ´£¬¾ÍÏñÕâÑù£º{$...}¡£
> > > > >
> > > > >  ÒªÔÚ×Ö·û´®ÖÐǶÈë¶ÔÏó³ÉÔ±±äÁ¿£¬Ö»Òª¼òµ¥µÄд "text {$obj->member} text"¡£¶øÔÚ PHP 3 ÖУ¬ÄúµÃʹÓÃ
> > > > "text
> > > > > ".$obj->member." text"¡£
> > > > >
> > > > >  Õâ¸ö¸Ä½ø¿ÉÒÔʹ´úÂë¸üÒ׶Á£¬µ«Õâ¿ÉÄÜ»áÆÆ»µÓà PHP 3 дµÄ´úÂë¡£µ«ÊÇÄúºÜÈÝÒ×ÐÞÕýËü£ºÔÚ´úÂëÖвéÕÒ×Ö·û´®ÖеÄ
> > > > {$£¬Ê¹ÓÃËÑË÷Ìæ»»¹¤¾ß½«ËüÌ滻Ϊ
> > > > > \{$¡£
> > > > >
> > > > >
> > > > >
> > > > >
> > > > > ÉÏÃæÊÇphpµÄ·½Ê½¡£
> > > > >
> > > > > _______________________________________________
> > > > > python-chinese
> > > > > Post: send python-chinese在lists.python.cn
> > > > > Subscribe: send subscribe to
> > > > > python-chinese-request在lists.python.cn
> > > > > Unsubscribe: send unsubscribe to
> > > > > python-chinese-request在lists.python.cn
> > > > > Detail Info:
> > > > > http://python.cn/mailman/listinfo/python-chinese
> > > > >
> > > >
> > > > ÄäÃûѧϰ Python ÖÐ ...
> > > > _______________________________________________
> > > > python-chinese
> > > > Post: send python-chinese在lists.python.cn
> > > > Subscribe: send subscribe to python-chinese-request在lists.python.cn
> > > > Unsubscribe: send unsubscribe to  python-chinese-request在lists.python.cn
> > > > Detail Info: http://python.cn/mailman/listinfo/python-chinese
> > >
> > >
> > >
> > >
> > > --
> > > Zhang Xiao
> > >
> > > Engineer, Web development
> > >
> > > Ethos Tech.
> > > http://www.ethos.com.cn
> > > _______________________________________________
> > > python-chinese
> > > Post: send python-chinese在lists.python.cn
> > > Subscribe: send subscribe to python-chinese-request在lists.python.cn
> > > Unsubscribe: send unsubscribe to
> > > python-chinese-request在lists.python.cn
> > > Detail Info: http://python.cn/mailman/listinfo/python-chinese
> > >
> >
> >
> > _______________________________________________
> > python-chinese
> > Post: send python-chinese在lists.python.cn
> > Subscribe: send subscribe to python-chinese-request在lists.python.cn
> > Unsubscribe: send unsubscribe to
> > python-chinese-request在lists.python.cn
> > Detail Info: http://python.cn/mailman/listinfo/python-chinese
> >
>
>
>
> --
> Zhang Xiao
>
> Engineer, Web development
>
> Ethos Tech.
> http://www.ethos.com.cn
>
> _______________________________________________
> python-chinese
> Post: send python-chinese在lists.python.cn
> Subscribe: send subscribe to python-chinese-request在lists.python.cn
> Unsubscribe: send unsubscribe to  python-chinese-request在lists.python.cn
> Detail Info: http://python.cn/mailman/listinfo/python-chinese
>
-------------- 下一部分 --------------
Ò»¸öHTML¸½¼þ±»ÒƳý...
URL: http://python.cn/pipermail/python-chinese/attachments/20070713/5229679a/attachment.htm 

[导入自Mailman归档:http://www.zeuux.org/pipermail/zeuux-python]

2007年07月16日 星期一 14:41

Leo Jay python.leojay在gmail.com
星期一 七月 16 14:41:33 HKT 2007

On 7/13/07, Xell Zhang <xellzhang在gmail.com> wrote:
> 我自己封装了一个类用来处理sql。
> class MySQLConnector:
>     ''' MySQLConnector holds a connection with a MySQL database
>
>     MySQLConnector instance creates a connection by using
>     parameters passed. The connection will be hold by this
>     instance. When a sql is run this instance will generate
>     a cursor and use it to query. After query cursor will
>     be closed.'''
>
>     def __init__(self, host, port, user, passwd, db):
>         self.con = MySQLdb.connect(
>             host = host,
>             port = port,
>             user = user,
>             passwd = passwd,
>             db = db
>         )
>         self.lock = threading.Lock ()
>
>     def query(self, sql, args = ()):
>         self.lock.acquire()
>         cursor = self.con.cursor()
>         cursor.execute(sql, args)
>         result = cursor.fetchall()
>         cursor.execute ('commit')
>         cursor.close()
>         self.lock.release()
>         return result
>
> 可以这样使用:
> dbCon = MySQLConnector('yourhost', 3306, 'username', 'passwd', 'dbname')
> dbCon.query('SELECT * FROM user WHERE username = %s', ('xzhang'))
> dbCon.query('INSERT INTO user (username, age) VALUES (%s, %s)', ('xzhang',
> 23))
>
> 我刚开始学习,不知道我的做法是否好。需要注意的是不管你传入的数据是什么类型,都用%s。
>


这样搞很容易被注入吧?

-- 
Best Regards,
Leo Jay

[导入自Mailman归档:http://www.zeuux.org/pipermail/zeuux-python]

2007年07月16日 星期一 17:45

leopay leopay在gmail.com
星期一 七月 16 17:45:05 HKT 2007

Èç¹ûÊÇÕâÑù×Óд
cursor.execute(sql, args)
ÊDz»»á×¢ÈëµÄ£¬Ëü»á×Ô¶¯°ÑargsÖÐתÒåµô
-------------- 下一部分 --------------
Ò»¸öHTML¸½¼þ±»ÒƳý...
URL: http://python.cn/pipermail/python-chinese/attachments/20070716/9f1729ec/attachment.htm 

[导入自Mailman归档:http://www.zeuux.org/pipermail/zeuux-python]

如下红色区域有误,请重新填写。

    你的回复:

    请 登录 后回复。还没有在Zeuux哲思注册吗?现在 注册 !

    Zeuux © 2025

    京ICP备05028076号