美文网首页
python 操作MySQL数据库

python 操作MySQL数据库

作者: qthink | 来源:发表于2017-03-09 10:04 被阅读0次

    1.工具

    MySQL,MySQL可视化工具navicat,Python3.5,pycharm

    2.代码

    # 创建连接
    
    conn = pymysql.connect(host='localhost',port=3306,user='root',passwd='root',db='lexicon',charset='utf8')
    
    # 创建游标
    
    cursor_neg = conn.cursor()
    
    cursor_neg.execute('select*from edu_01_neg')
    
    cursor_pos = conn.cursor()
    
    #定位到数据库中的两个表
    
    cursor_pos.execute('select*from edu_01_pos')
    
    # 获取剩余结果的第一行数据
    
    # row_1 = cursor.fetchall()
    
    # print(row_1)
    
    #读取两个表中的数据
    
    neg_cop=[]
    
    pos_cop=[]
    
    forr_negincursor_neg:
    
    # print(str(r[1]))
    
    neg_cop.append(str(r_neg[1]))
    
    for r_pos in cursor_pos:
    
    pos_cop.append(str(r_pos[1]))
    
    #关闭连接
    
    conn.commit()
    
    cursor_neg.close()
    
    cursor_pos.close()
    
    conn.close()
    

    PS:操作数据库之前需要首先打开数据库的

    相关文章

      网友评论

          本文标题:python 操作MySQL数据库

          本文链接:https://www.haomeiwen.com/subject/nohwgttx.html