import pymysql
host = "110.40.129.50"
username="jxz"
password="123456"   #字符串
db="jxz"  #数据库
port=3306  #端口号  默认  int
charset="utf8"  #字符集
#一句话来说就是让计算机认识各种字符 英文  汉语等等。。。ascii码
#gb2312  国标
#gbk  中国计算机认识了
#unicode 万国码
#utf-8
conn=pymysql.connect(host=host,user=username,
passwd=password,db=db,port=port,charset=charset)#连接数据库  autocmmit=True自动提交到数据库
cur=conn.cursor()#建立游标
#cur=conn.cursor(pymysql.cursors.DictCursor)#指定游标结果变成字典
sql="select * from students3_info;"
cur.execute(sql)#执行sql语句
print(cur.fetchall())#所有的结果   #结果返回的是二维数组
# print(cur.fetchone())#获取一条数据结果,确定是一条可以用,唯一的那种
# for c in cur:#执行循环游标,也是可以获取所有数据,数据量大的时候
#    print(c)   #循环一次一条
# print(cur.fetchmany(10))  想获取几条就获取几条
# print(cur.rowcount)  查看查询多少条
cur.close()  #关掉游标
conn.close() #关掉连接
#登录,格式化方式
uname=input("username:")
# sql= "select * from students3_info where name ='%s'"% uname   #sql自己拼好的,格式化好了,int类型加引号
# print(sql)未执行1
#cur.execute(sql.[uname,age])
#sql= "select * from students3_info where name =%s and age =%s;"  #%s变量,自动就加引号了
# print(sql)未执行2  可以防sql注入(安全攻击的一种手段)
# insert_sql = "insert into students3_info (name,age,password) values ('周周',18,'sdfsfsfd');"
# cur.execute(insert_sql) #执行aql
# conn.commit()#提交数据库
# conn.rollback()#回滚
# print(cur.fetchall())#所有的结果
# cur.close()
# conn.close()
# update_sql = "update students3_info set name='xxx_xiugai' where id=3;"
# cur.execute(update_sql) #执行aql
# conn.commit()#提交数据库
# conn.rollback()#回滚
# print(cur.fetchall())#所有的结果
# cur.close()
# conn.close()
# delete_sql = "delete from students3_info where id = 5; "
# cur.execute(delete_sql) #执行aql
# conn.commit()#提交数据库
# #conn.rollback()#回滚
# print(cur.fetchall())#所有的结果
#
# cur.close()
# conn.close()