python中的MySQLdb模組

2021-07-31 22:53:28 字數 1065 閱讀 9464

python中mysqldb模組的使用

python下連線資料庫訪問資料比c++方便多了,真是越來越愛python了。

#匯入模組

import mysqldb;

#連線資料庫,返回已連線的物件

conn = mysqldb.connect(host="localhost",user="root",passwd="791745",db="username");

#建立游標

cur = conn.cursor();

#選擇資料庫

conn.select_db('tpms');

#新增資料

cur.execute("insert into username(username,passwords) value('zhu','789654')");

#新增多條資料

sqil = "insert into username(username,passwords) value('zhu','789654')";

cur.executemany(sqil,[('hjk','456'),('jkljk','789')]);

#刪除資料

cur.execute("delete from username where username = 'cvst'");

#更新資料

cur.execute("update username set passwords = '789456' where username = 'jkljk'");

#之前的操作完後,雖然返回了正確的結果,但是資料庫中的數並沒有改變,因此需要提交至資料庫:

conn.commit();

#獲取搜尋結果中的第n條,按指標遞增

cur.fetchone();

#將結果指標返回到第一條

cur.scroll(0,'absolute');

#一次性獲取多條結果放在元組中

cur.fetchmany(11);

#關閉游標

cur.close();

#關閉連線

conn.close();

Python中MySQLdb的connect的用法

mysqldb模組是python連線mysql資料庫的乙個模組,在操作mysql資料庫是經常使用,在連線資料庫時connect是最常用的一種方法,這個方法有好多引數,總結了一下,主要有一下幾種 connect 方法用於連線資料庫,返回乙個資料庫連線物件。如果要連線乙個位於www.gyyx.com伺服...

Python中MySQLdb的connect的用法

mysqldb模組是python連線mysql資料庫的乙個模組,在操作mysql資料庫是經常使用,在連線資料庫時connect是最常用的一種方法,這個方法有好多引數,總結了一下,主要有一下幾種 connect 方法用於連線資料庫,返回乙個資料庫連線物件。如果要連線乙個位於www.gyyx.com伺服...

python下的MySQLdb使用

1.引入mysqldb庫 import mysqldb 2.和資料庫建立連線 conn mysqldb.connect host localhost user root passwd sa db mytable charset utf8 提供的connect方法用來和資料庫建立連線,接收數個引數,返...