python的MySQLdb库基本使用介绍

MySQLdb库

import MySQLdb

 

简介

提供mysql的基本操作(包括建表,读取表数据,插入数据到表)

 

数据库操作基本步骤

 


#!/usr/bin/python
# -*- coding: UTF-8 -*-

import MySQLdb

if __name__ == "__main__":
    # 打开数据库连接
    db = MySQLdb.connect("localhost", "root", "root", "iquota", charset=utf8)

    # 使用cursor()方法获取操作游标
    cursor = db.cursor()

    # 查看表
    sql_cmd="select * from app;"

    cursor.execute(sql_cmd)
    data = cursor.fetchone()
    data_all = cursor.fetchall()

    # 关闭数据库连接
    db.close()

    # 读取一行
    print data
    # 读取全部内容
    print data_all

mysql基本读取举例

输出结果


(10L, u5c26736821d24de48942d982b1c69000, uqa-stress, uqa app for stress test, uzhangsan)
((11L, u5c26736821d24de48942d982b1c69000, uqa-stress, uqa app for stress test, ulisi), (12L, u37ea7ce186404c2ea3a1fb4079d58c15, uqa-stress-test, uqa app for stress test, uzhangsan), (13L, u37ea7ce186404c2ea3a1fb4079d58c15, uqa-stress-test, uqa app for stress test, ulisi), (14L, u283182bf5ffe4398932d72e9a6d239cc, uapp1, uadd_request, ulisi), (21L, u394c56cde2cb4434a01cff43defcc0a6, uliurong07Test, uadd_request, ulisi))

View Code