SQLite relational database

Source: Internet
Author: User
Tags sqlite database stub

 Use of Sqliteopenhelper:

First, declare a Databasehelper class, which inherits from the Sqliteopenhelper class, first with the constructor, the declaration Databasehelper class as follows:

Databasehelper as an assistant class that accesses SQLite, offering two features
First, Getreadabledatabase (), getwriteabledatabase () can obtain a Sqlitedatabase object to manipulate the database through the object
Second, OnCreate () and Onupgrade () Two callback functions are provided, allowing us to do our own work while creating and upgrading the database.
public class Databasehelper extends sqliteopenhelper{
In Sqliteopenhelper subclasses, you must have this constructor
Context is an Activity object, the name of a table, the version of the versions database
private static final int VERSION = 1;
Public Databasehelper (Context context, String name, cursorfactory factory,int version) {
Super (context, name, Factory, version);
TODO auto-generated Constructor stub
}
Public Databasehelper (Context context,string name) {
Call a constructor with three arguments, that is, the next custom constructor
This (context,name,version);
}
Public Databasehelper (Context context,string name,int version) {
The constructor of the system is called, except that the value passed to the factory is empty
This (context,name,null,version);
}
This function is executed when the database is first created, and is actually the first time that the Sqlitedatabase object is obtained before the method is called.
@Override
public void OnCreate (Sqlitedatabase db) {
TODO auto-generated Method Stub
The Execsql function is used to execute SQL statements
Db.execsql ("CREATE table user (ID int,name varchar (20))");
LOG.D ("Mydebug", "My first Debug");
}

@Override
public void Onupgrade (sqlitedatabase db, int oldversion, int newversion) {
TODO auto-generated Method Stub
}

}

The following defines the listener events for adding and deleting buttons to the SQLite database:

Class Sqlbuttonlistener implements onclicklistener{

@Override
public void OnClick (View v) {
TODO auto-generated Method Stub
if (v.getid () = = R.id.createdatabase) {
Create the database, first call executes the OnCreate method
Databasehelper dbhelper = new Databasehelper (otheractivity.this, "test_zhanglei_db");
Sqlitedatabase db = Dbhelper.getreadabledatabase ();
}
else if (v.getid () = = R.id.updatadatabase) {
Update the version number of the current database
Databasehelper dbhelper = new Databasehelper (otheractivity.this, "test_zhanglei_db", 2);
}
else if (v.getid () = = R.id.insertdatabase) {//Insert button
Generating Contentvalue objects
Contentvalues values = new Contentvalues ();
Inserts a key-value pair into the object where the key is the column name, and the value is the value of the column that you want to insert, and the value must match the type of the column in the database
Values.put ("id", 1);
Values.put ("name", "Zhangsan");
Databasehelper dbhelper = new Databasehelper (otheractivity.this, "test_zhanglei_db", 2);
Sqlitedatabase db = Dbhelper.getwritabledatabase ();
You can insert data into the database by calling the Insert method
Db.insert ("user", null, values);
}
else if (v.getid () = = r.id.updatatable) {//Update button
UPDATE table_name set XXCOL=XXX WHERE xxcol=xx
Databasehelper dbhelper = new Databasehelper (otheractivity.this, "test_zhanglei_db", 2);
Sqlitedatabase db = Dbhelper.getwritabledatabase ();
Contentvalues values = new Contentvalues ();
Values.put ("name", "Zhangsanfeng");
The first parameter is the updated table name, the second parameter is a Contentvalues object, the third parameter is the WHERE clause, the? is a placeholder, and there are several arguments in the new string that follows
Db.update ("User", Values, "id=", New string[]{("1")});
}
else if (V.getid () ==r.id.querydatabase) {//Query button
Databasehelper dbhelper = new Databasehelper (otheractivity.this, "test_zhanglei_db", 2);
Sqlitedatabase db = Dbhelper.getreadabledatabase ();

Table name, query column name, WHERE clause, selection parameter, grouping (GROUPBY), grouping restriction (having), result sorting (order by)
cursor cursor = db.query ("User", new string[]{"id", "name"}, "id=", New string[]{"1"}, NULL, NULL, NULL);
while (Cursor.movetonext ()) {
The parameter in getString (1) is the column name in the table
String name = cursor.getstring (Cursor.getcolumnindex ("name"));
Textview.settext (Textview.gettext () +name);
}
}
}
}

SQLite relational database

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.