《永劫無間》迦南面板展示及獲取途徑
阿新 • • 發佈:2021-07-19
import pymysql
def insertOne(result):
# 連線資料庫
db = pymysql.Connect(
host='121.196.103.249',
port=33306,
user='root',
passwd='123456',
db='spider',
charset='utf8'
)
# 獲取遊標 cursor = db.cursor() try: # 插入資料 sql = """insert into test(name,age) values (%s,%s) """ cursor.execute(sql, (result["name"], result["age"])) except: print("異常") # 關閉連線 db.commit() cursor.close() db.close()
def insertMany(result):
# 連線資料庫
db = pymysql.Connect(
host='121.196.103.249',
port=33306,
user='root',
passwd='123456',
db='spider',
charset='utf8'
)
# 獲取遊標 cursor = db.cursor() try: # 插入多條資料 方式一 # sql = """insert into test(age,name) values (%s,%s) """ # data=((x["name"],x["age"]) for x in result) # cursor.executemany(sql,data) # 插入多條資料 方式二 sql = """insert into test(age,name) values (%s,%s) """ for x in result: cursor.execute(sql, (x["name"], x["age"])) except: print("資料庫異常") # 關閉連線 db.commit() cursor.close() db.close()
def select():
# 連線資料庫
db = pymysql.Connect(
host='121.196.103.249',
port=33306,
user='root',
passwd='123456',
db='spider',
charset='utf8'
)
# 獲取遊標 cursor = db.cursor() try: sql = "select * from test" cursor.execute(sql) # 獲取所有記錄列表 results = cursor.fetchall() except: print("異常") db.close() return results
def update(result):
# 連線資料庫
db = pymysql.Connect(
host='121.196.103.249',
port=33306,
user='root',
passwd='123456',
db='spider',
charset='utf8'
)
# 獲取遊標
cursor = db.cursor()
try:
sql = """UPDATE test set age=%s where name=%s"""
data=(result["age"],result["name"])
cursor.execute(sql,data)
except:
print("異常")
# 關閉連線
db.commit()
cursor.close()
db.close()
if name == 'main':
# result={"name":"haha","age":"18"}
# insertOne(result)
# print("插入成功")
# result=[{"name":"haha","age":"18"},{"name":"haha2","age":"19"}]
# insertMany(result)
# print("插入成功")
result={"name":"haha","age":"100"}
update(result)
# result=select()
# print(1)