标签:hash sql get raw 模块 wds utf8 esc char
sudo apt-get install python-mysql
import Mysqldb
conn=connect(参数列表)
cursor1=conn.cursor()
#encoding=utf-8
import MySQLdb
try:
conn=MySQLdb.connect(host=‘localhost‘,port=3306,db=‘test1‘,user=‘root‘,passwd=‘mysql‘,charset=‘utf8‘)
cs1=conn.cursor()
count=cs1.execute("insert into students(sname) values(‘张良‘)")
print count
conn.commit()
cs1.close()
conn.close()
except Exception,e:
print e.message
#encoding=utf-8
import MySQLdb
try:
conn=MySQLdb.connect(host=‘localhost‘,port=3306,db=‘test1‘,user=‘root‘,passwd=‘mysql‘,charset=‘utf8‘)
cs1=conn.cursor()
count=cs1.execute("update students set sname=‘刘邦‘ where id=6")
print count
conn.commit()
cs1.close()
conn.close()
except Exception,e:
print e.message
#encoding=utf-8
import MySQLdb
try:
conn=MySQLdb.connect(host=‘localhost‘,port=3306,db=‘test1‘,user=‘root‘,passwd=‘mysql‘,charset=‘utf8‘)
cs1=conn.cursor()
count=cs1.execute("delete from students where id=6")
print count
conn.commit()
cs1.close()
conn.close()
except Exception,e:
print e.message
#encoding=utf-8
import MySQLdb
try:
conn=MySQLdb.connect(host=‘localhost‘,port=3306,db=‘test1‘,user=‘root‘,passwd=‘mysql‘,charset=‘utf8‘)
cs1=conn.cursor()
sname=raw_input("请输入学生姓名:")
params=[sname]
count=cs1.execute(‘insert into students(sname) values(%s)‘,params)
print count
conn.commit()
cs1.close()
conn.close()
except Exception,e:
print e.message
#encoding=utf8
import MySQLdb
try:
conn=MySQLdb.connect(host=‘localhost‘,port=3306,db=‘test1‘,user=‘root‘,passwd=‘mysql‘,charset=‘utf8‘)
cur=conn.cursor()
cur.execute(‘select * from students where id=7‘)
result=cur.fetchone()
print result
cur.close()
conn.close()
except Exception,e:
print e.message
#encoding=utf8
import MySQLdb
try:
conn=MySQLdb.connect(host=‘localhost‘,port=3306,db=‘test1‘,user=‘root‘,passwd=‘mysql‘,charset=‘utf8‘)
cur=conn.cursor()
cur.execute(‘select * from students‘)
result=cur.fetchall()
print result
cur.close()
conn.close()
except Exception,e:
print e.message
#encoding=utf8
import MySQLdb
class MysqlHelper():
def __init__(self,host,port,db,user,passwd,charset=‘utf8‘):
self.host=host
self.port=port
self.db=db
self.user=user
self.passwd=passwd
self.charset=charset
def connect(self):
self.conn=MySQLdb.connect(host=self.host,port=self.port,db=self.db,user=self.user,passwd=self.passwd,charset=self.charset)
self.cursor=self.conn.cursor()
def close(self):
self.cursor.close()
self.conn.close()
def get_one(self,sql,params=()):
result=None
try:
self.connect()
self.cursor.execute(sql, params)
result = self.cursor.fetchone()
self.close()
except Exception, e:
print e.message
return result
def get_all(self,sql,params=()):
list=()
try:
self.connect()
self.cursor.execute(sql,params)
list=self.cursor.fetchall()
self.close()
except Exception,e:
print e.message
return list
def insert(self,sql,params=()):
return self.__edit(sql,params)
def update(self, sql, params=()):
return self.__edit(sql, params)
def delete(self, sql, params=()):
return self.__edit(sql, params)
def __edit(self,sql,params):
count=0
try:
self.connect()
count=self.cursor.execute(sql,params)
self.conn.commit()
self.close()
except Exception,e:
print e.message
return count
#encoding=utf8
from MysqlHelper import *
sql=‘insert into students(sname,gender) values(%s,%s)‘
sname=raw_input("请输入用户名:")
gender=raw_input("请输入性别,1为男,0为女")
params=[sname,bool(gender)]
mysqlHelper=MysqlHelper(‘localhost‘,3306,‘test1‘,‘root‘,‘mysql‘)
count=mysqlHelper.insert(sql,params)
if count==1:
print ‘ok‘
else:
print ‘error‘
#encoding=utf8
from MysqlHelper import *
sql=‘select sname,gender from students order by id desc‘
helper=MysqlHelper(‘localhost‘,3306,‘test1‘,‘root‘,‘mysql‘)
one=helper.get_one(sql)
print one
create table userinfos(
id int primary key auto_increment,
uname varchar(20),
upwd char(40),
isdelete bit default 0
);
insert into userinfos values(0,‘123‘,‘40bd001563085fc35165329ea1ff5c5ecbdbbeef‘,0);
#encoding=utf-8
from MysqlHelper import MysqlHelper
from hashlib import sha1
sname=raw_input("请输入用户名:")
spwd=raw_input("请输入密码:")
s1=sha1()
s1.update(spwd)
spwdSha1=s1.hexdigest()
sql="select upwd from userinfos where uname=%s"
params=[sname]
sqlhelper=MysqlHelper(‘localhost‘,3306,‘test1‘,‘root‘,‘mysql‘)
userinfo=sqlhelper.get_one(sql,params)
if userinfo==None:
print ‘用户名错误‘
elif userinfo[0]==spwdSha1:
print ‘登录成功‘
else:
print ‘密码错误‘
标签:hash sql get raw 模块 wds utf8 esc char
原文地址:https://www.cnblogs.com/ghming/p/8969943.html