• python mysql操作


    引入数据库的包

    import MySQLdb

    连接数据库
    conn=MySQLdb.connect(host='localhost',user='root',passwd='123456',db='test',port=3303,charset='utf8')
    #创建游标
    cur=conn.cursor()

    sql1="insert into user (title,name,content) values (%s,%s,%s)"

    #cur.executemany("insert into user (title,name,content) values (%s,%s,%s)",[('test05','z05','test content05'),('test06','zz06','test content06'),])
    #cur.execute("insert into user (title,name,content) values ('test05','z05','test content05'),('test06','zz06','test content06')")
    #conn.commit()
    #返回有影响的行数
    print cur.execute("select * from user")
    #返回全部
    print cur.fetchall()
    print cur.fetchall()
    #返回size条
    print cur.fetchmany(size)
    #返回一条
    print cur.fetchone()
    #从当前行移动到value
    cur.scroll(value,mode='relative')

    #从结果集移到value行
    cur.scroll(value,mode='absolute')

    踩过的坑:

    连接的时候端口号不能加引号,不然会报错:TypeError: an integer is required

    execute 和executemany 用法区别:

    cur.execute("insert into user (title,name,content) values ('test05','z05','test content05'),('test06','zz06','test content06')")

    cur.executemany("insert into user (title,name,content) values (%s,%s,%s)",[('test05','z05','test content05'),('test06','zz06','test content06'),])

  • 相关阅读:
    mycat 查询sql 报错
    mysql 主从 binlog
    数据库分库分表思路
    JavaScript数组知识
    JS判断当前页面是在 QQ客户端/微信客户端/iOS浏览器/Android浏览器/PC客户端
    js汉字转换为拼音
    工作中常用到的JS验证
    自动部署服务器代码
    php Excel 导入
    PHP 模拟http 请求
  • 原文地址:https://www.cnblogs.com/zhoufankui/p/5900073.html
Copyright © 2020-2023  润新知