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大致就是这么个意思!