zoukankan      html  css  js  c++  java
  • python pymysql存储

     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()
  • 相关阅读:
    vux 数据模拟mockjs的使用
    vux 配置颜色问题
    vue-router 学习
    vue 学习笔记
    点击加载更多
    table td 固定宽度
    js scroll 滚动连续多次触发事件只执行一次
    Merge into的注意点之ORA-30926: 无法在源表中获得一组稳定的行?
    js页面中取值的注意点
    insert into的方式
  • 原文地址:https://www.cnblogs.com/ymkdxw/p/10436865.html
Copyright © 2011-2022 走看看