zoukankan      html  css  js  c++  java
  • 学习笔记十

      今天转战python了,学习了python如何使用pip导入库(pip install 库),并使用python连接了mysql数据库,对其进行了增删改查操作。

     1 import pymysql
     2 
     3 # 打开连接
     4 
     5 
     6 def open_conn(dbname):
     7     db = pymysql.connect(
     8         host="localhost",
     9         port=3306,
    10         user="root",
    11         passwd="密码",
    12         db=dbname,
    13         charset="utf8")
    14 
    15     return db
    16 
    17 # 遍历查询
    18 
    19 
    20 def query(db):
    21 
    22     cursor = db.cursor()
    23     sql = "select * from demo"
    24     cursor.execute(sql)
    25     for each in cursor.fetchall():
    26         print(each)
    27 
    28 # 增加数据
    29 
    30 
    31 def add(db):
    32 
    33     cursor = db.cursor()
    34     sql = "insert into demo(name,sex,phone) values('小张','男','1234567')"
    35     cursor.execute(sql)
    36     db.commit()
    37     # 查看插入后的结果
    38     sql = "select * from demo where name ='小张'"
    39     cursor.execute(sql)
    40     for each in cursor.fetchall():
    41         print(each)
    42 
    43 # 修改数据
    44 
    45 
    46 def update(db):
    47     cursor = db.cursor()
    48     sql = " update demo set sex='女' where name='小张'"
    49     cursor.execute(sql)
    50     db.commit()
    51     # 查看更新后的结果
    52     sql = "select * from demo where name='小张'"
    53     cursor.execute(sql)
    54     for each in cursor.fetchall():
    55         print(each)
    56 
    57 
    58 # 删除数据
    59 
    60 def delete(db):
    61     cursor = db.cursor()
    62     sql = " delete from demo where name='小张'"
    63     cursor.execute(sql)
    64     db.commit()
    65     # 查看更新后的结果
    66     sql = "select * from demo where name='小张'"
    67     cursor.execute(sql)
    68     for each in cursor.fetchall():
    69         print(each)
    70 
    71 
    72 if __name__ == '__main__':
    73     # query(open_conn("dbname"))
    74     # add(open_conn("dbname"))
    75     # update(open_conn("dbname"))
    76     # delete(open_conn("dbname"))
    77     pass
    增删改查

      以下为效果截图:

      增添操作:

      

       修改操作:

      

       遍历查询操作:

      

       删除操作:

      

  • 相关阅读:
    poj3693 Maximum repetition substring (后缀数组+rmq)
    spoj687 REPEATS
    bzoj3626: [LNOI2014]LCA (树链剖分+离线线段树)
    bzoj2243 [SDOI2011]染色 (树链剖分+线段树)
    SPOJ QTREE- Query on a tree (树链剖分)
    hdu5662 YJQQQAQ and the function (单调栈)
    hdu4348 To the moon (主席树 || 离线线段树)
    hdu3565 Bi-peak Number (有上界和下界的数位dp)
    修改文件上传大小限制
    强制不按行
  • 原文地址:https://www.cnblogs.com/liyuchao/p/12316704.html
Copyright © 2011-2022 走看看