0% found this document useful (0 votes)
28 views

Cursor & Content Value

Cursors are used to retrieve data from SQLite databases and point to individual rows. ContentValues allows values to be stored with string keys and passed to content providers. Content providers manage access to centralized data and support basic CRUD operations through methods like query(), insert(), update(), and delete().

Uploaded by

uttamrao
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
28 views

Cursor & Content Value

Cursors are used to retrieve data from SQLite databases and point to individual rows. ContentValues allows values to be stored with string keys and passed to content providers. Content providers manage access to centralized data and support basic CRUD operations through methods like query(), insert(), update(), and delete().

Uploaded by

uttamrao
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

Cursor & Content Value

Cursor
● Cursor is used to Retrieving the data from SQLite Database
● The basic purpose of a cursor is to point to a single row of the result fetched by the query.
● We load the row pointed by the cursor object.
● By using cursor we can save lot of ram and memory.
● Cursor is the Interface which represents a 2 dimensional table of any database.
● When you try to retrieve some data using SELECT statement, then the database will first create a
CURSOR object and return its reference to you.
● To use Cursors android.database. Cursor must be imported.
● Cursors store query result records in rows and grant many methods to access and iterate through the
records.
● Cursors should be closed when no longer used, and will be deactivated with a call to Cursor.deactivate()
when the application pauses or exists.
● On resume the Cursor.requery statement is executed to re-enable the Cursor with fresh data.
● These functions can be managed by the parent Activity by calling startManagingCursor().
Public Methods Cursor

abstract close() Closes the Cursor, releasing all of its resources and
void making it completely invalid.

abstract copyStringToBuffer(int Retrieves the requested column text and stores it in the
void columnIndex, buffer provided.
CharArrayBuffer buffer)

abstract int getColumnCount() Return total number of columns

abstract int getColumnIndex(String Returns the zero-based index for the given column name,
columnName) or -1 if the column doesn't exist.

abstract int getColumnIndexOrThrow(St Returns the zero-based index for the given column name,
ring columnName) or throws IllegalArgumentException if the column doesn't
exist.

abstract getColumnName(int Returns the column name at the given zero-based column
String columnIndex) index.
abstract getColumnNames() Returns a string array holding the names of all of the
String[] columns in the result set in the order in which they were
listed in the result.

abstract int getCount() Returns the numbers of rows in the cursor.

abstract getDouble(int columnIndex) Returns the value of the requested column as a double.
double

abstract getExtras() Returns a bundle of extra values.


Bundle

abstract getFloat(int columnIndex) Returns the value of the requested column as a float.
float
abstract getExtras() Returns a bundle of extra values.
Bundle

abstract getFloat(int columnIndex) Returns the value of the requested column as a float.
float

abstract int getInt(int columnIndex) Returns the value of the requested column as an int.

abstract getLong(int columnIndex) Returns the value of the requested column as a long.
long

abstract int getPosition() Returns the current position of the cursor in the row set.

abstract getShort(int columnIndex) Returns the value of the requested column as a short.
short

abstract getString(int columnIndex) Returns the value of the requested column as a String.
String
Public Methods

abstract int getType(int Returns data type of the given column's value.
columnIndex)

abstract isAfterLast() Returns whether the cursor is pointing to the position after the last
boolean row.

abstract isBeforeFirst() Returns whether the cursor is pointing to the position before the
boolean first row.

abstract isClosed() return true if the cursor is closed


boolean

abstract isFirst() Returns whether the cursor is pointing to the first row.
boolean

abstract isLast() Returns whether the cursor is pointing to the last row.
boolean
abstract isNull(int Returns true if the value in the indicated column is null.
boolean columnIndex)

abstract move(int offset) Move the cursor by a relative amount, forward or backward, from
boolean the current position.

abstract moveToFirst() Move the cursor to the first row.


boolean

abstract moveToLast() Move the cursor to the last row.


boolean

abstract moveToNext() Move the cursor to the next row.


boolean

abstract moveToPosition(int Move the cursor to an absolute position.


boolean position)

abstract moveToPrevious() Move the cursor to the previous row.


boolean
Content Values
● ContentValues is a maplike class that matches a value to a String key
● A content provider manages access to a central repository of data.
● A provider is part of an Android application, which often provides its own UI for working with the data.
● However, content providers are primarily intended to be used by other applications, which access the provider using a
provider client object.

. public final class ContentValues extends Object implements Parcelable


Create Database
- Create Table
- Provide data in row formate
- Abc, xyz,pqr
ContentProvider
● onCreate() This method is called when the provider is started.
● query() This method receives a request from a client. The result is returned as a Cursor object.
● insert()This method inserts a new record into the content provider.
● delete() This method deletes an existing record from the content provider.
● update() This method updates an existing record from the content provider.
● getType() This method returns the MIME type of the data at the given URI.
CRUD

CRUD is nothing but an abbreviation for the basic operations that we perform in any database. And
the operations are

● Create
● Read
● Update
● Delete
Create Table

CREATE TABLE employees (


id INTEGER NOT NULL CONSTRAINT employees_pk PRIMARY KEY AUTOINCREMENT,
name varchar(200) NOT NULL,
department varchar(200) NOT NULL,
joiningdate datetime NOT NULL,
salary double NOT NULL
);

Creating a new Record

INSERT INTO employees


(name, department, joiningdate, salary)
VALUES
('Belal Khan', 'Technical', '2017-09-30 10:00:00', '40000');
Updating Data

UPDATE employees
SET
name = 'Belal Haque',
department = 'Research and Development',
salary = '100000'
WHERE id = 1;

You might also like