How to send a value to a database by Android

Source: Internet
Author: User

relational database SQLite3, it is a support SQL lightweight embedded database, embedded in the operation of a very broad, WM is also used SQLite3

Things that are too, in principle, will not be mentioned in this article, but if you want to be able to quickly learn how to operate SQLite3, then this is the article you are looking for!

First, let's look at the API, all the database-related interfaces, Classes are in. Database and Android.database.sqlite two packages, although there are only two packages, but if you are poor English or too lazy to be confused for a while, in fact, we really use a few!

1, Sqliteopenhelper (android.database.sqlite.SQLiteOpenHelper)

This is an abstract class, and we all know about abstract classes that if you want to use it, you must inherit it!

There are few methods of this class, there is a construction method

Sqliteopenhelper (Android.content.Context Context, java.lang.String name, Android.database.sqlite.SQLiteDatabase.CursorFactory Factory, int version);

Parameter does not do too much explanation, Cursorfactory General direct NULL can be

public void OnCreate (Sqlitedatabase db)

This method is called to create the database, so you should put the table creation action in this method, and later we will say in detail how to create the table

public void Onupgrade (sqlitedatabase db, int oldversion, int newversion)

From the method name we can know that this method is to perform the update, yes, when version change is the system will call this method, so in this method should be executed to delete the existing table, and then manually call OnCreate operation

Sqlitedatabase Getreadabledatabase ()

Sqlitedatabase objects that can be read

Sqlitedatabase Getwritabledatabase ()

Gets the writable Sqlitedatabase object

2, Sqlitedatabase (android.database.sqlite.SQLiteDatabase)

The operation of the database (increase, delete, check, change) is in this class

Execsql (SQL)

Execute SQL statement, using this method +sql statement can be very convenient to perform the increase, delete, check, change

In addition to this, Android also offers ways to increase, delete, check, change

Long INSERT (table_name, NULL, contentvalues) add record

int delete (table_name, where, Wherevalue) Delete record

int update (TABLE_NAME, contentvalues, where, wherevalue) update record

Cursor query (table_name, NULL, NULL, NULL, NULL, NULL, NULL) query record

In addition, there are many methods, such as: BeginTransaction () Start transaction, Endtransaction () End transaction ... Interested can see their own API, here is not more than repeat

3. Cursor (Android.database.Cursor)

Cursor (interface), this is very familiar with it, the cursor in a lot of methods, commonly used are:

Boolean movetoposition (position) moves the pointer to a record

Getcolumnindex (Contacts.People.NAME) Get ID by column name

int GetCount () Gets the total number of records

Boolean requery () re-query

Whether the Boolean isafterlast () pointer is at the end

Boolean Isbeforefirst () is the start position

Whether Boolean IsFirst () is the first record

Boolean islast () is the last record

Boolean Movetofirst (), Boolean Movetolast (), Boolean movetonext () with movetoposition (position)

4, Simplecursoradapter (Android.widget.SimpleCursorAdapter)

Perhaps you will be surprised, before I also said about the database operations are under the databases and Database.sqlite package, why put a adapter here, if you have used Android SQLite3, you will know

, because our operations on the database are often linked to the list.

Often a friend will make a mistake here, but it's also very simple.

Simplecursoradapter adapter = new Simplecursoradapter (

This

R.layout.list,

MyCursor,

New string[],

New int[]);

My.setadapter (adapter);

A total of 5 parameters, specifically as follows:

Parameter 1:content

Parameter 2: Layout

Parameter 3:cursor Cursor Object

Parameter 4: Displayed field, incoming string[]

Parameter 5: Displays the component used by the field, passed in int[], the ID of the TextView component in the array

Here, the operation of the database is over, but so far I have only done translation work, some students may still not master, rest assured, below we together along the normal development of ideas to clear up the clue!

The front is just to help the friends do not do the popularization, the following is what you really need!

First, write a class inheritance Sqliteopenhelpe

public class Databasehelper extends Sqliteopenhelper

Construction Method:

Databasehelper (Context context) {

Super (context, database_name, NULL, database_version);

}

How to write a table in the OnCreate method

public void OnCreate (Sqlitedatabase db) {

String sql = "CREATE TABLE tb_test (_id INTEGER DEFAULT ' 1 ' not NULL PRIMARY KEY autoincrement,class_jb TEXT not Null,clas S_YSBJ text not null,title text not NULL,CONTENT_YSBJ text not NULL) ";

Db.execsql (SQL);//Require exception capture

}

Delete the existing table in the Onupgrade method and call Onctreate to create the table manually

public void Onupgrade (sqlitedatabase db, int oldversion, int newversion) {

String sql = "drop table" +tbname;

Db.execsql (SQL);

OnCreate (DB);

}

The method of adding, deleting, checking, and modifying the table, which is used by the method provided by Sqliteopenhelper, can also be implemented with SQL statements, all the same

About getting readable/writable sqlitedatabase, I don't say it should be thought that only the search will use the readable sqlitedatabase

/**

* Add data

*/

Public long Insert (string tname, int Tage, string ttel) {

Sqlitedatabase db= getwritabledatabase ();//Get writable Sqlitedatabase Object

Contentvalues similar to map, the key value pair is deposited

Contentvalues contentvalues = new Contentvalues ();

Contentvalues.put ("Tname", tname);

Contentvalues.put ("Tage", Tage);

Contentvalues.put ("Ttel", Ttel);

Return Db.insert (Tbname, NULL, contentvalues);

}

/**

* Delete Records

* @param _id

*/

public void Delete (String _id) {

Sqlitedatabase db= getwritabledatabase ();

Db.delete (Tbname,

"_id=?",

New string[]);

}

/**

* Update the record, like the insert.

*/

public void Update (string _id,string tname, int tage, string ttel) {

Sqlitedatabase db= getwritabledatabase ();

Contentvalues contentvalues = new Contentvalues ();

Contentvalues.put ("Tname", tname);

Contentvalues.put ("Tage", Tage);

Contentvalues.put ("Ttel", Ttel);

Db.update (Tbname, Contentvalues,

"_id=?",

New string[]);

}

/**

* Query all data

* @return Cursor

*/

Public Cursor Select () () {

Sqlitedatabase db = Getreadabledatabase ();

Return Db.query (

Tbname,

New string[],

Null

NULL, NULL, NULL, "_id desc");

}

About the parameters of the Db.query method, there are a lot of, in order to prevent everyone from messing up, I simply say

Parameter 1: Table name

Parameter 2: Returns the column information contained in the data, with the column names in the string array

Parameter 3: Equivalent to the where,sql in the SQL where the content is written to this, for example: Tage>

Parameter 4: What if you wrote in Parameter 3? (Do you know why I wrote Tage&gt?), the one here is instead of? The value of the next example: New string[]

Parameter 5: Group, do not explain, do not want to group to pass NULL

Parameter 6:having, can't remember to look at the SQL

Parameter 7:orderby Ordering

Here you have completed the most first step! We'll take a look at all the classes:

Sqliteopenhelper we inherited it.

Sqlitedatabase additions and deletions can not be separated from it, even if you directly use SQL statements, but also to use the Execsql (SQL)

Second, here is nothing but a call to the Databasehelper class definition method, there is no point to say, but I still have a few words on the query

Android queries come out of the results of a cursor form return

cursor = Sqlitehelper.select () ();//Is it very simple?

The cursor that is queried is usually displayed in the ListView, which will use the Simplecursoradapter mentioned earlier

Simplecursoradapter adapter = new Simplecursoradapter (

This

R.layout.list_row,

Cursor

New string[],

New int[]

);

How to send a value to a database by Android

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.