Android database SQLite sqliteopenhelper

Source: Internet
Author: User

 

/*

Everyone knows that it will be very tiring to write a blog.

Hope = reprint please indicate the source: http://blog.csdn.net/ta893115871

Please do not pity your mouse, (* ^__ ^ *) Xi ......

*/

As we all know, databases are very important. Almost all the software on the market needs to use databases.

In Android, the database is SQLite, which is small and convenient, occupies less space and is easy to operate. It is very popular.

The database is similar to the query (query) addition (insert) modification (update) deletion (delete ).

This example

1. Press the menu button to create a new (insert) modification (update) Delete (delete) for the database query ).

2. Use listview widgets to trigger click events and scroll wheel events.

Let's take a look at the database.

First, create a helper class mydatabase to inheritSqliteopenhelpe must implement its oncreate () and
Onupdate () method;

When a database is created, the oncreate () method is called. Therefore, you can write the table to be added.

The method is as follows:

@ Overridepublic void oncreate (sqlitedatabase dB) {// todo auto-generated method stub/* Create Table */log. I (TAG, "oncreate ()"); string SQL = "CREATE TABLE" + table_name + "(" + field_id + "integer primary key autoincrement, "+" "+ field_text +" text) "mongodb.exe csql (SQL );}

The onupdate () method is called when the database is updated.

Therefore, you need to write the SQL statement for updating the table.

 

// Update the database @ overridepublic void onupgrade (sqlitedatabase dB, int oldversion, int newversion) {// todo auto-generated method stublog. I (TAG, "onupgrade ()"); // Delete the sqlstring SQL = "Drop table if exits" + table_namemongodb.exe csql (SQL); oncreate (db );}

2. Obtain the sqlitedatabasedb object,

Through DB = MyOpenhelper. Getreadabledatabase (); // obtain the read-only data storage object. If the database does not exist, create a database.

DB = MyOpenhelper. Getwritabledatabase (); // get a readable and writable data cry object.

If the database does not exist, create a database.

Database files are generated by yourself in/data/package name/database to check whether the database is created successfully in eclipse.

Select different methods based on your needs;

At this point, our database is successfully created.

(1) query:

Cursor cursor = dB. Query (Table,Columns,Selection,Selectionargs,Groupby,Having,Orderby );

A cursor object is returned.

Cursor. movetonext (); or

Cursor. movetoposition (); to control the cursor position;

 

Although there are many fields, it should be clear to those who know the database,

The first parameter is table_name.

The second parameter is the field to be queried.

The third parameter is the query condition.

This query condition is described here.

Query all data:

// ASC is ascending and desc is descending. The default value is ASC.

Msqlitedatabase. Query (Tablename,New
String [] {ID,Stuname,Stunumber,Stuscore
},Null,Null,Null,Null,ID+"
ASC ");

Query conditional data:

Cursor cursor = dB. Query (Tablename,NewString []
{ID,
Stuname,
Stunumber,Stuscore},"
Stuname
=? ",

NewString [] {"zhnagsan "},Null,Null,Null)

In this case, the fourth parameter is the third parameter? .

// Returns a cursor object, whichis positioned before the first entry

Cursorcur =
Msqlitedatabase. Query (Tablename,NewString [] {ID,Stuname,Stunumber,Stuscore
},Stunumber+ "=" + Number,Null,Null,Null,Null);

You can also leave the fourth parameter blank to directly query the data in the third parameter.

Both methods are supported.

After the cursor is generated.

If this cursor is not empty, move the cursor to the first data during the query.

Cursor. movetofirst ();

Because the cursor is directed to the previous row of data in the first row by default, if we do not point to the first row, an error is reported, and an exception occurs across the border.

(2) increase

// Add public long addmethod (string Str) {/* Add the new value to contentvalues */contentvalues CV = new contentvalues (); cv. put (field_text, STR); // key-Value Pair long row = dB. insert (table_name, null, CV); log. I (TAG, "addmethod ROW =" + row); Return row ;}

 

The first parameter is the name of the table to which you want to insert data.

The second parameter is not allowed to insert a completely empty record.

The second parameter is a key-Value Pair encapsulated in a contentvalues object.

(3) Modification

// Modify public void modmethod (int id, string Str) {contentvalues values = new contentvalues (); values. put (field_text, STR); string [] whereargs = {integer. tostring (ID)}; int rowsaffected = dB. update (table_name, values, field_id + "=? ", Whereargs); log. I (TAG," modmethod () rowsaffected = "+ rowsaffected );}

The parameters are clear.

The returned Number of affected rows is one or two.

(4) Delete

// Delete public void deletemethod (int id) {string [] whereargs = {INTEGER. tostring (ID)}; int rowsaffected = dB. Delete (table_name, field_id + "=? ", Whereargs); log. I (TAG," deletemethod () rowsaffected = "+ rowsaffected );}

 

The parameters are clear.

The returned Number of affected rows is one or two.

(5) shut down the database

// Close the Database @ overridepublic synchronized void close () {// todo auto-generated method stublog. I (TAG, "close ()"); dB. close (); super. close ();}

 For the source code of the example, see "2" android database SQLite sqliteopenhelper

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.