========================================================== ========================================================== ===
1. SQLite Introduction
Http://www.sqlite.org/
A very small database provides a relatively complete Relational Database
2. sqliteopenhelper usage
getReadableDatabase ()
getWritableDatabase ()
onCreate (SQLiteDatabase db)
onOpen (SQLiteDatabase db)
onUpgrade (SQLiteDatabase db, int oldVersion, int newVersion)
close ()
3. # sqlite3 test_mars_db
sqlite> .schema command: all start with dots. Shows which tables exist in the current database, and how the tables are created!
======================================================= ===================================
1. Operation steps of SQLite database:
(1) First create a class,
extends SQLiteOpenHelper
(2)
Write a constructor, a constructor with four parameters (Context context, String name,
CursorFactory factory, int version) {
super (context, name, factory, version);
}
Constructor with two parameters (Context context, String name) {
this (context, name, 1);
}
Three parameter constructor (Context context, String name, int version) {
this (context, name, null, version);
}
(3)
@override OnCreate method (SQLiteDatabase db) {
db.execSQL ("create table user (id int, name varchar (20))");
}
@override OnUpgrade (SQLiteDatabase db, int oldVersion, int newVersion) {
Can write some operations
}
2.// What is written in the listen method of the create database button
onClick (View v) in createDatabaseButtonListener {
// Explain, DatabaseHelper is the class that inherits SQLiteOpenHelper above
DatabaseHelper dbHelper = new DatabaseHelper (current class.this, "test_daming");
SQLiteDatabase db = dbHelper.getReadableDatabase ();
}
3.// What is written in the listen method of the update database button
onClick (View v) in updateDatabaseButtonListener {
DatabaseHelper dbHelper = new DatabaseHelper (current class.this, "test_daming", 2);
SQLiteDatabase db = dbHelper.getReadableDatabase ();
}
4, / / Insert the database table operation, written in the Button button listen method
onClick (View v) in insertButtonListener {
ContentValues values = new ContentValues ();
values.put ("id", 1);
values.put ("name", "daming");
DatabaseHelper dbHelper = new DatabaseHelper (current class.this, "test_daming");
SQLiteDatabase db = dbHelper.getWritableDatabase ();
db.insert ("user", null, values);
}
5, / / Update the database table operation, written in the Button button listen method
onClick (View v) in updateButtonListener {
DatabaseHelper dbHelper = new DatabaseHelper (current class.this, "database name");
SQLiteDatabase db = dbHelper.getWritableDatabase ();
ContentValues values = new ContentValues ();
values.put ("name", "Peking");
db.update ("user", values, "id =?", new String [] {"1"});
}
6, / / Find the database table operation, written in the Button button listen method
onClick (View v) in queryButtonListener {
DatabaseHelper dbHelper = new DatabaseHelper (current class.this, data name ");
SQLiteDatabase db = dbHelper.getWritableDatabse ();
Cursor cursor = db.query ("user", new String [] ("id", "name", "id =?", New
String [] {"1"}, "id", "id", "id"}));
while (cursor.moveToNext ()) {
String name = cursor.getString (cursor.getColumnIndex ("name"));
System.out.println ("query->" + name);
}
}
7, / / delete the database table operation, written in the Button button listen method
onClick (View v) in deleteButtonListener {
DatabaseHelper dbHelper = new DatabaseHelper (current class.this, data name ");
SQLiteDatabase db = dbHelper.getWritableDatabse ();
db.delete ("student", "id =?", new String [] {"1"});
System.out.println ("Delete the record with id = 1 in the table");
}