python鏈結mysql資料庫

2021-07-11 18:40:30 字數 637 閱讀 1320

安裝mysql-python包

$ sudo apt-get install python-mysqldb
在程式中使用

db = connect(url, user, password, database)

cursor = db.cursor()#獲取游標

sql = "create table employee (first_name char(20) not null,last_name char(20))"

cursor.execute(sql)#執行語句

print cursor.fetchall() #獲得返回值

execute(self, query, args) #執行單條sql語句,接收的引數為sql語句本身和使用的引數列表,返回值為受影響的行數

cursor用來接收返回值的方法

fetchall(self)#接收全部的返回結果行.

fetchmany(self, size=none)#接收size條返回結果行.如果size的值大於返回的結果行的數量,則會返回cursor.arraysize條資料

fetchone(self)#返回一條結果行

python鏈結MySQL資料庫

python鏈結mysql資料庫。用到mysqldb庫,安裝 pip install mysql python 我這邊安裝完報錯 error command c program files microsoft visual studio 14.0 vc bin cl.exe failed with ...

Python 鏈結MySQL資料庫

參考資料 這裡使用的是庫是pymysql python鏈結資料庫時,mysql中要有要連線的資料庫,否則會無法連線。import pymysql import movie host localhost 主機名 user root 使用者名稱 password 密碼 database douban2 ...

python自學(三)鏈結mysql,查詢資料

1 安裝mysql python 1.2.5.win32 py2.7 2 安裝是發現之前安裝的python版本是3.6.0,但是mysql驅動只支援道2.7,沒辦法,解除安裝重新安裝,也好,加深印象。3 安裝python2.7後成功安裝mysql python 1.2.5.win32 py2.7,e...