python ddt mysql_Python自动化测试系列[v1.0.0][数据驱动DDTMYSQL]

# encoding = utf-8

"""

__title__ = ''

__author__ = 'davieyang'

__mtime__ = '2018/4/21'

"""

import pymysql

from TestData.SqlScripts import create_table

from TestData.SqlScripts import create_database

from TestData.SqlScripts import drop_table

class MySQL(object):

def __init__(self, host, port, dbName, username, password, charset):

self.conn = pymysql.connect(

host=host,

port=port,

db=dbName,

user=username,

password=password,

charset=charset

)

self.cur = self.conn.cursor()

def create(self):

try:

self.cur.execute(create_database)

self.conn.select_db("davieyang")

self.cur.execute(drop_table)

self.cur.execute(create_table)

'''

cur.execute("drop database if exists davieyang") #如果davieyang数据库存在则删除

cur.execute("create database davieyang") #新创建一个数据库davieyang

cur.execute("use davieyang") #选择davieyang这个数据库

# sql 中的内容为创建一个名为testdata的表

sql = """create table testdata(id BIGINT,name VARCHAR(20),age INT DEFAULT 1)""" #()中的参数可以自行设置

conn.execute("drop table if exists testdata") # 如果表存在则删除

conn.execute(sql)# 创建表

# 删除

# conn.execute("drop table testdata")

conn.close()# 关闭游标连接

connect.close()# 关闭数据库服务器连接 释放内存

'''

except pymysql.Error as e:

raise e

else:

self.cur.close()

self.conn.commit()

self.conn.close()

print(u"创建数据库和表成功")

def insertDatas(self):

try:

sql = "insert into testdata(bookname, author) values(%s, %s);"

self.cur.executemany(sql, [('selenium xml DataDriven', 'davieyang'),

('selenium excel DataDriven', 'davieyang'),

('selenium ddt data list', 'davieyang')])

except pymysql.Error as e:

raise e

else:

self.conn.commit()

print(u"初始数据插入成功")

self.cur.execute("select * from testData;")

for i in self.cur.fetchall():

print(i[1], i[2])

self.cur.close()

self.conn.close()

def getDataFromDataBase(self):

# 从数据库中获取数据

# bookname作为搜索关键词,author作为期望结果

self.cur.execute("select bookname, author from testdata;")

# 从查询区域取回所有查询结果

dataTuple = self.cur.fetchall()

return dataTuple

def closeDataBase(self):

# 数据库清理

self.cur.close()

self.conn.commit()

self.conn.close()

if __name__ == "__main__":

db = MySQL(

host="localhost",

port=3306,

dbName="davieyang",

username="root",

password="root",

charset="utf8"

)

print(db.getDataFromDataBase())

db.closeDataBase()

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值