好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

python基础知识点讲解

ubuntu下安装MySQLdb

sudo apt-get install python-MySQLdb 

导入MySQLdb库

import MySQLdb 

创建数据库连接

conn = MySQLdb.connect(host="localhost",user="root",passwd="123456",db="test",charset="utf8") 

connect对象属性

commit() :如果数据库表进行了修改,提交保存当前的数据。当然,如果此用户没有权限就作罢了,什么也不会发生。

rollback() :如果有权限,就取消当前的操作,否则报错。

cursor([cursorclass]) :游标指针。

创建游标(指针)cursor

cur = conn.cursor() 

cursor执行命令的方法:

execute(query, args) :执行单条sql语句。query为sql语句本身,args为参数值的列表。执行后返回值为受影响的行数。

executemany(query, args) :执行单条sql语句,但是重复执行参数列表里的参数,返回值为受影响的行数

在数据表中插入一条记录

cur.execute("insert into users (username,password,email) values (%s,%s,%s)",("python","123456","python@gmail测试数据")) 

在数据表中插入多条记录

cur.executemany("insert into users (username,password,email) values (%s,%s,%s)",(("google","111222","g@gmail测试数据"),("facebook","222333","f@face.book"),("github","333444","git@hub测试数据"),("docker","444555","doc@ker测试数据"))) 

提交数据库操作

conn测试数据mit() 

查询数据

cur.execute("select * from users") 

fetchall(self) :接收全部的返回结果行.

fetchmany(size=None) :接收size条返回结果行.如果size的值大于返回的结果行的数量,则会返回cursor.arraysize条数据.

fetchone() :返回一条结果行.

scroll(value, mode='relative') :移动指针到某一行.如果mode='relative',则表示从当前所在行移动value条,如果mode='absolute',则表示从结果集的第一行移动value条.

cur.execute("select * from users")
lines = cur.fetchall()
for line in lines:
    print line

cur.execute("select * from users where id=1")
line_first = cur.fetchone()     #只返回一条
print line_first

cur.execute("select * from users")
print cur.fetchall() 

cursor对象获取数据的方法

游标cursor的操作

cur.scroll(n) 或 cur.scroll(n,"relative") :意思是相对当前位置向上或者向下移动,n为正数,表示向下(向前),n为负数,表示向上(向后)

还有一种方式,可以实现“绝对”移动,不是“相对”移动:增加一个参数"absolute"

cur.scroll(1)
cur.scroll(-2)
cur.scroll(2,"absolute")    #回到序号是2,但指向第三条 

更新数据

cur.execute("update users set username=%s where id=2",("mypython"))
conn测试数据mit() 

指定数据库

conn = MySQLdb.connect("localhost","root","123456",port=3306,charset="utf8")    #创建数据库时不指定那个数据库
conn.select_db("test")      #连接创建后再指定 

关闭数据库

cur.close()     #先关闭游标
conn.close()    #再关闭数据库 

第十七节 面向对象

类和对象

面向过程和面向对象的编程

面向过程的编程:函数式编程,C程序等

面向对象的编程:C++,Java,Python等

类和对象:是面向对象中的两个重要概念

类:是对事物的抽象,比如:汽车模型

对象:是类的一个实例,比如:QQ轿车,大客车

范例说明

汽车模型可以对汽车的特征和行为进行抽象,然后可以实例化一台真实的汽车实体出来

Python类定义

Python类的定义

使用class关键字定义一个类,并且类名的首字母要大写

当程序员需要创建的类型不能用简单类型表示时就需要创建类

类把需要的变量和函数组合在一起,这种包含也称之为“封装”

Python类的结构

class 类名:
    成员变量
    成员函数

class MyClass():
    first = 123
    def fun(self):
        print "I am function" 

对象的创建

句柄用于区分不同的对象

对象的属性和方法与类中的成员变量和成员函数对应

if __name__ == "__main__":
    myClass = MyClass()     #创建类的一个实例 

创建对象的过程称之为实例化;当一个对象被创建后,包含三个方面的特性:对象的句柄、属性和方法。

构造函数__init__

class Person:
    def __init__(self, name, lang, website):
        self.name = name
        self.lang = lang
        self.website = website 

self是一个很神奇的参数

self指向类的一个实例,当实例调用方法时,self就指向这个调用的方法的实例

子类、父类和继承

# 抽象形状类
class Shape:
    # 类的属性
    edge = 0
    # 构造函数
    def __init__(self, edge):
        self.edge = edge
    # 类的方法
    def getEdge(self):
        return self.edge
    # 抽象方法  
    def getArea(self):
        pass

#三角形类,继承抽象形状类
class Triangle(Shape):
    width = 0
    height = 0
    # 构造函数
    def __init__(self, width, height):
        #调用父类构造函数
        Shape.__init__(self, 3)
        self.width = width
        self.height = height
    #重写方法
    def getArea(self):
        return self.width * self.height / 2

#四边形类,继承抽象形状类       
class Rectangle(Shape):
    width = 0
    height = 0
    # 构造函数
    def __init__(self, width, height):
        #调用父类构造函数
        Shape.__init__(self, 4)
        self.width = width
        self.height = height
    #重写方法
    def getArea(self):
        return self.width * self.height

triangle = Triangle(4,5);
print triangle.getEdge()
print triangle.getArea()

rectangle = Rectangle(4,5);
print rectangle.getEdge()
print rectangle.getArea() 

python支持多继承,但不推荐使用

以上就是python基础知识点讲解的详细内容,更多请关注Gxl网其它相关文章!

查看更多关于python基础知识点讲解的详细内容...

  阅读:49次