python使用MySQLdb訪問mysql數(shù)據(jù)庫的方法
更新時間:2015年08月03日 12:46:29 作者:不是JS
這篇文章主要介紹了python使用MySQLdb訪問mysql數(shù)據(jù)庫的方法,實例分析了Python使用MySQLdb模塊操作mysql數(shù)據(jù)庫的相關技巧,具有一定參考借鑒價值,需要的朋友可以參考下
本文實例講述了python使用MySQLdb訪問mysql數(shù)據(jù)庫的方法。分享給大家供大家參考。具體如下:
#!/usr/bin/python import MySQLdb def doInsert(cursor,db): #insert # Prepare SQL query to INSERT a record into the database. sql = "UPDATE EMPLOYEE SET AGE = AGE+1 WHERE SEX = '%c'" %('M') try: cursor.execute(sql) db.commit() except: db.rollback() def do_query(cursor,db): sql = "SELECT * FROM EMPLOYEE \ WHERE INCOME > '%d'" % (1000) try: # Execute the SQL command cursor.execute(sql) # Fetch all the rows in a list of lists. results = cursor.fetchall() print 'resuts',cursor.rowcount for row in results: fname = row[0] lname = row[1] age = row[2] sex = row[3] income = row[4] # Now print fetched result print "fname=%s,lname=%s,age=%d,sex=%s,income=%d" % \ (fname, lname, age, sex, income ) except: print "Error: unable to fecth data" def do_delete(cursor,db): sql = 'DELETE FROM EMPLOYEE WHERE AGE > {}'.format(20) try: cursor.execute(sql) db.commit() except: db.rollback() def do_insert(cursor,db,firstname,lastname,age,sex,income): sql = "INSERT INTO EMPLOYEE(FIRST_NAME, \ LAST_NAME, AGE, SEX, INCOME) \ VALUES ('%s', '%s', '%d', '%c', '%d' )" % \ (firstname,lastname,age,sex,income) try: cursor.execute(sql) db.commit() except: db.rollback() # Open database connection # change this to your mysql account #connect(server,username,password,db_name) db = MySQLdb.connect("localhost","hunter","hunter","pydb" ) # prepare a cursor object using cursor() method cursor = db.cursor() do_query(cursor,db) doInsert(cursor,db) do_query(cursor,db) do_delete(cursor,db) do_query(cursor,db) do_insert(cursor,db,'hunter','xue',22,'M',2000) do_insert(cursor,db,'mary','yang',22,'f',5555) do_insert(cursor,db,'zhang','xue',32,'M',5000) do_insert(cursor,db,'hunter','xue',22,'M',333) do_query(cursor,db) # disconnect from server db.close()
希望本文所述對大家的Python程序設計有所幫助。
您可能感興趣的文章:
- Python3.7 pyodbc完美配置訪問access數(shù)據(jù)庫
- 詳解js文件通過python訪問數(shù)據(jù)庫方法
- 對Python通過pypyodbc訪問Access數(shù)據(jù)庫的方法詳解
- Python使用pyodbc訪問數(shù)據(jù)庫操作方法詳解
- Python輕量級ORM框架Peewee訪問sqlite數(shù)據(jù)庫的方法詳解
- Python的Tornado框架實現(xiàn)異步非阻塞訪問數(shù)據(jù)庫的示例
- Linux下通過python訪問MySQL、Oracle、SQL Server數(shù)據(jù)庫的方法
- python訪問mysql數(shù)據(jù)庫的實現(xiàn)方法(2則示例)
- Python訪問純真IP數(shù)據(jù)庫腳本分享
- 在Linux中通過Python腳本訪問mdb數(shù)據(jù)庫的方法
- Shell、Perl、Python、PHP訪問 MySQL 數(shù)據(jù)庫代碼實例
- python訪問純真IP數(shù)據(jù)庫的代碼
- 使用Python通過oBIX協(xié)議訪問Niagara數(shù)據(jù)的示例
相關文章
Pytorch - TORCH.NN.INIT 參數(shù)初始化的操作
這篇文章主要介紹了Pytorch - TORCH.NN.INIT 參數(shù)初始化的操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2021-02-02Tensorflow高性能數(shù)據(jù)優(yōu)化增強工具Pipeline使用詳解
這篇文章主要為大家介紹了Tensorflow高性能數(shù)據(jù)優(yōu)化增強工具Pipeline使用詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-11-11使用Python快速打開一個百萬行級別的超大Excel文件的方法
這篇文章主要介紹了使用Python快速打開一個百萬行級別的超大Excel文件的方法,本文通過實例代碼給大家介紹的非常想詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-03-03Python多線程同步Lock、RLock、Semaphore、Event實例
這篇文章主要介紹了Python多線程同步Lock、RLock、Semaphore、Event實例,Lock & RLock 用來確保多線程多共享資源的訪問,Semaphore用來確保一定資源多線程訪問時的上限,Event是最簡單的線程間通信的方式,需要的朋友可以參考下2014-11-11