ubuntu mysql安装:
sudo apt-get install libmysqlclient-dev
pip install mysqlclient
import MySQLdb
win mysql安装:
pip install mysql-connector
pip install pyMysql
redis安装:
redis程序我是在宝塔直接安装的,方便好用,没有宝塔自行百度安装
sudo pip install redis
mysql增删改查:
import pymysql
db = pymysql.connect(host='0.0.0.0',
port=3306,
user='admin',
passwd='123456',
db='sqlname')
cur = db.cursor()
print('初始化完成')
def select_db (sql,chiose='cha'):
if chiose != 'cha':
try:
cur.execute(sql)
db.commit()
except Exception as e:
print("操作异常:%s" % str(e))
db.rollback()
finally:
db.close()
else:
cur.execute(sql)
data = cur.fetchall()
db.close()
return data
if __name__ == "__main__":
sql = "SELECT * FROM 表名 WHERE 列名=值"
a = select_db(sql,'cha')
print(a[0][0])
"""INSERT INTO 表名 (列名1, 列名2) VALUES("%s","%s")""" % (值1 ,值2)
redis增删改查:
import redis
redis_ = redis.Redis(host='127.0.0.1', port=6379, decode_responses=True)
print('初始化完成')
redis_.set('键值', '值')
redis_.get('键值')
redis_.delete('键值')
redis_.expire('键值',604800)
|