Skip to content

操作MySQL数据库

python
import pymysql

查询

python
    def queryByVRId(self,vrid):
        db = pymysql.connect(host='192.168.1.181',
                             user='root',
                             password='ctu800617',
                             database='vr_product_apply')
        cursor = db.cursor()
        sql = '''select * from tbl_vrlist where vrid = %s'''%(str(vrid))
        try:
            cursor.execute(sql)
            return cursor.fetchall() # 查询所有
        except:
            print("Error: unable to fetch data")
        db.close()

插入

python
    def insertCertificate(self,imageName,path):
        sql = '''INSERT INTO tbl_certificate(`imageName`,`path`)VALUES('%s','%s');'''%(imageName,path)
        # print(sql)
        db = pymysql.connect(host='192.168.1.181',
                             user='root',
                             password='ctu800617',
                             database='vr_product_apply')
        cursor = db.cursor()
        try:
            cursor.execute(sql)
            db.commit()
        except:
            db.rollback()
        db.close()