数据库

import pymysql

#ip port user password db

#118.24.3.40 3306 jxz 123456 jxz

conn = pymysql.connect(host='118.24.3.40',user='jxz',
password='123456',db='jxz',port=3306,
charset='utf8',autocommit=True)#建立连接
cur = conn.cursor(pymysql.cursors.DictCursor) #建立游标
# sql = "insert1 app_student ( `name`, `sex`, `age`, `addr`, `grade`, `phone`, `gold`) values" \
# " ( '小黑', '男', '18', '河南省济源市北海大道32号', '天蝎座', '18612539443', '100');"



操作数据库的函数
import pymysql

def op_mysql(host,user,password,db,sql,port=3306,many_tag=False):
conn = pymysql.connect(host=host,user=user,password=password,
db=db,port=port,charset='utf8',
autocommit=True)
cur = conn.cursor(pymysql.cursors.DictCursor)
cur.execute(sql)
if many_tag:
result = cur.fetchall()
else:
result = cur.fetchone()
cur.close()
conn.close()
return result

# sql2 = 'select * from app_student where phone="18612539443"';
sql2 = 'select * from app_student limit 4';

cur.execute(sql2)#只是帮你执行sql语句
print(cur.fetchall()) #取到所有的数据
# print(cur.fetchone()) #只取一条
# print(cur.fetchmany(10)) #取几条
cur.close()
conn.close()
for  d in cur:#直接循环游标,就是获取数据库的每行数据
print(d)
for d,d2 in zip(cur,cur2):
if d!=d2:
print('%s和%s不一样'%(d,d2))



import pymysql,hashlib
conn = pymysql.connect(host='118.24.3.40', user='jxz', password='123456',
                       db='jxz', charset='utf8')
#cnz

cur = conn.cursor()

cur2 = conn.cursor()

cur.execute('select * from jxz.app_myuser;')

cur2.execute('select * from cnz.app_myuser_new;')

#1、先从两个表2里查出来数据,然后吧它md5一下子,对比
for d,d2 in zip(cur,cur2):
    if d!=d2:
        print('%s和%s不一样'%(d,d2))




# result1 = cur.fetchall()
# result2 = cur2.fetchall()
#
#
# print(set(result1) ^ set(result2) )

# cur.close()

 

posted on 2019-06-27 14:24  静思的技术博客  阅读(103)  评论(0编辑  收藏  举报

导航