Ⅰ 請教個 python mysqldb commit的問題

任何應用都離不開數據,所以在學習python的時候,當然也要學習一個如何用python操作資料庫了。MySQLdb就是python對mysql資料庫操作的模塊。官方Introction : MySQLdb is an thread-compatible interface to the popular MySQL database server that provides the Python database API. 它其實相當於翻譯了對應C的介面。

使用這種資料庫介面大多是就是執行連接資料庫->執行query->提取數據->關閉連接 這幾個步驟。MySQLdb提供比較關鍵的對象,分別是Connection、Cursor、Result。具體使用步驟很簡單先不寫了,先寫一些個人認為比較重要、值得注意的地方。

1、雖然在MySQLdb.Connect(host ,user , passw , db)函數中,我們經常使用的只是這幾個參數,但是其實裡面還有很多比如字元集、線程安全、ssl等也都是很重要的參數,使用時要身份注意。

2、當使用Connection.query()函數進行query後,connection 對象可以返回兩種result,分別是store_result和use_result,store_result 將結果集存回client端,而use_result則是結果集保存在server端,並且維護了一個連接,會佔用server資源。此時,不可以進行任何其他的查詢。建議使用store_result,除非返回結果集(result set)過大或是無法使用limit的情形。

3、提取(fetch)數據的返回形式大多有三種情形。 as a tuple(how=0) ;as dictionaries, key=column or table.column if plicated(how=1);as dictionaries, key=table.column (how=2)

4、每次fetch,在result內部都會產生數據位置的移動,也就是說假如有10行數據,執行result.fetch_row(3,0),會得到前三行,再執行result.fetch_row(3,0),則會得到中間的三行,所以說fetch會導致position的移動。另外值得注意的是,如果使用use_result,也就是數據存儲在server時,在fetch所有的條目之前,不能進行任何的query操作。

5、mysql本身不支持游標(Cursor),但是MySQLdb對Cursor進行了模擬。重要的執行query方法有execute 和 executemany 。execute方法,執行單條sql語句,調用executemany方法很好用,資料庫性能瓶頸很大一部分就在於網路IO和磁碟IO將多個insert放在一起,只執行一次IO,可以有效的提升資料庫性能。游標cursor具有fetchone、fetchmany、fetchall三個方法提取數據,每個方法都會導致游標游動,所以必須關注游標的位置。游標的scroll(value, mode)方法可以使得游標進行卷動,mode參數指定相對當前位置(relative)還是以絕對位置(absolute)進行移動。

6、MySQLdb提供了很多函數方法,在官方指南里沒有完全羅列,使用者可以用help去看看,裡面提供了很多方便的東西。

7、對於mysql來說,如果使用支持事務的存儲引擎,那麼每次操作後,commit是必須的,否則不會真正寫入資料庫,對應rollback可以進行相應的回滾,但是commit後是無法再rollback的。commit() 可以在執行很多sql指令後再一次調用,這樣可以適當提升性能。

8、executemany處理過多的命令也不見得一定好,因為數據一起傳入到server端,可能會造成server端的buffer溢出,而一次數據量過大,也有可能產生一些意想不到的麻煩。合理,分批次executemany是個不錯的辦法。

最後,我自己寫了個pyMysql模塊,主要是對MySQLdb提供的常用方法進行了簡單的再次封裝,也藉此機會好好學習下MySQLdb,以及練習python的編碼。該程序使用的資料庫表,採用myisam引擎,所以沒加上commit(),一般最好還是要加上的。

Ⅱ svn的commit功能,怎麼用python實現

真不太清楚你什麼意思,好吧照我理解的來,內給你個容ci的def
def co_file(src):
co_cmd="svn co "+src
os.system(co_cmd)
#function: ci file with log comments
def ci_file():
current_path=os.getcwd()
svn_path=current_path+"/scm"
os.chdir(svn_path)
ci_cmd="svn ci -F comments.txt --force-log"
os.system(ci_cmd)
os.system("rm -f scm/*")
os.chdir(current_path)

Ⅲ Python 資料庫 insert操作不成功

importsqlite3
conn=sqlite3.connect('temple.db')
curs=conn.cursor()
curs.execute('createtableifnotexistsmovie(urlvarchar(256),timevarchar(40))')
a="http"
b="1992-11"
c="insertintomovievalues('"+a+"','"+b+"')"
d='insertintomovievalues("%s","%s")'%(a,b)
curs.execute(c)

conn.commit()
curs.close()
conn.close(

Ⅳ python怎麼往mysql資料庫添加數據

你最後加了commit了嗎?沒加commit不能插入數據額

Ⅳ 如何查看commit的內容

在push之前有時候會不放心是不是忘記加某些文件,或者是不是多刪了個什麼東西,這時候希望能夠看看上次commit都做了些什麼。
一開始想到的是用git diff,但是git diff用於當前修改尚未commit的時候較為方便,一旦commit後,需要指定上次節點的名稱(一個hash值),不方便。這種時候用git log更合適,因為commit的內容會以log來記錄。
下面記錄幾個常用的情境以及對應的命令。

僅僅想看最近誰有提交,以及提交的描述

對應命令 git log

顯示Sample
commit
Author: XXX
Date: Thu Nov 3 11:38:15 2011 +0800
fill author information in the head of files and format some code
commit
Author: XXX
Date: Thu Nov 3 04:05:34 2011 +0800
user management is mostly complete
details:
add support for account disable/enable
rewrite most related views to suit the above need
provide two decorators for access control (see README)
fixed many errors in Milestone 1
commit
Author: XXX
Date: Mon Oct 17 20:19:04 2011 -0400
fix the bug of get_ori_url_from_shorturl().
commit
Author: XXX
Date: Mon Oct 17 20:17:37 2011 -0400
fix the bug of get_article_from_short_url.

僅僅想看最後一次的提交

對應命令參數 -n 1

顯示Sample
commit
Author: XXX
Date: Thu Nov 3 11:38:15 2011 +0800
fill author information in the head of files and format some code

想看到最近一次提交所有更改過的文件

對應命令 git log -n 1 --stat

顯示Sample
commit
Author: XXX
Date: Thu Nov 3 11:38:15 2011 +0800
fill author information in the head of files and format some code
Site/accounts/decorators.py | 2 +-
Site/accounts/forms.py | 1 +
Site/accounts/models.py | 1 +
Site/accounts/readme | 3 ++-
Site/accounts/templates/account_activate.html | 1 +
Site/accounts/templates/account_disabled.html | 1 +


28 files changed, 37 insertions(+), 8 deletions(-)

想看到最近一次提交所有更改的細節

對應命令 git log -n 1 -p

顯示Sample
commit
Author: XXX
Date: Thu Nov 3 11:38:15 2011 +0800
fill author information in the head of files and format some code
diff --git a/Site/accounts/decorators.py b/Site/accounts/decorators.py
index 22522bc..a6bb440 100755
--- a/Site/accounts/decorators.py
+++ b/Site/accounts/decorators.py
@@ -1,9 +1,9 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
+# author: Rex Nov. 3, 2011
from functools import wraps
from django.core.urlresolvers import reverse
from django.http import HttpResponseRedirect
-from django.utils.decorators import available_attrs
from Site.accounts.models import UserProfile
def login_required(view_func):
diff --git a/Site/accounts/forms.py b/Site/accounts/forms.py
index 016710b..778d92a 100755
--- a/Site/accounts/forms.py
+++ b/Site/accounts/forms.py
@@ -1,5 +1,6 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
+# author: Rex Nov. 3, 201


有了這幾條命令,基本上對於想看最近更改的情境就可以應付過去了。最後一條並不很常用,如果有visual的工具可能更直觀些。

Ⅵ 怎麼用python去git commit

git是分為三部分,一部分是你自己的文件,另外一個是緩存區,最後一個是本地庫。當你修改了自己的文件後,你會git add xx將修改保存到緩存區,然後再用commit推送修改到本地庫中。

Ⅶ python操作oracle多少條數據commit一次比較好

LGWR進程按照順序寫在線日誌,中間不會跳躍,而且LGWR進程不會在同一個日誌快寫2次,即使一次寫入的日誌快只佔幾個位元組,下次不會再用了,這就造成日誌空間的浪費。Oracle做一次Commit,就會觸發LGWR進程進行日誌緩沖到日誌文件的寫入操作,因此可以說更改相同數據量的前提下,如果提交過於頻繁,產生的日誌可能就會越多,即使第一次Commit佔用的日誌塊仍可以存儲下一次需要寫入的日誌緩沖,那麼下一次Commit會再次佔用一個新的日誌塊。
實驗:
1、系統的日誌塊大小是512位元組。
SQL> select max(lebsz) from sys.x$kccle;
MAX(LEBSZ)
----------
512
2、創建兩張相同數據量的表。
SQL> select count(*) from t1;
COUNT(*)
----------
11188
SQL> select count(*) from t2;
COUNT(*)
----------
11188
3、查看刪除t1表前系統的浪費日誌空間量。
SQL> select name, value from v$sysstat where name like '%wastage%';NAME VALUE---------------------------------------------------------------- ----------redo wastage 2080604、逐條刪除t1表的記錄。
SQL> begin
2 for i in 1 .. 11188 loop
3 delete from t1 where rownum < 2;
4 commit;
5 end loop;
6 end;
7 /
5、再次查看日誌空間浪費量。
SQL> select name, value from v$sysstat where name like '%wastage%';NAME VALUE---------------------------------------------------------------- ----------redo wastage 1118740SQL> select 1118740-208060 from al;
1118740-208060
--------------
910680
浪費日誌空間量是910680位元組。
6、查看當前進程的SID。
SQL> select distinct sid from v$mystat;
SID
----------
215
進而查出當前進程消耗的redo量總大小。
SQL> select b.name, a.value from v$sesstat a, v$statname b2 where a.statistic#=b.statistic#
3 and b.name like '%redo size%'
4 and a.sid=215;
NAME VALUE
-------------------- ----------
redo size 9103304
可知日誌空間浪費比率有10%
SQL> select 910680/9103304 from al;
910680/9103304
--------------
.100038404
7、接下來選擇一次性刪除t2表記錄,之前記錄下日誌空間浪費大小。
SQL> select name, value from v$sysstat where name like '%wastage%';NAME VALUE
-------------------- ----------
redo wastage 1130636
SQL> delete from t2;
11188 rows deleted.
SQL> commit;
Commit complete.
8、查看當前日誌空間浪費。
SQL> select name, value from v$sysstat where name like '%wastage%';NAME VALUE
-------------------- ----------
redo wastage 1132060
9、計算日誌浪費空間比率。
SQL> select 1132060-1130636 from al;
1132060-1130636
---------------
1424
SQL> select b.name, a.value from v$sesstat a, v$statname b2 where a.statistic#=b.statistic#
3 and b.name like '%redo size%'
4 and a.sid=215;
NAME VALUE
-------------------- ----------
redo size 13154544
SQL> select 1424/13154544 from al;
1424/13154544
-------------
.000108252
從結果看,日誌空間浪費比率僅為0.01%。
結論:
1、LGWR進程按照順序將日誌緩沖寫入日誌塊,不會在同一個日誌塊中寫入兩次,就可能造成上一次寫入的最後一個日誌塊會有空間的浪費,但下一次不能再使用,只能再次寫入一個新的日誌塊。
2、相同更改數據量的前提下,多次提交Commit要比一次Commit浪費更多的日誌塊空間。

Ⅷ 如何進行版本回退或只是修改已提交的commit

同步程序思路:用戶提交程序到SVN,SVN觸發hooks,按不同的hooks進行處理,這里用到的是post-commit,利用post-commit到代碼檢出到SVN伺服器的本地硬碟目錄,再通過rsync同步到遠程的WEB伺服器上。知識點:1、SVN的hooks#start-commit提交前觸發事務#pre-commit提交完成前觸發事務#post-commit提交完成時觸發事務#pre-revprop-change版本屬性修改前觸發事務#post-revprop-change版本屬性修改後觸發事務通過上面這些名稱編寫的腳本就就可以實現多種功能了,相當強大。2、同步命令rsync的具體參數使用3、具有基個語言的編程能力bashpythonperl都可以實現post-commit具體實現細節post-commit腳本編輯文件:sudovim/home/svn/fitness/hooks/post-commit注意:編輯完成post-commit後,執行:sudochmod755post-commit內容:#!/bin/shexportLANG=zh_CN.UTF-8sudo/usr/bin/svnupdate/var/www/www--usernamemirze--password123456注意:svn倉庫文件夾的other必須要有執行許可權不然腳本無法執行。SVNupdate之前一定要先手動checkout一份出來,還有這里一定要添加用戶和密碼如果只是手動一樣會更新,但自動一樣的不行。

Ⅸ python 怎麼操作資料庫事務

#-*-coding:utf-8-*-
importsys
importMySQLdb
reload(sys)
sys.setdefaultencoding('utf-8')
classDB(object):
def__init__(self,host='127.0.0.1',port=3306,user='root',passwd='123',database=''):
self.__host=host
self.__port=port
self.__user=user
self.__passwd=passwd
self.__database=database
self.__open=False
print'__init__'

def__connect__(self):
ifself.__open==False:
print'connectdb...'
self.__conn=MySQLdb.connect(host=self.__host,port=self.__port,user=self.__user,passwd=self.__passwd,charset='utf8')
self.__open=Truedef__executeSql__(self,sql):
self.__connect__()
self.__executor=self.__conn.cursor(cursorclass=MySQLdb.cursors.DictCursor)
self.__executor.execute('use'+self.__database)#切換資料庫
returnself.__executor.execute(sql)

defexecuteQueryForObject(self,sql):
self.__executeSql__(sql)
returnself.__executor.fetchone()
'''
返回key=value字典
'''
defexecuteQueryAll(self,sql):
self.__executeSql__(sql)
returnself.__executor.fetchall()

defexecuteUpdate(self,sql='',isAutoCommit=False):
c=self.__executeSql__(sql)
ifisAutoCommit==True:
self.commit()#提交事務
returnc
'''
#提交事務
'''
defcommit(self):
self.__conn.commit()#提交事務

'''
#關閉資料庫,釋放資源
'''
defcloseDB(self):
ifnotself.__connisNone:
print'closedb...'
self.__conn.commit()#提交事務
self.__conn.close()
defprint_parameters(self):
printself.__user
printself.__passwd
printself.__host
printself.__port
'''
if__name__=='__main__':
db=DB(database='tb2013')
#db.print_parameters()
#db.executeSql('select*fromtb_user')
printdb.executeQueryForObject('selectcount(*)ascountfromtb_user')
_rows=db.executeQueryAll('selectuserid,nickfromtb_userlimit10');
print_rows
forrowin_rows:
printrow
print'nick:%s'%str(row['nick'])
printdb.executeUpdate(sql='updatetb_usersetnick='test'whereuserid=95084397',isAutoCommit=True)
db.closeDB()
'''