python pumysql存储

2019-02-27 11:54:08来源:博客园 阅读 ()

新老客户大回馈,云服务器低至5折

 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()

 


原文链接:https://www.cnblogs.com/ymkdxw/p/10436865.html
如有疑问请与原作者联系

标签:

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有

上一篇:python(leetcode)-14最长公共前缀

下一篇:分配权限