久久精品精选,精品九九视频,www久久只有这里有精品,亚洲熟女乱色综合一区
    分享

    python 連接各類主流數據庫簡單示例

     imelee 2017-01-10

    本篇博文主要介紹Python連接各種數據庫的方法及簡單使用
    包括關系數據庫:sqlite,mysql,mssql
    非關系數據庫:MongoDB,Redis

    代碼寫的比較清楚,直接上代碼

    1.連接sqlite

    # coding=utf-8
    # http://www.runoob.com/sqlite/sqlite-python.html
    import sqlite3
    import traceback
    
    try:
        # 如果表不存在,就創建
        with sqlite3.connect('test.db') as conn:
    
            print("Opened database successfully")
    
            # 刪除表
            conn.execute("DROP TABLE IF EXISTS  COMPANY")
    
            # 創建表
            sql = """
                     CREATE TABLE IF NOT EXISTS COMPANY
                   (ID INTEGER  PRIMARY KEY       AUTOINCREMENT,
                   NAME           TEXT    NOT NULL,
                   AGE            INT     NOT NULL,
                   ADDRESS        CHAR(50),
                   SALARY         REAL);
            """
            conn.execute(sql)
    
            print("create table successfully")
    
            # 添加數據
            conn.executemany("INSERT INTO COMPANY (NAME,AGE,ADDRESS,SALARY) VALUES (?, ?, ?, ? )",
                             [('Paul', 32, 'California', 20000.00),
                              ('Allen', 25, 'Texas', 15000.00),
                              ('Teddy', 23, 'Norway', 20000.00),
                              ('Mark', 25, 'Rich-Mond ', 65000.00),
                              ('David', 27, 'Texas', 85000.00),
                              ('Kim', 22, 'South-Hall', 45000.00),
                              ('James', 24, 'Houston', 10000.00)])
            # conn.execute("INSERT INTO COMPANY (NAME,AGE,ADDRESS,SALARY)        # VALUES ( 'Paul', 32, 'California', 20000.00 )")
            #
            # conn.execute("INSERT INTO COMPANY (NAME,AGE,ADDRESS,SALARY)        # VALUES ('Allen', 25, 'Texas', 15000.00 )")
            #
            # conn.execute("INSERT INTO COMPANY (NAME,AGE,ADDRESS,SALARY)        # VALUES ('Teddy', 23, 'Norway', 20000.00 )")
            #
            # conn.execute("INSERT INTO COMPANY (NAME,AGE,ADDRESS,SALARY)        # VALUES ( 'Mark', 25, 'Rich-Mond ', 65000.00 )")
            #
            # conn.execute("INSERT INTO COMPANY (NAME,AGE,ADDRESS,SALARY)        # VALUES ( 'David', 27, 'Texas', 85000.00 )");
            #
            # conn.execute("INSERT INTO COMPANY (NAME,AGE,ADDRESS,SALARY)        # VALUES ( 'Kim', 22, 'South-Hall', 45000.00 )")
            #
            # conn.execute("INSERT INTO COMPANY (NAME,AGE,ADDRESS,SALARY)        # VALUES ( 'James', 24, 'Houston', 10000.00 )")
    
            # 提交,否則重新運行程序時,表中無數據
            conn.commit()
            print("insert successfully")
    
            # 查詢表
            sql = """
                select id,NAME,AGE,ADDRESS,SALARY FROM COMPANY
             """
    
            result = conn.execute(sql)
    
            for row in result:
                print("-" * 50)  # 輸出50個-,作為分界線
                print("%-10s %s" % ("id", row[0]))  # 字段名固定10位寬度,并且左對齊
                print("%-10s %s" % ("name", row[1]))
                print("%-10s %s" % ("age", row[2]))
                print("%-10s %s" % ("address", row[3]))
                print("%-10s %.2f" % ("salary", row[4]))
                # or
                # print('{:10s} {:.2f}'.format("salary", row[4]))
    
    
    except sqlite3.Error as e:
        print("sqlite3 Error:", e)
        traceback.print_exc()
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83

    2.連接mysql

    2.1使用mysqldb庫中的_mysql

    #! /usr/bin/env python2.7
    # coding=utf-8
    # Created by xiaosanyu at 16/5/30
    
    # mysqldb 只支持python2.7
    # http://mysql-python./
    
    import MySQLdb
    from contextlib import closing
    import traceback
    
    try:
        # 獲取一個數據庫連接
        with closing(MySQLdb.connect(host='localhost', user='root', passwd='root', db='test', port=3306,charset='utf8')) as conn:
            print("connect database successfully")
            with closing(conn.cursor()) as cur:
                # 刪除表
                cur.execute("DROP TABLE IF EXISTS  COMPANY")
                # 創建表
                sql = """
                         CREATE TABLE IF NOT EXISTS COMPANY
                       (ID INTEGER  PRIMARY KEY NOT NULL  auto_increment,
                       NAME           TEXT    NOT NULL,
                       AGE            INT     NOT NULL,
                       ADDRESS        CHAR(50),
                       SALARY         REAL);
                """
                cur.execute(sql)
    
                print("create table successfully")
    
                # 添加數據
                # 在一個conn.execute里面里面執行多個sql語句是非法的
                cur.executemany("INSERT INTO COMPANY (NAME,AGE,ADDRESS,SALARY) VALUES ( %s, %s, %s, %s )",
                                [('Paul', 32, 'California', 20000.00),
                                 ('Allen', 25, 'Texas', 15000.00),
                                 ('Teddy', 23, 'Norway', 20000.00),
                                 ('Mark', 25, 'Rich-Mond ', 65000.00),
                                 ('David', 27, 'Texas', 85000.00),
                                 ('Kim', 22, 'South-Hall', 45000.00),
                                 ('James', 24, 'Houston', 10000.00)])
    
                # 提交,否則重新運行程序時,表中無數據
                conn.commit()
                print("insert successfully")
    
                # 查詢表
                sql = """
                    select id,NAME,AGE,ADDRESS,SALARY FROM COMPANY
                 """
    
                cur.execute(sql)
    
                for row in cur.fetchall():
                    print("-" * 50)  # 輸出50個-,作為分界線
                    print("%-10s %s" % ("id", row[0]))  # 字段名固定10位寬度,并且左對齊
                    print("%-10s %s" % ("name", row[1]))
                    print("%-10s %s" % ("age", row[2]))
                    print("%-10s %s" % ("address", row[3]))
                    print("%-10s %s" % ("salary", row[4]))
    
    except MySQLdb.Error as e:
        print("Mysql Error:", e)
        traceback.print_exc()  # 打印錯誤棧信息
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65

    2.2 使用MySQLdb

    #! /usr/bin/env python2.7
    # coding=utf-8
    # Created by xiaosanyu at 16/5/30
    
    # mysqldb 只支持python2.7
    # http://mysql-python./
    
    import MySQLdb
    from contextlib import closing
    import traceback
    
    try:
        # 獲取一個數據庫連接
        with closing(MySQLdb.connect(host='localhost', user='root', passwd='root', db='test', port=3306,charset='utf8')) as conn:
            print("connect database successfully")
            with closing(conn.cursor()) as cur:
                # 刪除表
                cur.execute("DROP TABLE IF EXISTS  COMPANY")
                # 創建表
                sql = """
                         CREATE TABLE IF NOT EXISTS COMPANY
                       (ID INTEGER  PRIMARY KEY NOT NULL  auto_increment,
                       NAME           TEXT    NOT NULL,
                       AGE            INT     NOT NULL,
                       ADDRESS        CHAR(50),
                       SALARY         REAL);
                """
                cur.execute(sql)
    
                print("create table successfully")
    
                # 添加數據
                # 在一個conn.execute里面里面執行多個sql語句是非法的
                cur.executemany("INSERT INTO COMPANY (NAME,AGE,ADDRESS,SALARY) VALUES ( %s, %s, %s, %s )",
                                [('Paul', 32, 'California', 20000.00),
                                 ('Allen', 25, 'Texas', 15000.00),
                                 ('Teddy', 23, 'Norway', 20000.00),
                                 ('Mark', 25, 'Rich-Mond ', 65000.00),
                                 ('David', 27, 'Texas', 85000.00),
                                 ('Kim', 22, 'South-Hall', 45000.00),
                                 ('James', 24, 'Houston', 10000.00)])
    
                # 提交,否則重新運行程序時,表中無數據
                conn.commit()
                print("insert successfully")
    
                # 查詢表
                sql = """
                    select id,NAME,AGE,ADDRESS,SALARY FROM COMPANY
                 """
    
                cur.execute(sql)
    
                for row in cur.fetchall():
                    print("-" * 50)  # 輸出50個-,作為分界線
                    print("%-10s %s" % ("id", row[0]))  # 字段名固定10位寬度,并且左對齊
                    print("%-10s %s" % ("name", row[1]))
                    print("%-10s %s" % ("age", row[2]))
                    print("%-10s %s" % ("address", row[3]))
                    print("%-10s %s" % ("salary", row[4]))
    
    except MySQLdb.Error as e:
        print("Mysql Error:", e)
        traceback.print_exc()  # 打印錯誤棧信息
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65

    2.3使用pymysql

    2.1和2.2節使用MySQLdb,不支持Python3.x
    pymysql對Python2.x和Python3.x的支持都比較好

    # Created by xiaosanyu at 16/5/30
    # coding=utf-8
    
    # https://github.com/PyMySQL/PyMySQL/
    import pymysql
    from contextlib import closing
    import traceback
    
    try:
        # 獲取一個數據庫連接,with關鍵字 表示退出時,conn自動關閉
        # with 嵌套上一層的with 要使用closing()
        with closing(pymysql.connect(host='localhost', user='root', passwd='root', db='test', port=3306,
                                     charset='utf8')) as conn:
    
            print("connect database successfully")
    
            # 獲取游標,with關鍵字 表示退出時,cur自動關閉
            with conn.cursor() as cur:
                # 刪除表
                cur.execute("DROP TABLE IF EXISTS  COMPANY")
                # 創建表
                sql = """
                         CREATE TABLE IF NOT EXISTS COMPANY
                       (ID INTEGER  PRIMARY KEY NOT NULL  auto_increment,
                       NAME           TEXT    NOT NULL,
                       AGE            INT     NOT NULL,
                       ADDRESS        CHAR(50),
                       SALARY         REAL);
                """
                cur.execute(sql)
    
                print("create table successfully")
    
                # 添加數據
                # 在一個conn.execute里面里面執行多個sql語句是非法的
                cur.executemany("INSERT INTO COMPANY (NAME,AGE,ADDRESS,SALARY) VALUES ( %s, %s, %s, %s )",
                                [('Paul', 32, 'California', 20000.00),
                                 ('Allen', 25, 'Texas', 15000.00),
                                 ('Teddy', 23, 'Norway', 20000.00),
                                 ('Mark', 25, 'Rich-Mond ', 65000.00),
                                 ('David', 27, 'Texas', 85000.00),
                                 ('Kim', 22, 'South-Hall', 45000.00),
                                 ('James', 24, 'Houston', 10000.00)])
    
                # 提交,否則重新運行程序時,表中無數據
                conn.commit()
                print("insert successfully")
    
                # 查詢表
                sql = """
                    select id,NAME,AGE,ADDRESS,SALARY FROM COMPANY
                 """
    
                cur.execute(sql)
    
                for row in cur.fetchall():
                    print("-" * 50)  # 輸出50個-,作為分界線
                    print("%-10s %s" % ("id", row[0]))  # 字段名固定10位寬度,并且左對齊
                    print("%-10s %s" % ("name", row[1]))
                    print("%-10s %s" % ("age", row[2]))
                    print("%-10s %s" % ("address", row[3]))
                    print("%-10s %s" % ("salary", row[4]))
    except pymysql.Error as e:
        print("Mysql Error:", e)
        traceback.print_exc()
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66

    3.連接mssql

    # Created by xiaosanyu at 16/5/30
    
    # http://www./en/latest/
    import pymssql
    from contextlib import closing
    
    try:
        # 先要保證數據庫中有test數據庫
        # 獲取一個數據庫連接,with關鍵字 表示退出時,conn自動關閉
        # with 嵌套上一層的with 要使用closing()
        with closing(pymssql.connect(host='192.168.100.114', user='sa', password='sa12345', database='test', port=1433,
                                     charset='utf8')) as conn:
    
            print("connect database successfully")
    
            # 獲取游標,with關鍵字 表示退出時,cur自動關閉
            with conn.cursor() as cur:
                # 刪除表
                cur.execute(
                        '''if exists (select 1 from  sys.objects where name='COMPANY' and  type='U')  drop table COMPANY''')
                # 創建表
                sql = """
                         CREATE TABLE  COMPANY
                       (ID INT  IDENTITY(1,1) PRIMARY KEY NOT NULL ,
                       NAME           TEXT    NOT NULL,
                       AGE            INT     NOT NULL,
                       ADDRESS        CHAR(50),
                       SALARY         REAL);
                """
                cur.execute(sql)
    
                print("create table successfully")
    
                # 添加數據
                # 在一個conn.execute里面里面執行多個sql語句是非法的
                cur.executemany("INSERT INTO COMPANY (NAME,AGE,ADDRESS,SALARY) VALUES ( %s, %s, %s, %s )",
                                [('Paul', 32, 'California', 20000.00),
                                 ('Allen', 25, 'Texas', 15000.00),
                                 ('Teddy', 23, 'Norway', 20000.00),
                                 ('Mark', 25, 'Rich-Mond', 65000.00),
                                 ('David', 27, 'Texas', 85000.00),
                                 ('Kim', 22, 'South-Hall', 45000.00),
                                 ('James', 24, 'Houston', 10000.00)])
    
                # 提交,否則重新運行程序時,表中無數據
                conn.commit()
                print("insert successfully")
    
                # 查詢表
                sql = """
                    select id,NAME,AGE,ADDRESS,SALARY FROM COMPANY
                 """
    
                cur.execute(sql)
    
                for row in cur.fetchall():
                    print("-" * 50)  # 輸出50個-,作為分界線
                    print("%-10s %s" % ("id", row[0]))  # 字段名固定10位寬度,并且左對齊
                    print("%-10s %s" % ("name", row[1]))
                    print("%-10s %s" % ("age", row[2]))
                    print("%-10s %s" % ("address", row[3]))
                    print("%-10s %s" % ("salary", row[4]))
    except pymssql.Error as e:
        print("mssql Error:", e)
        # traceback.print_exc()
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66

    4.連接MongoDB

    # Created by xiaosanyu at 16/5/30
    
    # https://docs./ecosystem/drivers/python/
    # https://pypi./pypi/pymongo/
    
    import pymongo
    from pymongo.mongo_client import MongoClient
    import pymongo.errors
    import traceback
    
    try:
        # 連接到 mongodb 服務
        mongoClient = MongoClient('localhost', 27017)
        # 連接到數據庫
        mongoDatabase = mongoClient.test
        print("connect database successfully")
    
        # 獲取集合
        mongoCollection = mongoDatabase.COMPANY
    
        # 移除所有數據
        mongoCollection.remove()
    
        # 添加數據
        mongoCollection.insert_many([{"Name": "Paul", "Age": "32", "Address": "California", "Salary": "20000.00"},
                                     {"Name": "Allen", "Age": "25", "Address": "Texas", "Salary": "15000.00"},
                                     {"Name": "Teddy", "Age": "23", "Address": "Norway", "Salary": "20000.00"},
                                     {"Name": "Mark", "Age": "25", "Address": "Rich-Mond", "Salary": "65000.00"},
                                     {"Name": "David", "Age": "27", "Address": "Texas", "Salary": "85000.00"},
                                     {"Name": "Kim", "Age": "22", "Address": "South-Hall", "Salary": "45000.00"},
                                     {"Name": "James", "Age": "24", "Address": "Houston", "Salary": "10000.00"}, ])
    
        #獲取集合中的值
        for row in mongoCollection.find():
            print("-" * 50)  # 輸出50個-,作為分界線
            print("%-10s %s" % ("_id", row['_id']))  # 字段名固定10位寬度,并且左對齊
            print("%-10s %s" % ("name", row['Name']))
            print("%-10s %s" % ("age", row['Age']))
            print("%-10s %s" % ("address", row['Address']))
            print("%-10s %s" % ("salary", row['Salary']))
    
        print('\n\n\n')
        # 使id自增
        mongoCollection.remove()
        # 創建計數表
        mongoDatabase.counters.save({"_id": "people_id", "sequence_value": 0})
        # 創建存儲過程
        mongoDatabase.system_js.getSequenceValue = '''function getSequenceValue(sequenceName){
                var sequenceDocument = db.counters.findAndModify({
                    query: {_id: sequenceName},
                    update: {$inc:{sequence_value: 1}},
                    new:true
                });
                return sequenceDocument.sequence_value;
            }'''
        mongoCollection.insert_many(
                [{"_id": mongoDatabase.eval("getSequenceValue('people_id')"), "Name": "Paul", "Age": "32",
                  "Address": "California", "Salary": "20000.00"},
                 {"_id": mongoDatabase.eval("getSequenceValue('people_id')"), "Name": "Allen", "Age": "25",
                  "Address": "Texas", "Salary": "15000.00"},
                 {"_id": mongoDatabase.eval("getSequenceValue('people_id')"), "Name": "Teddy", "Age": "23",
                  "Address": "Norway", "Salary": "20000.00"},
                 {"_id": mongoDatabase.eval("getSequenceValue('people_id')"), "Name": "Mark", "Age": "25",
                  "Address": "Rich-Mond", "Salary": "65000.00"},
                 {"_id": mongoDatabase.eval("getSequenceValue('people_id')"), "Name": "David", "Age": "27",
                  "Address": "Texas", "Salary": "85000.00"},
                 {"_id": mongoDatabase.eval("getSequenceValue('people_id')"), "Name": "Kim", "Age": "22",
                  "Address": "South-Hall", "Salary": "45000.00"},
                 {"_id": mongoDatabase.eval("getSequenceValue('people_id')"), "Name": "James", "Age": "24",
                  "Address": "Houston", "Salary": "10000.00"}, ])
    
        for row in mongoCollection.find():
            print("-" * 50)  # 輸出50個-,作為分界線
            print("%-10s %s" % ("_id", int(row['_id'])))  # 字段名固定10位寬度,并且左對齊
            print("%-10s %s" % ("name", row['Name']))
            print("%-10s %s" % ("age", row['Age']))
            print("%-10s %s" % ("address", row['Address']))
            print("%-10s %s" % ("salary", row['Salary']))
    except pymongo.errors.PyMongoError as e:
        print("mongo Error:", e)
        traceback.print_exc()
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82

    5.連接Redis

    5.1使用redis

    # coding=utf-8
    # Created by xiaosanyu at 16/5/31
    
    # https://pypi./pypi/redis/2.10.5
    # http://redis-py./en/latest/#
    import redis
    
    r = redis.Redis(host='localhost', port=6379, db=0, password="12345")
    print("connect", r.ping())
    
    # 看信息
    info = r.info()
    # or 查看部分信息
    # info = r.info("Server")
    
    # 輸出信息
    items = info.items()
    for i, (key, value) in enumerate(items):
        print("item %s----%s:%s" % (i, key, value))
    
    # 刪除鍵和對應的值
    r.delete("company")
    
    # 可以一次性push一條或多條數據
    r.rpush("company", {"id": 1, "Name": "Paul", "Age": "32", "Address": "California", "Salary": "20000.00"},
            {"id": 2, "Name": "Allen", "Age": "25", "Address": "Texas", "Salary": "15000.00"},
            {"id": 3, "Name": "Teddy", "Age": "23", "Address": "Norway", "Salary": "20000.00"})
    r.rpush("company", {"id": 4, "Name": "Mark", "Age": "25", "Address": "Rich-Mond", "Salary": "65000.00"})
    r.rpush("company", {"id": 5, "Name": "David", "Age": "27", "Address": "Texas", "Salary": "85000.00"})
    r.rpush("company", {"id": 6, "Name": "Kim", "Age": "22", "Address": "South-Hall", "Salary": "45000.00"})
    r.rpush("company", {"id": 7, "Name": "James", "Age": "24", "Address": "Houston", "Salary": "10000.00"})
    
    # eval用來將dict格式的字符串轉換成dict
    for row in map(lambda x: eval(x), r.lrange("company", 0, r.llen("company"))):
        print("-" * 50)  # 輸出50個-,作為分界線
        print("%-10s %s" % ("_id", row['id']))  # 字段名固定10位寬度,并且左對齊
        print("%-10s %s" % ("name", row['Name']))
        print("%-10s %s" % ("age", row['Age']))
        print("%-10s %s" % ("address", row['Address']))
        print("%-10s %s" % ("salary", row['Salary']))
    
    # 關閉當前連接
    # r.shutdown() #這個是關閉redis服務端
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44

    5.2使用pyredis

    # Created by xiaosanyu at 16/5/30
    
    # http://pyredis./en/latest/
    import pyredis
    
    r = pyredis.Client(host='localhost', port=6379, database=0, password="12345")
    print("connect", r.ping().decode("utf-8"))
    
    # 看信息
    
    # info = r.execute("info").decode()
    # or 查看部分信息
    info = r.execute("info", "Server").decode()
    
    # 輸出信息
    print(info)
    
    # 刪除鍵和對應的值
    r.delete("company")
    
    # 可以一次性push一條或多條數據
    r.rpush("company", '''{"id": 1, "Name": "Paul", "Age": "32", "Address": "California", "Salary": "20000.00"}''',
            '''{"id": 2, "Name": "Allen", "Age": "25", "Address": "Texas", "Salary": "15000.00"}''',
            '''{"id": 3, "Name": "Teddy", "Age": "23", "Address": "Norway", "Salary": "20000.00"}''')
    r.rpush("company", '''{"id": 4, "Name": "Mark", "Age": "25", "Address": "Rich-Mond", "Salary": "65000.00"}''')
    r.rpush("company", '''{"id": 5, "Name": "David", "Age": "27", "Address": "Texas", "Salary": "85000.00"}''')
    r.rpush("company", '''{"id": 6, "Name": "Kim", "Age": "22", "Address": "South-Hall", "Salary": "45000.00"}''')
    r.rpush("company", '''{"id": 7, "Name": "James", "Age": "24", "Address": "Houston", "Salary": "10000.00"}''')
    
    # eval用來將dict格式的字符串轉換成dict
    for row in map(lambda x: eval(x), r.lrange("company", 0, r.llen("company"))):
        print("-" * 50)  # 輸出50個-,作為分界線
        print("%-10s %s" % ("_id", row['id']))  # 字段名固定10位寬度,并且左對齊
        print("%-10s %s" % ("name", row['Name']))
        print("%-10s %s" % ("age", row['Age']))
        print("%-10s %s" % ("address", row['Address']))
        print("%-10s %s" % ("salary", row['Salary']))
    
    # 關閉當前連接
    r.close()
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41

    代碼下載:python_connect_database

    歡迎交流溝通~

      本站是提供個人知識管理的網絡存儲空間,所有內容均由用戶發布,不代表本站觀點。請注意甄別內容中的聯系方式、誘導購買等信息,謹防詐騙。如發現有害或侵權內容,請點擊一鍵舉報。
      轉藏 分享 獻花(0

      0條評論

      發表

      請遵守用戶 評論公約

      類似文章 更多

      主站蜘蛛池模板: 欧美饥渴熟妇高潮喷水| 久久亚洲男人第一AV网站| 第一亚洲中文久久精品无码| 另类专区一区二区三区| 人妻中文字幕亚洲一区| 亚洲一区二区精品动漫| 亚洲国产在一区二区三区| 亚洲另类丝袜综合网| 精品人妻系列无码人妻漫画| 免费人成在线观看| 四虎国产精品永久在线下载| 亚洲一区二区精品极品| 久久精品第九区免费观看| 人妻人人做人做人人爱| 狠狠人妻久久久久久综合| 深夜精品免费在线观看| 人妻少妇邻居少妇好多水在线| 精品国产亚洲一区二区三区| 国产成熟女人性满足视频| 欧美亚洲高清国产| 国产欧美一区二区精品久久久| 成人亚欧欧美激情在线观看| 国产AV无码专区亚洲AWWW| 777奇米四色成人影视色区| 国产美熟女乱又伦AV果冻传媒 | 免费又大粗又爽又黄少妇毛片| 人妻少妇偷人无码视频| 精品国偷自产在线视频| 精品免费看国产一区二区| 久久精品岛国AV一区二区无码| 亚洲av永久无码精品网站| 在线播放免费人成毛片| 内射一区二区三区四区| 亚洲精品55夜色66夜色| 99视频30精品视频在线观看| 凹凸在线无码免费视频| 女人喷液抽搐高潮视频| 老师扒下内裤让我爽了一夜| 亚洲精品二区在线播放| 激情综合婷婷色五月蜜桃| 国产精品专区第1页|