python 使用mysql数据库

# 获取db和cursor
def GetDatabaseAndCurSor():
	# 数据库需要提前创建好
    db = MySQLdb.connect("localhost", 'root', '123456', 'video', charset='utf8')
    cursor = db.cursor()
    return db, cursor
# 非增删改查操作
sql = 'drop table webPage'
cursor.execute(sql)
sql = 'create table webPage(name varchar(50),href varchar(50))'
cursor.execute(sql)
# 查询
cursor.execute("select * from webPage where href='%s'" % href)
results = cursor.fetchall() # cursor.fetchone()
for res in results: # 遍历每一行
	print(res[0]) # 第0列
# 增删改需要commit()
cursor.execute("update mp4url set deny=0,downloaded=1 where href='%s'" % url)
db.commit()
db.close()

你可能感兴趣的:(python,mysql)