1. 和数据库建立连接
adbapi.ConnectionPool
conn=adbapi.ConnectionPool("MySQLdb",host=MYSQLCONFIG["host"],user=MYSQLCONFIG["user"],passwd=MYSQLCONFIG["passwd"],charset=MYSQLCONFIG["charset"],port=3306)
2.执行异步SQL查询
conn.runQuery
result=yield conn.runQuery("select ip,tid from yw_cap.task_task WHERE tid=%s",(tid,))
其返回的结果相当于MySQLdb 的fetchall的结果
3. 执行异步SQL 增删改
conn.runOperation
yield conn.runOperation("update yw_cap.task_task set name=%s, project=%s, app=%s,ip=%s,svnpath=%s,version=%s,svnuser=%s, svnpasswd=%s,info=%s,type=%s,args=%s WHERE tid=%s", (name,project,app,ip,svnpath,svnversion,svnuser,svnpasswd,info,_type,args,tid))
返回一个数字, 代表本次SQL操作影响的行数
4. 执行自定义的数据库操作
conn.runInteration
def operate_db(cursor,args): cursor.execute('''insert into yw_cap.task_task(name, project, app, ip, addtime, edittime, status, svnpath, version, svnuser, svnpasswd, info, owner, type, args) VALUES(%s,%s,%s,%s,UNIX_TIMESTAMP(),UNIX_TIMESTAMP(),0,%s,%s,%s,%s,%s,%s,%s,%s)''',args) return cursor.lastrowid conn=adbapi.ConnectionPool("MySQLdb",host=MYSQLCONFIG["host"],user=MYSQLCONFIG["user"],passwd=MYSQLCONFIG["passwd"],charset=MYSQLCONFIG["charset"],port=3306) tid=yield conn.runInteraction(operate_db,(name,project,app,ip,svnpath,svnversion,svnuser,svnpasswd,info,owner,_type,args))