侧边栏壁纸
  • 累计撰写 15 篇文章
  • 累计创建 3 个标签
  • 累计收到 0 条评论

目 录CONTENT

文章目录

关于python pymysql应用

天马
2021-04-06 / 0 评论 / 0 点赞 / 224 阅读 / 214 字 / 正在检测是否收录...
# coding:utf-8
import pymysql


class MysqlUtil(object):
    def __init__(self):
	# 获取数据库连接
        self.db = pymysql.connect('192.168.18.183', 'root', 'root', 'ygp_sms')
	#指定查询 返回类型
        self.cursor = self.db.cursor(pymysql.cursors.DictCursor)


    def execute(self, sql):
        print('执行数据了')
        return self.cursor.execute(sql)

    def save(self, save):
        i = 1
        for s in save:
            try:
                sql = "insert into demomsg01(phone,y,n,time ,s ) values ('"+s['phone']+"','"+s['y']+"','"+s['n']+"','"+s['time']+"','"+s['s']+"')"
                self.execute(sql)
                self.db.commit()
                s1 = '保存了 %d 条数据了' %i
                print(s1)
                i = i + 1
            except BaseException as e:
                print(e)

    def save02(self, save):
        i = 1
        for s in save:
            try:
                sql = "insert into demomsg02(phone,y,n,time ,s ) values ('" + s['phone'] + "','" + s['y'] + "','" + s[
                    'n'] + "','" + s['time'] + "','" + s['s'] + "')"
                self.execute(sql)
                self.db.commit()
                s1 = '保存了 %d 条数据了' % i
                print(s1)
                i = i + 1
            except BaseException as e:
                print(e)
      def query(self, sql):
	 #查询方法
        return self.cursor.execute(sql)




0

评论区