zoukankan      html  css  js  c++  java
  • python--如何操作表

    >>> import MySQLdb
    >>> conn=MySQLdb.connect(user='admin',passwd='',host='192.168.31.212')
    >>> cur=conn.cursor() #创建游标
    >>> conn.select_db('zsw')
    第一种方法插入数据>>> cur.execute("insert into person(number,name,birthday) value(1,'zhaosunwei','1984-11-28')")
    1L
    第二种方法插入数据(推荐)>>> sqli="insert into person(number,name,birthday) value(%s,%s,%s)"
    >>> cur.execute(sqli,(3,'liujilei','1982.05.06'))
    1L
    插入多条数据>> sqli="insert into person(number,name,birthday) values(%s,%s,%s)"
    >>> cur.executemany(sqli,[(4,'sunjingbo','1964.08.01'),(5,'cuizhongjun','1964,11,29')])
    2L
    删除数据>>> cur.execute('delete from person where number=4')
    1L
    更新数据>>> cur.execute("update person set name='sunbo' where number=3")
    1L
    选择数据>>> cur.execute("select * from person")
    4L
    一条一条的取数据>>> cur.fetchone()
    (1L, 'zhaosunwei', datetime.date(1984, 11, 28))
    >>> cur.fetchone()
    (2L, 'cuixiuxiu', datetime.date(1987, 8, 13))
    >>> cur.fetchone()
    (3L, 'sunbo', datetime.date(1982, 5, 6))
    >>> cur.fetchone()
    (5L, 'cuizhongjun', datetime.date(1964, 11, 29))
    >>> cur.fetchone()
    游标移到最开始位置>>> cur.scroll(0,'absolute')
    >>> cur.fetchone()
    (1L, 'zhaosunwei', datetime.date(1984, 11, 28))
    多条数据同时取>>> cur.fetchmany(cur.execute("select * from person"))
    ((1L, 'zhaosunwei', datetime.date(1984, 11, 28)), (2L, 'cuixiuxiu', datetime.date(1987, 8, 13)), (3L, 'sunbo', datetime.date(1982, 5, 6)), (5L, 'cuizhongjun', datetime.date(1964, 11, 29)))
    游标关掉>>> cur.close()
    链接关掉>>> conn.close()
  • 相关阅读:
    数据结构第四篇——线性表的链式存储之双向链表
    基本概念之引用赋值需要注意什么?
    基本概念之将引用作为函数的参数有哪些特点?
    基本概念之什么是引用?
    基本概念之模板类有什么优势?
    我的第一篇博文
    为CentOS 6 配置本地YUM源
    为CentOS 6 配置本地YUM源
    poj 1990 MooFest
    [置顶] 学生信息管理系统“重复设置”问题
  • 原文地址:https://www.cnblogs.com/zhaosunwei/p/7230336.html
Copyright © 2011-2022 走看看