Python MySQL

原文:https://www.cnblogs.com/longbigbeard/p/9309180.html
原文:https://www.runoob.com/python/python-mysql.html


import pymysql


try:
    
    # 打开数据库连接
    db = pymysql.connect("127.0.0.1", "root", "root", "test1", charset='utf8')
    
    # 获取操作游标 
    cursor = db.cursor()
    
    # 插入数据    
    value=(1, 'name1')
    sql = "INSERT INTO table1(id,name)VALUES(%s,%s)"
    cursor.execute(sql,value) #执行sql语句
    db.commit()
    
    # 查询数据
    select = "select * from table1"
    cursor.execute(select)
    line_count = cursor.fetchone()
    print(line_count)
    
    
    db.close()# 关闭连接
    print ("ok")


except:
    print("could not connect to mysql server")

posted @ 2020-05-14 14:13  古兴越  阅读(160)  评论(0编辑  收藏  举报