Home > Article > Backend Development > Explanation of python basic knowledge points
sudo apt-get install python-MySQLdb
import MySQLdb
conn = MySQLdb.connect(host="localhost",user="root",passwd="123456",db="test",charset="utf8")
commit(): If the database table is modified, submit to save the current data. Of course, if this user doesn't have permission and just does it, nothing will happen.
rollback(): If you have permission, cancel the current operation, otherwise an error will be reported.
cursor([cursorclass]): Cursor pointer.
cur = conn.cursor()
execute(query, args): Execute a single sql statement. query is the sql statement itself, and args is the list of parameter values. The return value after execution is the number of affected rows.
executemany(query, args): Execute a single sql statement, but repeatedly execute the parameters in the parameter list, and the return value is the number of affected rows
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")
:Receive all Return result rows.
: Receive size return result rows. If the value of size is greater than the number of returned result rows, then Will return cursor.arraysize pieces of data.
: Returns one result row.
: Move the pointer to a certain row. If mode='relative', it means moving the value bar from the current row. If mode='absolute', it means moving the value bar from the result set. The first line moves the value bar.<pre class="brush:php;toolbar:false">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()</pre>
or cur.scroll(n,"relative")
: means to move up or down relative to the current position , n is a positive number, which means downward (forward), n is a negative number, which means upward (backward)
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() #再关闭数据库
Section 17 Object-oriented
class 类名: 成员变量 成员函数 class MyClass(): first = 123 def fun(self): print "I am function"
if __name__ == "__main__": myClass = MyClass() #创建类的一个实例
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()
The above is the detailed content of Explanation of python basic knowledge points. For more information, please follow other related articles on the PHP Chinese website!