zoukankan      html  css  js  c++  java
  • 六、MySQLdb 模块

    1. 基本代码如下:

    1) #导入 mysql包

     import MySQLdb

     

    2) #连接数据库,此处信息也可以放到配置文件

      db=MySQLdb.connect("localhost",“usename”,“userpw”,“dbname”,charset='utf8')

     

     3)#获取游标

     cursor=db.cursor()

     

    4)#执行sql语句

     cursor.execute("select * form student")

     

    5) #使用 fetchone() 获取一条数据

     info=cursor.fetchone()

     

    6) #使用 fetchall() 获取全部数据

     datas=cursor.fetchall()

     #定义空列表

     name=[]

     id=[]

     #将查询出来的名字和id分别放入name列表和id列表中

     for data in datas:

      id.append=row[0]

      name.append=row[1]

    2. 基础SQL语句:

     1)对表结构的操作(DDL)

      # 创建数据库

       create database db_name

      # 创建数据表

       create table table_name

      # 修改数据表

       alter table table_name    rename to  table_name_new    #修改表名

       alter table table_name    add column age int notnull       #增加列名

       alter table table_name    change age age_1      #修改列名

       alter table table_name    modify  age int(2)      #修改数据类型

       [note]: change用来字段重命名,不能修改字段类型和约束;
           modify不用来字段重命名,只能修改字段类型和约束

      # 删除数据表

      drop table table_name

     2)对表内容的操作(DML)

       # 查询语句

       select * from student where name='张三'

        # 修改语句

       update student set age=18 where name='张三'

        # 插入语句

       insert student (id,name) values('李四',‘17’)

        # 删除表数据

       delete form student where name='李四'

      

  • 相关阅读:
    Tarjan算法求解桥和边双连通分量(附POJ 3352 Road Construction解题报告)
    无向图求点割集的算法
    hdu 2121无根最小树形图要建一个虚拟节点
    hdu 1576扩展欧几里得算法
    欧几里德算法的扩展-求解不定方程
    hdu 3072 强连通+缩点+最小树形图思想
    1352 集合计数 扩展欧几里德算法
    1247 可能的路径 逆向思维
    Atcoder B
    C. Timofey and a tree 观察题 + dfs模拟
  • 原文地址:https://www.cnblogs.com/cj1138187197/p/12883797.html
Copyright © 2011-2022 走看看