zoukankan      html  css  js  c++  java
  • python操作mysql

    python通过MySQLdb模块编辑mysql数据库,python3使用pymysql模块

    添加数据

     1 import pymysql
     2 
     3 conn = pymysql.connect(host='127.0.0.1',user='root',passwd='xilehang99',db='liyaoguo')
     4 
     5 cur = conn.cursor()
     6 
     7 reCount = cur.execute('insert into students(name,sex,age,tel,nal) values(%s,%s,%s,%s,%s)',('jack','F',23,123456,'chain'))
     8 
     9 conn.commit()
    10 
    11 cur.close()
    12 conn.close()
    13 
    14 print (reCount)

    查看数据

     1 import pymysql
     2 
     3 conn = pymysql.connect(host='127.0.0.1',user='root',passwd='xilehang99',db='liyaoguo')
     4 
     5 cur = conn.cursor()
     6 
     7 reCount = cur.execute('select * from students')  #查询表内容
     8 
     9 conn.commit()
    10 
    11 cur.close()
    12 conn.close()
    13 
    14 print (reCount)
    15 print(cur.fetchone())  #获取一条结果
    16 print(cur.fetchmany(2))  #获取前两条结果
    17 print(cur.fetchall())  #获取所有结果

    修改数据

     1 import pymysql
     2 
     3 conn = pymysql.connect(host='127.0.0.1',user='root',passwd='xilehang99',db='liyaoguo')
     4 
     5 cur = conn.cursor()
     6 
     7 reCount = cur.execute('update students set Name = %s',('alin',))  #修改数据
     8 reCount = cur.execute('delete from students')  #删除数据
     9 
    10 conn.commit()
    11 
    12 cur.close()
    13 conn.close()
    14 
    15 print (reCount)
  • 相关阅读:
    将字符数组写到字符串
    两种比较不错的密码修改方案
    数组的应用 结构类型 使用深复制和浅复制 显示员工信息
    字符串处理技巧
    uva 1339
    11039
    uva 11636
    Uva401Palindromes
    poj2524-Ubiquitous Religions
    Poj1611The Suspects
  • 原文地址:https://www.cnblogs.com/yoyovip/p/5800014.html
Copyright © 2011-2022 走看看