查询 select
#!/usr/bin/env python
#-*- coding: utf-8 -*-
import httplib
import json
import requests
import MySQLdb
from datetime import datetime
#打开数据库连接
db = MySQLdb.connect("localhost",'root','root','db_basename',charset='utf8')
db.set_character_set('utf8')
'''
cur = db.cursor()
name = 'Wap端大咖购买'
status = 'paid'
num = cur.execute('select * from cash_orders where title=%s and status=%s',(name,status))
#num = cur.execute('select wx_follows.ruhr_openid from cash_orders left join user on cash_orders.userId = user.id left join wx_follows on user.unionid = weixin_follows.unionid where cash_orders.title =%s and cash_orders.status = %s and wx_follows.ruhr_openid is not null GROUP BY wx_follows.ruhr_openid',(name,status))
info = cur.fetchmany(num)
#遍历
cur = db.cursor() #num = cur.execute('select * from cash_orders where title=%s and status=%s',(name,status)) num = cur.execute('select *from push_wx_course_msg') info = cur.fetchmany(num) for row in info: id = row[0] course_id = row[1] lesson_title = row[2] push_user_id = row[3] push_openid = row[4] create_date = row[5] #打印数据 print 'id = %d,course_id=%d,lesson_title=%s,push_user_id=%d,push_openid=%s,create_date=%s' % \ (id,course_id,lesson_title,push_user_id,push_openid,create_date)
插入 insert (记得执行后 还要提交commit() )
cursor = db.cursor()
sql = "INSERT INTO push_wx_course_msg(course_id,lesson_title,push_user_id,push_openid,create_date) VALUES ('%d', '%s', '%d', '%s', '%s' )" % (1,title,1,openid,current_time)
#print(sql)
cursor.execute(sql)
db.commit()
更新 update (记得执行后 还要提交commit() )
cursor = db.cursor()
sql = "update push_wx_course_msg set lesson_title ='%s' where id = '%d'" % ('测试大咖update',3)
#print(sql)
cursor.execute(sql)
db.commit()
删除 delete
cursor = db.cursor()
sql = "delete from push_wx_course_msg where id = '%d'" % (2)
#print(sql)
cursor.execute(sql)
db.commit()