1 # _*_ coding:utf-8 _*_ 2 import requests 3 from bs4 import BeautifulSoup 4 import re 5 import pymysql 6 7 def create(): 8 db = pymysql.connect("localhost", "root", "111111", "aoyang") # 连接数据库 9 cursor = db.cursor() 10 cursor.execute("DROP TABLE IF EXISTS aoy")#查询是否有相同的表,如果有删除,重新建表 11 sql = """CREATE TABLE `aoy` ( 12 `id` int(10) NOT NULL AUTO_INCREMENT , 13 `name` varchar(50) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL , 14 `Price` decimal(10,2) NULL DEFAULT NULL , 15 `spec` varchar(50) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL , 16 `Manufactor` varchar(50) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL , 17 PRIMARY KEY (`id`) 18 )""" 19 cursor.execute(sql) 20 db.close() 21 22 def insert(value): 23 db = pymysql.connect("127.0.0.1", "root", "111111", "aoyang") 24 cursor = db.cursor() 25 sql = "INSERT INTO aoy(name,Price,spec,Manufactor) VALUES (%s, %s, %s, %s)" 26 try: 27 cursor.execute(sql, value) 28 db.commit() 29 print('插入数据成功') 30 except: 31 db.rollback() 32 print("插入数据失败") 33 #cursor.execute(sql, value) 34 #db.commit() 35 db.close() 36 37 38 def aa(): 39 ddd = '哈' 40 i = [ddd, '22.22', 'kkk','方法'] 41 q = ['sdasdas', '22.22', 'kkk','问'] 42 43 insert(q) 44 45 insert(i) 46 create() 47 aa()