selenium

数据库如下:

直接上代码,如下:

 1 import MySQLdb   # 导入数据库包
 2 
 3 conn = MySQLdb.connect(
 4     host='fhdskhaf.aliyuncs.com',   # 连接服务器
 5     port=3306,  # 端口号,默认3306的话可以省略这一行
 6     user='zhangsan', # 用户名
 7     passwd='fhdkasjf',  # 密码
 8     db='datav', # 数据库名
 9     charset='utf8'  # 编码方式(字符集)
10 )
11 
12 c = conn.cursor()  # 创建游标
13 
14 # 查询每一行
15 c.execute('select * from zy_1')
16 for i in range(c.rowcount):
17     row = c.fetchone()
18     print(row)
19     if row[1] == 'shuxue':    # row[1]取出每行数据中的某一列,下标从0开始
20         print('ok')
21         break
22 
23 c.close()    # 关闭游标
24 conn.close()  # 关闭数据库连接

增删改查,分别如下(增删改需要使用commit()):

#
c.execute("insert into zy_1(name, score) VALUES('yuwen', 99), ('shuxue', 89)")
conn.commit()
#
c.execute("delete from zy_1 where id > 2")
conn.commit()
#
c.execute("update zy_1 set score = 66 where id = 1")
conn.commit()
#
c.execute('select * from zy_1')
rows = c.fetchall()
print(rows)

三种查询方法:

1 c.fetchone()    # 查询一行
2 c.fetchmany(2)  # 查询2行
3 c.fetchall()    # 全部查询出来
原文地址:https://www.cnblogs.com/xiaochongc/p/12636749.html