has been using TORNDB and sqlalchemy, this time I pay attention to the next mysqldb, I feel that if you know more about T-SQL, or with mysqldb more flexible, and very simple, isn't it? All of its operations are done with one execute () method.
code is as follows |
copy code |
#-*-coding:utf-8-*- Import mysqldb conn = MySQLdb.connect (host= ' localhost ', db= ' MyData ', user= ' root ', passwd= ' root ', charset= ' UTF8 ') cursor = conn.cursor () #添加数据, other modifications, deletions are also done as follows sql = ' INSERT into authors (email, name) VA Lues (%s,%s) ' param = (' hello@17python.com ', ' 17python ') N = cursor.execute (sql, param) print n #查询数据 sql = ' SELECT email,name from authors ' Cursor.execute (SQL) to row in Cursor.fetchall (): print ' Email:%s-name:%s '% (Row[0], row[1]) Cursor.close () Conn.commit () #提交事物, Be sure to write to Conn.close () |