标签:句柄 oca python基础 date 面向过程 comm www. 首字母 net
ubuntu下安装MySQLdb
sudo apt-get install python-MySQLdb
导入MySQLdb库
import MySQLdb
创建数据库连接
conn = MySQLdb.connect(host="localhost",user="root",passwd="123456",db="test",charset="utf8")
commit()
:如果数据库表进行了修改,提交保存当前的数据。当然,如果此用户没有权限就作罢了,什么也不会发生。rollback()
:如果有权限,就取消当前的操作,否则报错。cursor([cursorclass])
:游标指针。创建游标(指针)cursor
cur = conn.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.com"))
在数据表中插入多条记录
cur.executemany("insert into users (username,password,email) values (%s,%s,%s)",(("google","111222","g@gmail.com"),("facebook","222333","f@face.book"),("github","333444","git@hub.com"),("docker","444555","doc@ker.com")))
提交数据库操作
conn.commit()
查询数据
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的操作
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.commit()
指定数据库
conn = MySQLdb.connect("localhost","root","123456",port=3306,charset="utf8") #创建数据库时不指定那个数据库
conn.select_db("test") #连接创建后再指定
关闭数据库
cur.close() #先关闭游标
conn.close() #再关闭数据库
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
子类、父类和继承
# 抽象形状类
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支持多继承,但不推荐使用
标签:句柄 oca python基础 date 面向过程 comm www. 首字母 net
原文地址:http://www.cnblogs.com/lqylqy/p/7302420.html