先說點題外話,偷懶了一個多月了,今天正式回歸,今年要多學習點知識,一定要找個碼農的工作
首先要安裝python MySQLdb模塊,直接安裝就行了 32位:https://pypi.python.org/pypi/MySQL-python/1.2.5 64位:http://arquivos.victorjabur.com/python/modules/MySQL-python-1.2.3.win-amd64-py2.7.exe 2.連接測試import MySQLdb 分別為地址,用戶名,密碼,數據庫名稱db = MySQLdb.connect("localhost","root","112233","jike" )cursor = db.cursor()cursor.execute("SELECT VERSION()")data = cursor.fetchone()3測試查詢import MySQLdbdb = MySQLdb.connect("localhost","root","112233","jike" )cursor = db.cursor()cursor.execute("SELECT * from usertable")row = int(cursor.rowcount)print rowdata=cursor.fetchall()for da in data: print 'userid=%s ,username=%s' %dadb.close()結果 4 userid=2 ,username=shui userid=3 ,username=cao userid=4 ,username=zhao userid=5 ,username=zhao
4.測試添加,更新,刪除 默認是開啟事務的,必須提交后才能更新到數據庫中。
import MySQLdbdb = MySQLdb.connect("localhost","root","112233","jike" )cursor = db.cursor()cursor.execute("insert into usertable(userid,username) values(5,'zhao')")print cursor.rowcountcursor.execute("update usertable set username='cao' where userid=3")print cursor.rowcountcursor.execute("delete from usertable where userid=1")print cursor.rowcountdb.commit()db.close()新聞熱點
疑難解答