python的mysql模組使用

2021-08-19 18:01:25 字數 1841 閱讀 9883

如果還未安裝,我們可以使用以下命令安裝最新版的 pymysql:

$ pip install pymysql

$ git clone 

pymysql/pymysql

$ cd pymysql/

$ python3 setup.py install

mysql> create database testdb;

query ok, 1 row affected (0.00 sec)

mysql> use testdb;

database changed

mysql> create table testtable(id int ,username char(20));

query ok, 0 rows affected (0.01 sec)

mysql> insert into testtable values(20,'shan');

query ok, 1 row affected (0.00 sec)

mysql> insert into testtable values(40,'wu');

query ok, 1 row affected (0.00 sec)

mysql> select * from testtable;

+------+----------+

| id | username |

+------+----------+

| 10 | shanwu |

| 20 | shan |

| 40 | wu |

+------+----------+

3 rows in set (0.00 sec)

import pymysql

# 開啟資料庫連線

db = pymysql.connect(host='127.0.0.1', port=3306, user='root', passwd='123456', db='testdb', charset='utf8')

# 使用 cursor() 方法建立乙個游標物件 cursor

cursor = db.cursor()

# 使用 execute() 方法執行 sql 查詢

cursor.execute("select version()")

# 使用 fetchone() 方法獲取單條資料.

data = cursor.fetchone()

print("database version : %s " % data)

# 關閉資料庫連線

db.close()

#輸出[root@shanwu python]# python3 db.py

database version : 5.6.36

#!/usr/bin/env python

import pymysql

class

testmysql

(object):

def__init__

(self):

self.dbconfig =

conn = pymysql.connect(**self.dbconfig)

self.a = conn

defselect

(self):

print("select")

defupdate

(self):

print("update")

if __name__ == '__main__':

conn = testmysql()

python模組使用 python的模組使用

模組是python組織 的基本方式。乙個python指令碼可以單獨執行,也可以匯入到另乙個指令碼中執行,當指令碼被匯入執行時,我們將其稱為模組 module 所有的.py檔案都可以作為乙個模組匯入 模組名與指令碼的檔名相同 例如我們編寫了乙個名為hello.py的指令碼,則可以在另乙個指令碼中用im...

python商業專案 Python模組的使用

python模組的使用 python模組的使用 跨域請求 如果某個路由或者分組需要支援跨域請求,可以使用route get new id news read ext html allowcrossdomain 跨域請求一般會傳送一條options的請求,一旦設定了跨域請求的話,不需要自己定義opti...

收藏 python的paramiko模組使用

安裝 依賴模組 pycrypto the python cryptography toolkit wget wget 1 安裝pycrypto tar zxvf pycrypto 2.0.1.tar.gz cd pycrypto 2.0.1 python setup.py build sudo py...