SQLite Introduction
SQLite is a lightweight, embedded, and relational database. It is currently used in iPhone, Android, and other mobile phone systems. SQLite is portable, easy to use, small, efficient, and reliable. SQLite is embedded into applications that use it.ProgramThey share the same process space, rather than a separate process. From the external perspective, it is not like an RDBMS, but inside the process, it is complete, self-contained database engine.
In Android, when you need to operate the SQLite database, you need to get a sqliteopenhelper object, while sqliteopenhelper is an abstract class. You need to inherit this class and implement some methods in this class.
1. inherit from sqliteopenhelper and you will have the following two methods:
◆ Getreadabledatabase () Create or open a query database
◆ Getwritabledatabase () Create or open a writable Database
◆ They will return the sqlitedatabase object, and the user will perform subsequent operations through the obtained sqlitedatabase object
2. At the same time, the user can override the following callback functions and call back the following methods when operating the database:
◆ Oncreate (sqlitedatabase): This method is called when the database is created for the first time. Generally, we create a database table in this method.
◆ Onupgrade (sqlitedatabase, Int, INT): When the database needs to be modified, the android system will actively call this method. In general, we delete database tables and create new database tables in this method. Of course, whether other operations are required depends on the needs of the application.
◆ Onopen (sqlitedatabase): This is the callback function when the database is opened and is generally not used.
Note:
1. The following constructor must exist in the subclass of sqliteoepnhelper:
- Public databasehelper (context, string name, cursorfactory factory,
- Int version ){
- // The constructor in the parent class must be called through Super
- Super (context, name, factory, version );
- }
-
For convenience, you can also create other constructors with two or three parameters.
2. The public void oncreate (sqlitedatabase dB) function is executed when getreadabledatabase () is called or getwritabledatabase () is called to create a database for the first time. In fact, it is the first time that the sqlitedatabse object is obtained, to call this method.
- Public void oncreate (sqlitedatabase dB ){
- System. Out. println ("Create a database ");
- // Execsql function is used to execute SQL statements
- Db.exe csql ("create table user (ID int, name varchar (20 ))");
- }
-
When inserting a record into a database table, you must first include the data in a contentvalues and insert a key-value pair to the object. The key-value pair is the column name, the value is the value that you want to insert into this column. The value must be consistent with the data type in the database. Call the getwritabledatabase method of databasehelper to obtain the databasehelper object that can be written, and then insert records into it. Pay attention to the transmission of parameters that call the insert, update, or query method of the databasehelper object.
In addition, after the query method is executed, a cursor is returned. The cursor first points to the previous row of the first row in the record set. Therefore, you must first call cursor. next () move the cursor to the first row of the record set, and then obtain the data.
JavaCode
- Public class sqliteactivity extends activity {
-
- /** Called when the activity is first created .*/
-
- Private button createbutton;
-
- Private button insertbutton;
-
- Private button updatebutton;
-
- Private button updaterecordbutton;
- Private button querybutton;
-
- @ Override
-
- Public void oncreate (bundle savedinstancestate ){
-
- Super. oncreate (savedinstancestate );
-
- Setcontentview (R. layout. Main );
-
- Createbutton = (button) findviewbyid (R. Id. createdatabase );
- Updatebutton = (button) findviewbyid (R. Id. updatedatabase );
-
- Insertbutton = (button) findviewbyid (R. Id. insert );
-
- Updaterecordbutton = (button) findviewbyid (R. Id. Update );
-
- Querybutton = (button) findviewbyid (R. Id. query );
-
- Createbutton. setonclicklistener (New createlistener ());
- Updatebutton. setonclicklistener (New updatelistener ());
-
- Insertbutton. setonclicklistener (New insertlistener ());
-
- Updaterecordbutton. setonclicklistener (New updaterecordlistener ());
-
- Querybutton. setonclicklistener (New querylistener ());
-
- }
- Class createlistener implements onclicklistener {
-
- @ Override
-
- Public void onclick (view v ){
-
- // Create a databasehelper object
-
- Databasehelper dbhelper = new databasehelper (sqliteactivity. This, "test_mars_db ");
- // A database is created or opened only after the getreadabledatabase () method of the databasehelper object is called or the getwritabledatabase () method.
-
- Sqlitedatabase DB = dbhelper. getreadabledatabase ();
-
- }
-
- }
-
- Class updatelistener implements onclicklistener {
-
- @ Override
-
- Public void onclick (view v ){
-
- Databasehelper dbhelper = new databasehelper (sqliteactivity. This, "test_mars_db", 2 );
-
- Sqlitedatabase DB = dbhelper. getreadabledatabase ();
-
- }
-
- }
-
- Class insertlistener implements onclicklistener {
-
-
- @ Override
-
- Public void onclick (view v ){
-
- // Generate the contentvalues object
-
- Contentvalues values = new contentvalues ();
- // To insert a key-value pair to the object, the key-value pair is the column name and the value is the value to be inserted into the column. The value must be consistent with the data type in the database.
-
- Values. Put ("ID", 1 );
-
- Values. Put ("name", "zhangsan ");
-
- Databasehelper dbhelper = new databasehelper (sqliteactivity. This, "test_mars_db", 2 );
- Sqlitedatabase DB = dbhelper. getwritabledatabase ();
-
- // Call the insert method to insert data to the database.
-
- DB. insert ("user", null, values );
-
- }
-
- }
-
- // The update operation is equivalent to executing the update statement in the SQL statement.
- // Update table_name setXxcol=XxxWhereXxXxcol= XX...
-
- Class updaterecordlistener implements onclicklistener {
-
-
- @ Override
-
- Public void onclick (view arg0 ){
- // Todo auto-generated method stub
-
- // Get a writable sqlitedatabase object
-
- Databasehelper dbhelper = new databasehelper (sqliteactivity. This, "test_mars_db ");
-
- Sqlitedatabase DB = dbhelper. getwritabledatabase ();
- Contentvalues values = new contentvalues ();
-
- Values. Put ("name", "zhangsanfeng ");
-
- // The first parameter is the name of the table to be updated.
-
- // The second parameter is a contentvaleus object.
-
- // The third parameter is the WHERE clause.
- DB. Update ("user", values ,"ID=? ", New string [] {" 1 "});
-
- }
-
- }
-
- Class querylistener implements onclicklistener {
-
-
- @ Override
-
- Public void onclick (view v ){
- System. Out. println ("AAA ------------------");
-
- Log. D ("mydebug", "myfirstdebugmsg ");
-
-
- Databasehelper dbhelper = new databasehelper (sqliteactivity. This, "test_mars_db ");
-
- Sqlitedatabase DB = dbhelper. getreadabledatabase ();
- Cursor cursor = dB. Query ("user", new string [] {"ID", "name "},"ID=? ", New string [] {" 1 "}, null );
-
- While (cursor. movetonext ()){
-
- String name = cursor. getstring (cursor. getcolumnindex ("name "));
- System. Out. println ("query --->"+ Name );
-
- }
-
- }
-
- }
-
-
- }