• python的数据库链接


    https://blog.csdn.net/canofy/article/details/83294330
    #-*-coding:utf-8-*-
    import MySQLdb

    #yum update python linux下更新python的命令

    #获取数据库连接
    def getConn():
    host="192.168.0.237"
    username="root"
    pwd="root"
    database="cn_ad_issue"
    return MySQLdb.connect(host=host,user=username,passwd=pwd,db=database)


    #根据sql查询数据库,获取的数据是tuple类型
    def getBySql(sql):
    conn=getConn()
    cursor=conn.cursor()
    cursor.execute(sql)
    result=cursor.fetchall()
    cursor.close()
    conn.close()
    return result

    #执行一条sql语句,可以上update,delete以及insert
    #"insert into t_userinfo(login_name,password,status,role,role_name,note) values(%s,%s,%s,%s,%s,%s)"
    #param=(("bbb","bbb","a",1,"aaa","bbb"),("bbb","bbb","a",1,"aaa","bbb"))
    #"update t_userinfo set login_name='ddd' where login_name='bbb'"
    #"delete from t_userinfo where login_name='ddd'"
    def execute(sql):
    conn=getConn()
    cursor=conn.cursor()
    n=cursor.execute(sql)
    # n=cursor.executemany(sql,param)
    cursor.close()
    conn.close()
    return n

    #每个值的集合为一个tuple,整个参数集组成一个tuple,或者list
    #使用executemany方法来批量的插入数据
    #批量添加, param为tuple类型,如: param=(("bbb","bbb","a",1,"aaa","bbb"),("bbb","bbb","a",1,"aaa","bbb"))
    #返回添加的条数
    def insertBatch(sql,param):
    conn=getConn()
    cursor=conn.cursor()
    n=cursor.executemany(sql,param)
    cursor.close()
    conn.close()
    return n


    #sql="delete from t_userinfo where login_name='ddd'"
    #param=(("t",),("z",))
    #n=cursor.executemany(sql,param)
    #print insertBatch(sql,param)
    #execute(sql)

    li=[]
    subli=[]
    tuple1=getBySql("select * from t_userinfo")
    #li.append(tuple1) # append方法只把tuple作为一个整体添加到list中
    li.extend(tuple1) # extend方法会把tuple(元组)转换成list(列表)
    count=len(tuple1)
    #for subtuple1 in tuple1:
    # li.extend(subtuple1);//每一个数据都成为了list中的一项
    # print subtuple1;
    # subcount=len(subtuple1)
    # for key in subtuple1:
    # print key
    # print 'Wn'

    print li
    for iter in li:
    print iter;
  • 相关阅读:
    [Linux]
    [.Net]
    [.Net]
    [Linux]
    [Google]
    面向对象的7个基本设计原则
    windows SDK中的wininet写http客户端
    C++ 用libcurl库进行http通讯网络编程
    感悟
    关于Windows高DPI的一些简单总结
  • 原文地址:https://www.cnblogs.com/fengff/p/10310782.html
Copyright © 2020-2023  润新知