cursor資料庫
❶ android資料庫怎麼樣把Cursor的值 賦值給字元串
調用cursor的getString方法(參考代碼見下面)
在while循環里,如果cursor.moveToNext()能移動到下一條
就代表游標對象里有數據。然後調用cursor的getString()方法把cursor的復制給字元串。
java">publicList<BlackBean>findAll(){
sqliteDatabasedb=helper.getReadableDatabase();
List<BlackBean>blackBeans=newArrayList<BlackBean>();
Cursorcursor=db.query("blackNumber",newString[]{"number","mode"},null,null,null,null,null);
while(cursor.moveToNext()){
BlackBeanblackNumberInfo=newBlackBean();
blackNumberInfo.setNumber(cursor.getString(0));
blackNumberInfo.setMode(cursor.getString(1));
blackBeans.add(blackNumberInfo);
}
cursor.close();
returnblackBeans;
}
❷ Cursor游標是什麼有什麼作用
在資料庫中,游標是一個十分重要的概念。游標提供了一種對從表中檢索出的數據進行操作的靈活手段,就本質而言,游標實際上是一種能從包括多條數據記錄的結果集中每次提取一條記錄的機制。游標總是與一條SQL選擇語句相關聯因為游標由結果集(可以是零條、一條或由相關的選擇語句檢索出的多條記錄)和結果集中指向特定記錄的游標位置組成。當決定對結果集進行處理時,必須聲明一個指向該結果集的游標。如果曾經用C語言寫過對文件進行處理的程序,那麼游標就像您打開文件所得到的文件句柄一樣,只要文件打開成功,該文件句柄就可代表該文件。對於游標而言,其道理是相同的。可見游標能夠實現按與傳統程序讀取平面文件類似的方式處理來自基礎表的結果集,從而把表中數據以平面文件的形式呈現給程序。我們知道關系資料庫管理系統實質是面向集合的,在MS SQL SERVER中並沒有一種描述表中單一記錄的表達形式,除非使用where子句來限制只有一條記錄被選中。因此我們必須藉助於游標來進行面向單條記錄的數據處理。由此可見,游標允許應用程序對查詢語句select返回的行結果集中每一行進行相同或不同的操作,而不是一次對整個結果集進行同一種操作;它還提供對基於游標位置而對表中數據進行刪除或更新的能力;而且,正是游標把作為面向集合的資料庫管理系統和面向行的程序設計兩者聯系起來,使兩個數據處理方式能夠進行溝通。在資料庫開發過程中,當你檢索的數據只是一條記錄時,你所編寫的事務語句代碼往往使用SELECT INSERT語句。但是我們常常會遇到這樣情況,即從某一結果集中逐一地讀取一條記錄。那麼如何解決這種問題呢?游標為我們提供了一種極為優秀的解決方案——那就是使用游標。
❸ 資料庫裡面靜態游標包含哪兩種類型
游標是SQL的一個內存工作區,由系統或用戶以變數的形式定義。游標的作用就是用於臨時存儲從資料庫中提取的數據塊。Oracle資料庫的Cursor類型包含三種: 靜態游標:分為顯式(explicit)游標和隱式(implicit)游標;REF游標:是一種引用類型,類似於指針。下面我們一一介紹它們的使用。
1.隱式游標
1)Select …INTO…語句,DML語句,使用隱式Cursor。此外,還有一種使用FOR LOOP的Implicit Cursor用法。
2)可以通過隱式Cusor的屬性來了解操作的狀態和結果。Cursor的屬性包含:
SQL%ROWCOUNT 整型代表DML語句成功執行的數據行數。
SQL%FOUND 布爾型值為TRUE代表插入、刪除、更新或單行查詢操作成功。
SQL%NOTFOUND 布爾型與SQL%FOUND屬性返回值相反。
SQL%ISOPEN 布爾型DML執行過程中為真,結束後為假。
3) 隱式Cursor由系統自動打開和關閉.
例如:
setserveroutputon
declare
begin
updateemployeessetemployee_name='Mike'whereemployee_id=1001;
ifSQL%FOUNDthen
dbms_output.put_line('Nameisupdated');
else
dbms_output.put_line('Nameisnotupdated');
endif;
end;
/
setserveroutputon
declare
begin
fortableInfoin(select*fromuser_tables)loop
dbms_output.put_line(tableInfo.table_name);
endloop;
exception
whenothersthen
dbms_output.put_line(sqlerrm);
end;
/
<p>setserveroutputon
declare
cursorcurisselect*fromuser_tables;
tableInfouser_tables%rowtype;
begin
opencur;
loop
fetchcurintotableInfo;
exitwhencur%notfound;
dbms_output.put_line(tableInfo.table_name);
endloop;</p><p>exception
whenothersthen
dbms_output.put_line(sqlerrm);</p><p>closecur;
end;
/</p>
setserveroutputon
declare
cursorcurisselect*fromuser_tables;
begin
fortableInfoincurloop
dbms_output.put_line(tableInfo.table_name);
endloop;
exception
whenothersthen
dbms_output.put_line(sqlerrm);
end;
/
<p>setserveroutputon
declare
cursorcur(tblNamevarchar2)isselect*fromuser_constraintswheretable_name=tblName;
tableInfouser_constraints%rowtype;
begin
opencur('EMPLOYEES');
loop
fetchcurintotableInfo;
exitwhencur%notfound;
dbms_output.put_line(tableInfo.constraint_name);
endloop;</p><p>exception
whenothersthen
dbms_output.put_line(sqlerrm);</p><p>closecur;
end;
/</p><p></p>
setserveroutputon
declare
cursorcur(tblNamevarchar2)isselect*fromuser_constraintswheretable_name=tblName;
begin
fortableInfoincur('EMPLOYEES')loop
dbms_output.put_line(tableInfo.constraint_name);
endloop;
exception
whenothersthen
dbms_output.put_line(sqlerrm);
end
/
setserveroutputon
declare
cursorcurisselect*fromemployeesforupdate;
begin
fortableInfoincurloop
=salary*1.1wherecurrentofcur;
endloop;
commit;
exception
whenothersthen
dbms_output.put_line(sqlerrm);
end;
/
createorreplacepackageTESTas
typeemployees_refcursor_%rowtype;
procereemployees_loop(employees_curINemployees_refcursor_type);
endTEST;
/
procereemployees_loop(employees_curINemployees_refcursor_type)is
empemployees%rowtype;
begin
loop
fetchemployees_curintoemp;
exitwhenemployees_cur%NOTFOUND;
dbms_output.put_line(emp.employee_id);
endloop;
endemployees_loop;
endTEST;
/
setserveroutputon
declare
empRefCurTEST.employees_refcursor_type;
begin
foriin10..20loop
dbms_output.put_line('DepartmentID='||i);
openempRefCurforselect*fromemployeeswheredepartment_id=i;
TEST.employees_loop(empRefCur);
endloop;
exception
whenothersthen
dbms_output.put_line(sqlerrm);
closeempRefCur;
end;
/
SQL>createtableemployees_tmpasselectfirst_name,last_name,salaryfromemployeeswhere0=1;
setserveroutputon
declare
cursoremployees_cur(depIdemployees.department_id%type)isselectfirst_name,last_name,_id=depId;
typeemployee_table_typeistableofemployees_cur%rowtypeindexbypls_integer;
employee_tableemployee_table_type;
begin
openemployees_cur(100);
fetchemployees_curbulkcollectintoemployee_table;
closeemployees_cur;
foriin1..employee_table.countloop
dbms_output.put_line(employee_table(i).first_name||''||employee_table(i).last_name||','||employee_table(i).salary);
endloop;
foralliinemployee_table.first..employee_table.last
insertintoemployees_tmpvalues(employee_table(i).first_name,employee_table(i).last_name,employee_table(i).salary);
commit;
end;
/
2.顯式游標
1) 顯式Cursor的屬性包含:
游標的屬性 返回值類型 意義
%ROWCOUNT 整型 獲得FETCH語句返回的數據行數
%FOUND 布爾型 最近的FETCH語句返回一行數據則為真,否則為假
%NOTFOUND 布爾型 與%FOUND屬性返回值相反
%ISOPEN 布爾型 游標已經打開時值為真,否則為假
2) 對於顯式游標的運用分為四個步驟:
a 定義游標---Cursor [Cursor Name] IS;
b 打開游標---Open [Cursor Name];
c 操作數據---Fetch [Cursor name]
d 關閉游標---Close [Cursor Name]
以下是幾種常見顯式Cursor用法。
還可以使用帶參數open的cursor。
可以使用WHERE CURRENT OF子句執行UPDATE或DELETE操作。
3.REF CURSOR(Cursor Variables)
REF Cursor在運行的時候才能確定游標使用的查詢。利用REF CURSOR,可以在程序間傳遞結果集(一個程序里打開游標變數,在另外的程序里處理數據)。
也可以利用REF CURSOR實現BULK SQL,提高SQL性能。
REF CURSOR分兩種,Strong REF CURSOR 和 Weak REF CURSOR。
Strong REF CURSOR:指定retrun type,CURSOR變數的類型必須和return type一致。
Weak REF CURSOR:不指定return type,能和任何類型的CURSOR變數匹配。
Ref cursor的使用:
1) Type [Cursor type name] is ref cursor
2) Open cursor for...
3) Fetch [Cursor name]
4) Close Cursor
例如:
Step1:
Step2:
Step3:
4.BULK SQL
使用FORALL和BULK COLLECT子句。利用BULK SQL可以減少PLSQL Engine和SQL Engine之間的通信開銷,提高性能。
1. To speed up INSERT, UPDATE, and DELETE statements, enclose the SQL statement within a PL/SQL FORALL statement instead of a loop construct. 加速INSERT, UPDATE, DELETE語句的執行,也就是用FORALL語句來替代循環語句。
2. To speed up SELECT statements, include the BULK COLLECT INTO clause in the SELECT statement instead of using INTO. 加速SELECT,用BULK COLLECT INTO 來替代INTO。
❹ python連接MySQL資料庫問題 cursor( ) 、execute()和fetc
MySQLdb.connect是python 連接MySQL資料庫的方法,在Python中 import MySQLdb即可使用,至於connect中的參數很簡單:x0dx0ahost:MySQL伺服器名x0dx0auser:資料庫使用者x0dx0apassword:用戶登錄密碼x0dx0adb:操作的資料庫名x0dx0acharset:使用的字元集(一般是gb2312)x0dx0acursor = db.cursor() 其實就是用來獲得python執行Mysql命令的方法,也就是x0dx0a我們所說的操作游標x0dx0a下面cursor.execute則是真正執行MySQL語句,即查詢TABLE_PARAMS表的數據。x0dx0a至於fetchall()則是接收全部的返回結果行 row就是在python中定義的一個變數,用來接收返回結果行的每行數據。同樣後面的r也是一個變數,用來接收row中的每個字元,如果寫成C的形式就更好理解了x0dx0afor(string row = '' row<= cursor.fetchall(): row++)x0dx0a for(char r = '' r<= row; r++)x0dx0aprintf("%c", r);x0dx0a大致就是這么個意思!