Python基礎之Mysql
阿新 • • 發佈:2017-06-28
數據 obj 調用 cnblogs 基礎 execute inf wrr sel
1.MySQLdb 是用於Python鏈接Mysql數據庫的接口,它實現了 Python 數據庫 API 規範 V2.0,基於 MySQL C API 上建立的。
2.Python下MySQLdb安裝。
(1)先下載MySQLdb安裝包,解壓。
(2)cmd以管理員身份運行,進行MySQLdb目錄,將MySQLdb安裝至Python下:python setup.py install
He.py代碼:
#coding=utf-8 import pymysql conn=pymysql.connect(host="127.0.0.1",port=3306,user="root",passwd="root",db="dgcms",charset="utf8") cur=conn.cursor() sql="select * from blog" cur.execute(sql) rows = cur.fetchall() for dr in rows: print(dr)
調用代碼:
Python 3.6.1 (v3.6.1:69c0db5, Mar 21 2017, 17:54:52) [MSC v.1900 32 bit (Intel)] on win32 Type "copyright", "credits" or "license()" for more information.>>> from jinjiemysql import * (7, ‘VB.NET‘, ‘VB.NET‘) (3, ‘go go‘, ‘er3r53r5‘) (4, ‘ C#‘, ‘RWRRWWW‘) (6, ‘object c‘, ‘object c‘) (5, ‘python‘, ‘haaaa‘) >>>
Python基礎之Mysql