zoukankan      html  css  js  c++  java
  • python写入mysql

     
    import pymysql
    conn = pymysql.connect(host='192.168.70.129',port=3306,user='root',passwd='123456',db='my') #创建连接
    cursor = conn.cursor()  #创建游标
    raw = cursor.execute("select * from rruinfo")   #执行sql语句,返回影响行数
    print raw
    row_1 = cursor.fetchone()  #取一条
    row_1 = cursor.fetchmany(3)  #取3条
    row_1 = cursor.fetchall()  #取全部
    '''
    注:在fetch数据时按照顺序进行,可以使用cursor.scroll(num,mode)来移动游标位置,如:
    •cursor.scroll(1,mode='relative')  # 相对当前位置移动
    •cursor.scroll(2,mode='absolute') # 相对绝对位置移动
    
    '''
    print row_1
    effect_row = cursor.executemany("insert into rruinfo(rruname,len,age,updatetime)values(%s,%s,%s,%s)", [("1.1.1.11",11,1,1),("1.1.1.11",2,1,1)])
    conn.commit()#使用executemany时候需要commit一下,不然不生效
    new_id = cursor.lastrowid  # 获取最新自增ID
    print raw
    cursor.close() #关闭游标
    conn.close()   #关闭连接
    
    import pymysql
    #conn = pymysql.connect(host='127.0.0.1', port=3306, user='root', passwd='123', db='t1')
    # 游标设置为字典类型
    cursor = conn.cursor(cursor=pymysql.cursors.DictCursor)
    #r = cursor.execute("call p1()")
    raw = cursor.execute("select * from rruinfo")         #不好使
    result = cursor.fetchone()
    print '-----------------------------------'
    print result
    conn.commit()
    cursor.close()
    conn.close()
    
  • 相关阅读:
    JS放在head和放在body中的区别
    模板模式(Template Pattern)
    原型模式
    Linux下的头文件搜索路径
    How to Change the Default Theme Appearance [editing with no theme]
    版本控制
    What is libacl.so.1 ?
    交叉编译器 arm-linux-gnueabi 和 arm-linux-gnueabihf 的区别
    mount --bind 的妙用
    mount的bind选项
  • 原文地址:https://www.cnblogs.com/qiangayz/p/8667397.html
Copyright © 2011-2022 走看看