数据库连接:
连接数据库前,请先确认以下事项:
1.您已经创建了数据库school。
2.在school数据库中您已经创建了表test1。
3.test1表字段为 id,name,sex。
4.连接数据库school使用的用户名为 "root" ,密码为 "123456",你可以可以自己设定或者直接使用root用户名及其密码,Mysql数据库用户授权请使用Grant命令。
5.在你的机子上已经安装了pymysql。
创建数据库表:
在数据库表中添加内容:
在数据库表中删除内容:
在数据库表中修改内容:
数据库查询:
Python查询Mysql使用 fetchone()方法获取单条数据, 使用fetchall()方法获取多条数据。
fetchone(): 该方法获取下一个查询结果集。结果集是一个对象。
fetchall(): 接收全部的返回结果行。
rowcount: 这是一个只读属性,并返回执行execute()方法后影响的行数。
结果:
id:2,name:张三,sex:男
id:3,name:瑞克,sex:男
id:4,name:王五,sex:男
连接数据库前,请先确认以下事项:
1.您已经创建了数据库school。
2.在school数据库中您已经创建了表test1。
3.test1表字段为 id,name,sex。
4.连接数据库school使用的用户名为 "root" ,密码为 "123456",你可以可以自己设定或者直接使用root用户名及其密码,Mysql数据库用户授权请使用Grant命令。
5.在你的机子上已经安装了pymysql。
创建数据库表:
- import pymysql
- def createtable():
- #1.建立连接
- conn=pymysql.connect('localhost','root','123456','school')
- #2.得到cursor对象
- mycursor=conn.cursor()
- #3.执行sql语句
- sqlstr='''''
- create table test1(
- id int PRIMARY KEY auto_increment,
- name varchar(20) not null,
- sex char(2)
- )DEFAULT CHARSET UTF8
- '''
- mycursor.execute(sqlstr)
- #4.获取执行结果,如果是创建表的语句则返回none
- mycursor.fetchone()
- #5.关闭连接对象
- conn.close()
- createtable()
- import pymysql
- def inserttable():
- conn = pymysql.connect('localhost', 'root', '123456', 'school')
- mycursor = conn.cursor()
- sqlstr='''''
- insert into test1(name,sex) values('李四','男')
- '''.encode('utf8')
- try:
- # 执行sql语句
- mycursor.execute(sqlstr)
- # 提交到数据库执行
- conn.commit()
- except:
- # 如果发生错误则回滚
- conn.rollback()
- conn.close()
- inserttable()
- import pymysql
- #提取公共连接方法
- def getconn():
- conn = pymysql.connect('localhost', 'root', '123456', 'school')
- mycursor=conn.cursor()
- return [conn,mycursor]
- def deltable():
- conn=getconn()
- sqlstr='''''delete from test1 WHERE id=1'''
- try:
- print(conn[1].execute(sqlstr))
- conn[0].commit()
- except:
- conn[0].rollback()
- conn[0].close()
- deltable()
- import pymysql
- #提取公共连接方法
- def getconn():
- conn = pymysql.connect('localhost', 'root', '123456', 'school')
- mycursor=conn.cursor()
- return [conn,mycursor]
- def updatetable():
- conn=getconn()
- sqlstr='''''update test1 set NAME='瑞克' WHERE id=3'''
- try:
- print(conn[1].execute(sqlstr))
- conn[0].commit()
- except:
- conn[0].rollback()
- conn[0].close()
- updatetable()
- import pymysql
- #提取公共连接方法
- def getconn():
- conn = pymysql.connect('localhost', 'root', '123456', 'school')
- mycursor=conn.cursor()
- return [conn,mycursor]
- def operationtable(sqlstr):
- conn=getconn()
- try:
- print(conn[1].execute(sqlstr))
- conn[0].commit()
- except:
- conn[0].rollback()
- conn[0].close()
- #删除
- #sqlstr='''delete from test1 WHERE id=1'''
- #修改
- #sqlstr='''update test1 set NAME='瑞克' WHERE id=3'''
- #增加
- sqlstr='''''insert into test1(name,sex) values('王五','男')'''
- operationtable(sqlstr)
数据库查询:
Python查询Mysql使用 fetchone()方法获取单条数据, 使用fetchall()方法获取多条数据。
fetchone(): 该方法获取下一个查询结果集。结果集是一个对象。
fetchall(): 接收全部的返回结果行。
rowcount: 这是一个只读属性,并返回执行execute()方法后影响的行数。
- import pymysql
- #提取公共连接方法
- def getconn():
- conn = pymysql.connect('localhost', 'root', '123456', 'school')
- mycursor=conn.cursor()
- return [conn,mycursor]
- class Test():
- def __init__(self,id=None,name=None,sex=None):
- self.id=id
- self.name=name
- self.sex=sex
- def __str__(self):
- return 'id:'+str(self.id)+',name:'+self.name+',sex:'+self.sex
- #查询
- def querytable():
- list=[]
- conn=getconn()
- sqlstr='''''select id,name,sex from test1'''
- conn[1].execute(sqlstr)
- rs=conn[1].fetchall()
- for row in rs:
- test=Test(row[0],row[1],row[2])
- list.append(test)
- conn[0].close()
- return list
- testlist=querytable()
- for row in testlist:
- print(row)
id:2,name:张三,sex:男
id:3,name:瑞克,sex:男
id:4,name:王五,sex:男