python 操作 mysql資料庫很簡單
與其他語言的jdbc操作流程一致。
1、建立資料庫connection連線
2、建立游標
3、執行sql語句
4、提交事務
5、關閉connection連線。
import pymysql
#建立資料庫連線物件
db=pymysql.connect(host='192.168.240.165',user='root',passwd='123456',db='test',charset='utf8')
# 使用 cursor() 方法建立乙個游標物件 cursor
cursor = db.cursor()
# 使用 execute() 方法執行 sql 語句,也可以執行 insert,delete等操作
cursor.execute("select * from test")
#事務提交
#db.commit()
# 使用 fetchone() 方法獲取單條資料.
data = cursor.fetchone()
print ( data)
# 關閉資料庫連線
db.close()
python操作mysql查詢資料
首先需要連線資料庫,然後才查詢出資料。例如下表名字為 sinauser iduse id use name11 db12 2db233 db3class database def init self self.conn mysqldb.connect 連線資料庫 host 連線你要取出資料庫的ip,...
python操作MySQL資料庫
堅持每天學一點,每天積累一點點,作為自己每天的業餘收穫,這個文章是我在吃飯的期間寫的,利用自己零散的時間學了一下python操作mysql,所以整理一下。我採用的是mysqldb操作的mysql資料庫。先來乙個簡單的例子吧 import mysqldb try conn mysqldb.connec...
Python操作Mysql資料庫
coding utf8 author yangjing import mysqldb 查詢。def select host user root password port 3306,db sql connect mysqldb.connect host host,user user,passwd p...