pyMysql模块
Posted 嗯姆嗯姆嗯姆
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了pyMysql模块相关的知识,希望对你有一定的参考价值。
# pip3 install pymysql import pymysql client=pymysql.connect( host=‘127.0.0.1‘, port=3306, user=‘root‘, password=‘‘, database=‘db5‘, charset=‘utf8‘ ) cursor=client.cursor() sql=‘insert into t1 values(1,"egon"); ‘ try: res=cursor.execute(sql) # res=cursor.execute(sql) # res=cursor.execute(sql) # res=cursor.execute(sql) print(res) client.commit() except Exception: client.rollback() cursor.close() client.close() # 基于pymysql模块的增删改 # pip3 install pymysql import pymysql client=pymysql.connect( host=‘127.0.0.1‘, port=3306, user=‘root‘, password=‘‘, database=‘db5‘, charset=‘utf8‘ ) cursor=client.cursor() # 增 # userinfo=[ # (3,"alex"), # (4,"lxx"), # (5,"xxx") # ] # # for user in userinfo: # sql=‘insert into t1 values(%s,"%s"); ‘%(user[0],user[1]) # print(sql) # cursor.execute(sql) # 增简便方法 # userinfo=[ # (3,"alex"), # (4,"lxx"), # (5,"xxx") # ] # sql=‘insert into t1 values(%s,%s);‘ # cursor.executemany(sql,userinfo) # 删 # cursor.execute(‘delete from t1 where id =3;‘) # 改 # sql=‘alter table t1 add age int; ‘ sql=‘alter table t1 modify name varchar(20); ‘ cursor.execute(sql) client.commit() cursor.close() client.close() # 基于mysql模块的查询 import pymysql client=pymysql.connect( host=‘127.0.0.1‘, port=3306, user=‘root‘, password=‘‘, database=‘db5‘, charset=‘utf8‘ ) cursor=client.cursor() inp_user=input(‘输入账号:‘).strip() inp_pwd=input(‘输入密码:‘).strip() # sql=‘select id from user where name="%s" and pwd=password("%s");‘%(inp_user,inp_pwd) sql=‘select id from user where name="%s" and pwd="%s";‘%(inp_user,inp_pwd) print(sql) rows=cursor.execute(sql) #rows行数 如果有证明存在 if rows: print(‘