標籤:
--------------------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手記