python控制mysql的API手記

來源:互聯網
上載者:User

標籤:

 

--------------------python控制mysql的API--------------------
#import MySQLdb:引用對應的開發包
#conn=MySQLdb.connect
(host=‘localhost‘,user=‘root‘,passwd=‘root‘,db=‘test‘,port=3306):建立資料
庫串連
#cur=conn.cursor():建立遊標

#cur.execute(self, query, args):執行單條sql語句,接收的參數為sql語句本身和
使用的參數列表,傳回值為受影響的行數
#cur.executemany(self, query, args):執行單挑sql語句,但是重複執行參數列表裡
的參數,傳回值為受影響的行數

#cursor用來執行命令的方法:
#cur.commit():提交。修改資料庫的時候需要在執行操作後,使用commit對資料庫
進行修改操作
#cur.rollback():復原

#cursor用來接受傳回值的方法:
#cur.fetchall(self):接收全部的返回結果行。
#cur.fetchmany(self, size=None):接收size條返回結果行.如果size的值大於返回
的結果行的數量,則會返回cursor.arraysize條資料。
#cur.fetchone(self):fetchone(self):返回一條結果行。
#cur.rowcount:擷取結果集的條數。
#cur.description:擷取連線物件的描述資訊。
#cur.rowcount:擷取影響了多少行。

#scroll(self, int, mode=‘relative‘):
int:移動的行數,整數;在相對模式下,正數向下移動,負值表示向上移動。
mode:移動的模式,預設是relative,相對模式;可接受absoulte,絕對模式。
#cur.close():進行遊標的關閉
#conn.close():進行資料庫連接的關閉操作
#except mdb.Error,e:
conn.rollback()

--------------------配置mysql所在作業系統進行遠程服務作業--------------------
1、建立新使用者:
mysql -uroot -p:登入到mysql中
use mysql:開啟對應的mysql資料庫
insert into mysql.user(Host,User,Password) values
("localhost","test","1234"):建立一個使用者(此處的"localhost",是指該使用者只
能在本地登入,不能在另外一台機器上遠程登入。如果想遠程登入的話,
將"localhost"改為"%",表示在任何一台電腦上都可以登入。也可以指定某台機器可
以遠程登入。)

2、許可權:
(1)這裡的意思是所有資料庫裡的所有表都授權給使用者
grant all privileges on testDB.* to [email protected] identified by
‘1234‘with grant option
grant select,delete,update,create,drop on *.* to [email protected]"%" identified
by "1234";

(2)flush privileges;:重新整理系統許可權表

注意:IDENTIFIED BY後面是你的mysql root使用者密碼

test使用者對所有資料庫都有select,delete,update,create,drop 許可權。
@"%" 表示對所有非本地主機授權,不包括localhost。(localhost地址設為
127.0.0.1)
對localhost授權:加上一句grant all privileges on testDB.* to
[email protected] identified by ‘1234‘;即可。


3、最後只要重啟mysql就行了
/etc/init.d/mysql restart

----------------------從ubuntu串連到win下的mysql的設定--------------------

mysql -uroot -p use mysql;

update user set host = ‘%‘ where user = ‘root‘;

flush privileges;

 

python控制mysql的API手記

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.