Python新增数据到数据库

'''
数据库的-新增-更新-删除

'''

from pymysql import *
# import pymysql

def option_db():
    try:
        conn = connect(host='127.0.0.1', port=3306, user='root',passwd='root',
                       db='python03', charset='utf8')

        cursor = conn.cursor()

        sql = "insert into students('name') values('老王1');"
        cursor.execute(sql)

        sql = "insert into students('name') values('老王2');"
        cursor.execute(sql)

        # 提交事务
        conn.commit()

        cursor.close()
        conn.close()

    except Exception as e:
        print('error')
        # 部分提交   执行成功
        # conn.commit()

        # 回滚  银行转账
        conn.rollback()

if __name__ == '__main__':
    option_db()
    
    

你可能感兴趣的:(Python新增数据到数据库)