#python + Mysqldb
#Mpymysql 库名,关于数据库操作的库
#先进行安装这个库
#方法一:pip install pymysql
#方法二:在pycharm 中搜素pymysql
案例1:
import pymysql #或者引用pymysql db=pymysql.connect("192.168.153.131","root","123456","baoan",3306,charset='utf8') #连接数据库 cursor=db.cursor() #游标对象 sql="select * f rom dept where dept1=102" #sql语句 #sql0="select * from dept" #sql语句 #sql1="select count(*) from dept" #sql语句 #sql2="insert into dept(dept1,dept_name) values(106,'ittest') " #sql3="update dept set dept_name = 'it' where dept1=106" # sql4="delete from dept where dept1=106" # #cursor.execute(sql2) #执行语句 # #cursor.execute(sql3) # #cursor.execute(sql4) cursor.execute(sql) #查询更新后的数据 one=cursor.fetchone() #游标获取并占用一条数据 print (one)
import pymysql #或者引用pymysql db=pymysql.connect("192.168.153.131","root","123456","baoan",3306,charset='utf8') #连接数据库 cursor=db.cursor() #游标对象 #sql="select * f rom dept where dept1=102" #sql语句 sql0="select * from dept" #sql语句 #sql1="select count(*) from dept" #sql语句 #sql2="insert into dept(dept1,dept_name) values(106,'ittest') " #sql3="update dept set dept_name = 'it' where dept1=106" # sql4="delete from dept where dept1=106" # #cursor.execute(sql2) #执行语句 # #cursor.execute(sql3) # #cursor.execute(sql4) # cursor.execute(sql) #查询更新后的数据 # one=cursor.fetchone() #游标获取并占用一条数据 # print (one) # cursor.execute(sql4) #删除符合条件的数据 # cursor.execute(sql) #查询符合条件的数据 # two=cursor.fetchone() #获取占用这条数据 # print (two) cursor.execute(sql0) #执行语句 all=cursor.fetchall() #游标获取未被占用的所有数据. print (all)
插入数据
import pymysql #或者引用pymysql db=pymysql.connect("192.168.153.131","root","123456","baoan",3306,charset='utf8') #连接数据库 cursor=db.cursor() #游标对象 #sql="select * f rom dept where dept1=102" #sql语句 #sql0="select * from dept" #sql语句 #sql1="select count(*) from dept" #sql语句 sql2="insert into dept(dept1,dept_name) values(106,'ittest') " #sql3="update dept set dept_name = 'it' where dept1=106" # sql4="delete from dept where dept1=106" cursor.execute(sql2) #执行语句 # #cursor.execute(sql3) # #cursor.execute(sql4) # cursor.execute(sql1) #查询更新后的数据 one=cursor.fetchone() #游标获取并占用一条数据 print (one)
修改数据
import pymysql #或者引用pymysql
db=pymysql.connect("192.168.153.131","root","123456","baoan",3306,charset='utf8') #连接数据库
cursor=db.cursor() #游标对象
#sql="select * f rom dept where dept1=102" #sql语句
#sql0="select * from dept" #sql语句
#sql1="select count(*) from dept" #sql语句
#sql2="insert into dept(dept1,dept_name) values(106,'ittest') "
sql3="update dept set dept_name = 'it' where dept1=106"
# sql4="delete from dept where dept1=106"
#cursor.execute(sql2) #执行语句
cursor.execute(sql3)
#cursor.execute(sql4)
# cursor.execute(sql1) #查询更新后的数据
one=cursor.fetchone() #游标获取并占用一条数据
print (one
删除数据
import pymysql #或者引用pymysql db=pymysql.connect("192.168.153.131","root","123456","baoan",3306,charset='utf8') #连接数据库 cursor=db.cursor() #游标对象 #sql="select * f rom dept where dept1=102" #sql语句 #sql0="select * from dept" #sql语句 #sql1="select count(*) from dept" #sql语句 #sql2="insert into dept(dept1,dept_name) values(106,'ittest') " #sql3="update dept set dept_name = 'it' where dept1=106" sql4="delete from dept where dept1=106" #cursor.execute(sql2) #执行语句 #cursor.execute(sql3) cursor.execute(sql4) # cursor.execute(sql1) #查询更新后的数据 one=cursor.fetchone() #游标获取并占用一条数据 print (one)