python-mysql链接脚本

 #coding:utf-8
import pymysql
import datetime

class face_mysql:
    def __init__(self):
        pass
    #设置数据库和密码
    def conn_mysql(self):
        # db = pymysql.connect(host='localhost', port=3306, user='root', passwd='root', db='mysql', charset="utf8")
        db = pymysql.connect(host='172.20.71.35', port=3306, user='root', passwd='', db='mysql', charset="utf8")
        return db

    def insert_facejson(self,pic_name,pic_json,uid,ugroup):
    # def insert_facejson(self, pic_name, pic_json):
        db = self.conn_mysql()
        cursor = db.cursor()
        dt = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
        # sql = "insert into face_json(json,pic_name,date,state) values('%s' ,'%s','%s','%d') ;" % (
        #     pic_json, pic_name, dt, 1)
        sql = "insert into face_json(json,pic_name,date,state,uid,ugroup) values('%s' ,'%s','%s','%d','%s','%s') ;" % (
            pic_json, pic_name, dt, 1, uid, ugroup)
        # print("sql=",sql)
        try:
            #执行sql语句
            cursor.execute(sql)
            #提交给数据库执行
            lastid = int(cursor.lastrowid)
            db.commit()
        except:
            #Rollback in case there is any error
            db.rollback()
        db.close()
        return lastid

    def findall_facejson(self,ugroup):
        db = self.conn_mysql()
        cursor = db.cursor()
        sql = "select * from face_json where state=1 and ugroup=%s;"%(ugroup)
        try:
            cursor.execute(sql)
            results = cursor.fetchall()
            return results
        except:
            print("Error:unable to fetch data")
        db.close()


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值