1. 程式人生 > >python3 對MySQL資料的增刪改查

python3 對MySQL資料的增刪改查

# /usr/bin/python3.7.0
# -*- coding:utf-8 -*-
import json
import urllib2
import urllib
import pymysql
 


def doMysql():
    #使用者名稱:root 密碼:123456 資料庫名:testpy 埠預設是3306

    db = pymysql.connect("localhost", "root","123456","testpy",charset="utf8")
    cursor = db.cursor()
    
    try:
        print('try')
        sql ="INSERT INTO `users` (`email`, `password`) VALUES ('222', '123456')"
        cursor.execute(sql)
        cursor.close()
        db.commit(); #提交操作
     
        print("success")
    except:
        db.rollback();
        print('false')
    db.close()
          

 

def main():
        doMysql()


if __name__ == "__main__":
    main()