## 用EXPIRE设置一个键值对的过期秒数,或者使用EXPIREAT以UNIX时间戳的形式设置过期时间,一般用作缓存 GET name # "Florance" EXPIRE name 2 # (integer) 1 ## 等待>2s GET name # (nil) ## null响应,即没有找到该键
用DEL命令可以删除键和值 SET total 1 DEL total GET total # (nil)
print(redis.sadd('menu', 'dessert', 'Tea', 'Coffee',"Milk","Water","Berberage","Beer")) print(redis.sadd('menu2', "Berberage","Beer")) print(redis.srem('menu', 'Drink')) # return index if not exists return 0 print(redis.spop('menu')) # random pop print(redis.smove('menu', 'menu2', 'Milk')) # delete Milk from menu move it to menu2 print(redis.scard('menu')) # number of elements in menu print(redis.smembers('menu')) # show all elements in menu print(redis.srandmember('menu')) # random element print(redis.sismember('menu', 'Beer')) # if element Beer in set menu print(redis.sinter(['menu', 'menu2'])) # intersect print(redis.sinterstore('inttag', ['menu', 'menu2'])) print(redis.sunion(['menu', 'menu2'])) print(redis.sunionstore('inttag', ['menu', 'menu2'])) print(redis.sdiff(['menu', 'menu2'])) print(redis.sdiffstore('inttag', ['menu', 'menu2'])) # 7 # 2 # 0 # b'dessert' # True # 5 # {b'Coffee', b'Beer', b'Berberage', b'Water', b'Tea'} # b'Coffee' # True # {b'Beer', b'Berberage'} # 2 # {b'Coffee', b'Beer', b'Berberage', b'Milk', b'Water', b'Tea'} # 6 # {b'Water', b'Coffee', b'Tea'} # 3
pool = ConnectionPool(host=HOST, password=PASS, port=PORT, db=3) redis = StrictRedis(connection_pool=pool)
csv_file = 'D:/Program Files/R-3.4.3/library/knitr/examples/download_count.csv' with open(csv_file) as f: for line_num, line in enumerate(f, 1): line = line.strip() cache_key = '%s_%s' % ('csv', line_num) redis.lpush(cache_key, line)
PostgreSQL
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
## init.bat ''' set PGHOME=D:\Bio\Postgre\bin set PATH=%PGHOME%\bin;%path% set PGHOST=localhost set PGLIB=%PGHOME%\lib set PGDATA=%PGHOME%\data ''' initdb pg_ctl start createuser -h 127.0.0.1 lysql createdb -h 127.0.0.1 demo psql -h 127.0.0.1 -U lysql -d demo -p 5432 alter user lysql with password '123456';
conn = psycopg2.connect(database="demo", user="lysql", password="123456", host="127.0.0.1", port="5432") try: cursor = conn.cursor() transaction = cursor.execute("""CREATE TABLE STUDENTS( ID INT PRIMARY KEY NOT NULL, NAME TEXT NOT NULL, AGE INT NOT NULL, ADDRESS CHAR(50), PHONE TEXT NOT NULL); """) print('Successful') conn.commit() except (Exception, psycopg2.DatabaseError) as error: print(error) finally: print(conn) if conn isnotNone: conn.close()
conn = psycopg2.connect(database="demo", user="lysql", password="123456", host="127.0.0.1", port="5432" ) try: cursor = conn.cursor() cursor.execute("UPDATE STUDENTS set ADDRESS = 'Seattle' where ID=2 "); conn.commit() cursor.execute("SELECT * from STUDENTS ORDER BY ID") rows = cursor.fetchall() print("Records fetched successfully") for row in rows: print("ID = ", row[0]) print( "NAME = ", row[1]) print( "ADDRESS = ", row[2]) print( "PHONE = ", row[3], "\n") except (Exception, psycopg2.DatabaseError) as error: print(error) finally: if conn isnotNone: conn.close()
Records fetched successfully
ID = 1
NAME = Jessica
ADDRESS = 22
PHONE = New York
ID = 2
NAME = Lucifier
ADDRESS = 15
PHONE = Seattle
ID = 3
NAME = Antonio
ADDRESS = 13
PHONE = Philadelphia
ID = 4
NAME = Athena
ADDRESS = 25
PHONE = San Francisco
Delete Data
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
conn = psycopg2.connect(database="demo", user="lysql", password="123456", host="127.0.0.1", port="5432" ) try: cursor = conn.cursor() cursor.execute("DELETE from STUDENTS where ID=2 OR AGE <=20;"); conn.commit() print(cursor.rowcount) cursor.execute("SELECT * from STUDENTS ORDER BY ID") rows = cursor.fetchall() for row in rows: print("ID = ", row[0]) print( "NAME = ", row[1]) print( "ADDRESS = ", row[2]) print( "PHONE = ", row[3], "\n") except (Exception, psycopg2.DatabaseError) as error: print(error) finally: if conn isnotNone: conn.close()
2
ID = 1
NAME = Jessica
ADDRESS = 22
PHONE = New York
ID = 4
NAME = Athena
ADDRESS = 25
PHONE = San Francisco
MySQL
Initialize
1 2 3 4 5 6 7 8 9 10
## Windows mysqld --install mysqld --initialize --user=root --console # root@localhost: KoL_gm2zn8gl net start mysql mysql -u root -p set password=password('123456'); exit Drop database Students; # net stop mysql
Creat db
1 2 3 4 5 6 7 8 9 10
import pymysql db = pymysql.connect(host='localhost',user='root', password='123456', port=3306) cursor = db.cursor() cursor.execute('SELECT VERSION()') data = cursor.fetchone() print('Database version:', data) # Database version: ('5.7.21',) cursor.execute("CREATE DATABASE Students DEFAULT CHARACTER SET utf8") db.close()
Database version: ('5.7.21',)
Creat table
1 2 3 4 5
db = pymysql.connect(host='localhost', user='root', password='123456', port=3306, db='Students') cursor = db.cursor() sql = 'CREATE TABLE IF NOT EXISTS Demo (id VARCHAR(255) NOT NULL, name VARCHAR(255) NOT NULL, age INT NOT NULL, PRIMARY KEY (id))' cursor.execute(sql) db.close()