Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to insert the data into the database correctly

2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)05/31 Report--

This article introduces you how to correctly insert the data into the database, the content is very detailed, interested friends can refer to, hope to be helpful to you.

Put the data into the database

Insert the data into the database by passing the ContentValues object into the instert () method:

/ / Gets the data repository in write mode

SQLiteDatabase db = mDbHelper.getWritableDatabase ()

/ / Create a new map of values, where column names are the keys

ContentValues values = new ContentValues ()

Values.put (FeedReaderContract.FeedEntry.COLUMN_NAME_ENTRY_ID, id)

Values.put (FeedReaderContract.FeedEntry.COLUMN_NAME_TITLE, title)

Values.put (FeedReaderContract.FeedEntry.COLUMN_NAME_CONTENT, content)

/ / Insert the new row, returning the primary key value of the new row

Long newRowId

NewRowId = db.insert (

FeedReaderContract.FeedEntry.TABLE_NAME

FeedReaderContract.FeedEntry.COLUMN_NAME_NULLABLE

Values)

The first parameter of the insert () method is the table name. The second parameter provides a column name in the frame, and the frame inserts a null value into the table when the value of ContentValues is empty (if this parameter is "null", then the frame does not insert a row into the table when there is no value.

Read data from the database

To read data from the database, use the query () method, which you need to pass in the selection criteria and the column you want to get the data from. The query results are returned in the Cursor object.

SQLiteDatabase db = mDbHelper.getReadableDatabase ()

/ / Define a projection that specifies which columns from the database

/ / you will actually use after this query.

String [] projection = {

FeedReaderContract.FeedEntry._ID

FeedReaderContract.FeedEntry.COLUMN_NAME_TITLE

FeedReaderContract.FeedEntry.COLUMN_NAME_UPDATED

...

}

/ / How you want the results sorted in the resulting Cursor

String sortOrder =

FeedReaderContract.FeedEntry.COLUMN_NAME_UPDATED + "DESC"

Cursor c = db.query (

FeedReaderContract.FeedEntry.TABLE_NAME, / / The table to query

Projection, / / The columns to return

Selection, / / The columns for the WHERE clause

SelectionArgs, / / The values for the WHERE clause

Null, / / don't group the rows

Null, / / don't filter by row groups

SortOrder / / The sort order

);

Use the move method of the Cursor object to view a row of data in the cursor, which must be called before you start reading the data. Typically, you should start by calling the moveToFirst () method, which puts the location where the data is read to the first entity in the result set. For each row, you can read the value of the column by calling the corresponding get method of the Cursor object, if the getString () or getLong () method. For each get method, you must pass it the index position of the column you want, and you can get the column index by calling the getColumnIndex () or getColumnIndexOrThrow () method. For example:

Cursor.moveToFirst ()

Long itemId = cursor.getLong (

Cursor.getColumnIndexOrThrow (FeedReaderContract.FeedEntry._ID)

);

Delete data from the database

To delete row data from a table, you need to provide selection criteria that identify the row. Data API provides a mechanism for creating selection criteria, which prevents SQL injection. The mechanism divides the selection conditions into selection conditions and selection parameters. The conditional clause defines the columns to view and also allows you to filter using combined columns. The parameter is the value that the user filters the data that is bound to the condition. Because this does not result in processing like SQL statements, it avoids SQL injection.

/ / Define 'where' part of query.

String selection = FeedReaderContract.FeedEntry.COLUMN_NAME_ENTRY_ID + "LIKE?"

/ / Specify arguments in placeholder order.

String [] selelectionArgs = {String.valueOf (rowId)}

/ / Issue SQL statement.

Db.delete (table_name, selection, selectionArgs)

Update the database

Use the update () method when you need to edit database values.

This method combines the syntax of the content values in the insert () method with the where syntax in the delete () method when updating the data.

SQLiteDatabase db = mDbHelper.getReadableDatabase ()

/ / New value for one column

ContentValues values = new ContentValues ()

Values.put (FeedReaderContract.FeedEntry.COLUMN_NAME_TITLE, title)

/ / Which row to update, based on the ID

String selection = FeedReaderContract.FeedEntry.COLUMN_NAME_ENTRY_ID + "LIKE?"

String [] selelectionArgs = {String.valueOf (rowId)}

Int count = db.update (

FeedReaderDbHelper.FeedEntry.TABLE_NAME

Values

Selection

SelectionArgs)

On how to correctly insert the data into the database to share here, I hope the above content can be of some help to you, can learn more knowledge. If you think the article is good, you can share it for more people to see.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 206

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report