python操做mysql數據庫的相關操做實例

python操做mysql數據庫的相關操做實例


# -*- coding: utf-8 -*-
#python operate mysql database
import MySQLdb
 
#數據庫名稱
DATABASE_NAME = ''
#host = 'localhost' or '172.0.0.1'
HOST = ''
#端口號
PORT = ''
#用戶名稱
USER_NAME = ''
#數據庫密碼
PASSWORD = ''
#數據庫編碼
CHAR_SET = ''
 
#初始化參數
def init():
    global DATABASE_NAME
    DATABASE_NAME = 'test'
    global HOST
    HOST = 'localhost'
    global PORT
    PORT = '3306'
    global USER_NAME
    USER_NAME = 'root'
    global PASSWORD
    PASSWORD = 'root'
    global CHAR_SET
    CHAR_SET = 'utf8'
     
#獲取數據庫鏈接
def get_conn():
    init()
    return MySQLdb.connect(host = HOST, user = USER_NAME, passwd = PASSWORD, db = DATABASE_NAME, charset = CHAR_SET)
 
#獲取cursor
def get_cursor(conn):
    return conn.cursor()
 
#關閉鏈接
def conn_close(conn):
    if conn != None:
        conn.close()
 
#關閉cursor
def cursor_close(cursor):
    if cursor != None:
        cursor.close()
 
#關閉全部
def close(cursor, conn):
    cursor_close(cursor)
    conn_close(conn)
 
#建立表
def create_table():
    sql = '''
    CREATE TABLE `student` (
    `id` int(11) NOT NULL,
    `name` varchar(20) NOT NULL,
    `age` int(11) DEFAULT NULL,
    PRIMARY KEY (`id`),
    UNIQUE KEY `name` (`name`)
    ) ENGINE=InnoDB DEFAULT CHARSET=utf8
    '''
    conn = get_conn()
    cursor = get_cursor(conn)
    result = cursor.execute(sql)
    conn.commit()
    close(cursor, conn)
    return result
 
#查詢表信息
def query_table(table_name):
    if table_name != '':
        sql = 'select * from ' + table_name
        conn = get_conn()
        cursor = get_cursor(conn)
        result = cursor.execute(sql)
        for row in cursor.fetchall():
            print(row)
            #for r in row:      #循環每一條數據
                #print(r)
        close(cursor, conn)
    else:
        print('table name is empty!')
 
#插入數據
def insert_table():
    sql = 'insert into student(id, name, age) values(%s, %s, %s)'
    params = ('1', 'Hongten_a', '21')
    conn = get_conn()
    cursor = get_cursor(conn)
    result = cursor.execute(sql, params)
    conn.commit()
    close(cursor, conn)
    return result
 
#更新數據
def update_table():
    sql = 'update student set name = %s where id = 1'
    params = ('HONGTEN')
    conn = get_conn()
    cursor = get_cursor(conn)
    result = cursor.execute(sql, params)
    conn.commit()
    close(cursor, conn)
    return result
 
#刪除數據
def delete_data():
    sql = 'delete from student where id = %s'
    params = ('1')
    conn = get_conn()
    cursor = get_cursor(conn)
    result = cursor.execute(sql, params)
    conn.commit()
    close(cursor, conn)
    return result
 
#數據庫鏈接信息   
def print_info():
    print('數據庫鏈接信息:' + DATABASE_NAME + HOST + PORT + USER_NAME + PASSWORD + CHAR_SET)
 
#打印出數據庫中表狀況
def show_databases():
    sql = 'show databases'
    conn = get_conn()
    cursor = get_cursor(conn)
    result = cursor.execute(sql)
    for row in cursor.fetchall():
        print(row)
         
#數據庫中表狀況
def show_tables():
    sql = 'show tables'
    conn = get_conn()
    cursor = get_cursor(conn)
    result = cursor.execute(sql)
    for row in cursor.fetchall():
        print(row)
 
    
def main():
    show_tables()
    #建立表
    result = create_table()
    print(result)
    #查詢表
    query_table('student')
    #插入數據
    print(insert_table())
    print('插入數據後....')
    query_table('student')
    #更新數據
    print(update_table())
    print('更新數據後....')
    query_table('student')
    #刪除數據
    delete_data()
    print('刪除數據後....')
    query_table('student')
    print_info()
    #數據庫中表狀況
    show_tables()
     
 
if __name__ == '__main__':
    main()
相關文章
相關標籤/搜索