Mysql类的基本方法举例:
# -*- coding:utf-8 -*-
__author__ = 'Administrator'
import time, MySQLdb
# 连接数据库
conn=MySQLdb.connect(host="localhost",user="root",passwd="root",db="test",charset="utf8")
cursor = conn.cursor()
# 删除表
sql = "drop table if exists user"
cursor.execute(sql)
# 创建
sql = "create table if not exists user(name varchar(128) primary key, created int(10))"
cursor.execute(sql)
# 写入
sql = "insert into user(name,created) values(%s,%s)"
param = ("aaa",int(time.time()))
n = cursor.execute(sql,param)
print 'insert',n
# 写入多行
sql = "insert into user(name,created) values(%s,%s)"
param = (("bbb",int(time.time())), ("ccc",33), ("ddd",44) )
n = cursor.executemany(sql,param)
print 'insertmany',n
# 更新
sql = "update user set name=%s where name='aaa'"
param = ("zzz")
n = cursor.execute(sql,param)
print 'update',n
# 查询
n = cursor.execute("select * from user")
for row in cursor.fetchall():
print row
for r in row:
print r
# 删除
sql = "delete from user where name=%s"
param =("bbb")
n = cursor.execute(sql,param)
print 'delete',n
# 查询
n = cursor.execute("select * from user")
print cursor.fetchall()
cursor.close()
# 提交
conn.commit()
# 关闭
conn.close()
使用举例:
1、获取数据库验证码的方法:(例子:获取注册验证码)
def register_ma(self, phone2):
conn = MySQLdb.connect(host="192.168.1.58", user="tzgqa154", passwd="123456", db="tzgqa154", port=3310, charset="utf8")
cursor = conn.cursor()
url3 = "select vcContent from tbsmsrecord where vcPhone = %s ORDER BY id desc LIMIT 0,1;"\
% (phone2)
url4 = "select vcContent from tbsmssend where vcPhone = %s ORDER BY id desc LIMIT 0,1;" \
% (phone2)
cursor.execute(url4)
url_4 = cursor.fetchall()
if url_4 == ():
cursor.execute(url3)
url_3 = cursor.fetchall()
show_3 = url_3[0][0]
return str(show_3[11:17])
else:
show_4 = url_4[0][0]
return str(show_4[11:17])
2、在利用接口进行重复投资获得大量加息数据时应用(例子:5000条加息券投资记录)
# -*- coding:utf-8 -*-
__author__ = 'Administrator'
import requests
import time
import MySQLdb
class Cg_Invest_sub:
def ticketid(self, ilogid):
conn = MySQLdb.connect(host="192.168.1.58", user="tzgqa154", passwd="123456", db="tzgqa154", port=3310,
charset="utf8")
cursor = conn.cursor()
url = "SELECT id FROM rewardinterestgrant where iLoginAccountId = %s and iState = 1 ORDER BY id desc;" % (ilogid)
cursor.execute(url)
url_1 = cursor.fetchall()
show_1 = url_1[0][0]
return int(show_1)
def cg_002(self):
headers = {
"Content-Type":"application/json",
"token": "MTIwNTUwNzoxNTA3NjE3MDQyODU1OmE2ZGFjNGFkMGQ4Y2UwOTRmMTM0Yzk2ZDA1YmI3NDI2",
"plat": "ios",
'Cache-Control': 'no-cache',
'Host': 'testrest2.tongmi.org'
}
m_url = "https://testrest2.tongmi.org/tzgCredit/investor/asset/investJx/invest"
data = "{'subjectId':17305,'investAmt':'200.00','agreeProtocol':'ture'," \
"'investType':'1','isBidAuth': 'ture','payPassword':" \
" 'Byf3tli1nk/1FVaD/MfSeUwCo089ioTi6TQ3sn/DRrmQZDC0quz2dfDZi/iifPf/OJOjMSMuMe4Ok4dFEi" \
"jOvnRfvvlnSctG5tkXhzlF228tphngGGA1HkdVSgOW5kjvyIFbTviUv2jgu/klaZ2Zw4Vf6CwQOx91j" \
"E4uDfHZEUo='}"
res = requests.post(m_url, data=data, headers=headers)
# print (res.url)
print "普通标投资结果:",res.text
if __name__ == '__main__':
mmm = Cg_Invest_sub()
for i in range(5000):
mmm.cg_002()
使用Mysql类可以灵活得操作获取数据库数据,用以测试过程使用,这是一个值得学习的方法!!