Add, delete, modify, query, and delete android Databases
Main java
Package com. itheima. crud; import android. app. activity; import android. content. context; import android. database. sqlite. SQLiteDatabase; import android. OS. bundle; import android. view. view; import android. view. view. onClickListener; import com. itheima. crud. r; import com. itheima. crud. bean. infoBean; import com. itheima. crud. dao. infoDao; public class MainActivity extends Activity implements OnClickListener {private Context mContext; @ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); mContext = this; // create a help Class Object MySqliteOpenHelper mySqliteOpenHelper = new helper (mContext); // call the getReadableDatabase method to initialize the database creation SQLiteDatabase db = MySqliteOpenHelper. getReadableDatabase (); // find the findViewById (R. id. bt_add ). setOnClickListener (this); findViewById (R. id. bt_del ). setOnClickListener (this); findViewById (R. id. bt_update ). setOnClickListener (this); findViewById (R. id. bt_query ). setOnClickListener (this) ;}@ Override public void onClick (View v) {InfoDao infoDao = new InfoDao (mContext); // create a dao object for addition, deletion, modification, and query switch (v. getId () {case R. id. bt_add: InfoBean bean = new InfoBean (); bean. name = "Zhang San"; bean. phone = "110"; infoDao. add (bean); InfoBean bean1 = new InfoBean (); bean1.name = ""; bean1.phone = "120"; infoDao. add (bean1); break; case R. id. bt_del: infoDao. del ("Zhang San"); break; case R. id. bt_update: InfoBean bean2 = new InfoBean (); bean2.name = "zhangsan"; bean2.phone = "119"; infoDao. update (bean2); break; case R. id. bt_query: infoDao. query ("Zhang San"); infoDao. query ("Li Si"); break; default: break ;}}}
The class under the same package as the primary java inherits the class of the database.
Package com. itheima. crud; import android. content. context; import android. database. sqlite. SQLiteDatabase; import android. database. sqlite. SQLiteDatabase. cursorFactory; import android. database. sqlite. SQLiteOpenHelper; public class MySqliteOpenHelper extends SQLiteOpenHelper {public MySqliteOpenHelper (Context context) {// context: Context, name: database file name factory: used to create a cursor object, the default value is null // version: the database version number. The onUpgrade method will be called if it changes from 1. After 4.0, the super (context, "info. db ", null, 1);} // The oncreate method is called when the database is created for the first time. It is especially suitable for table structure initialization and SQL statements need to be executed; SQLiteDatabase db can be used to execute SQL statements @ Override public void onCreate (SQLiteDatabase db) {// execute the SQL statement db.exe cSQL ("create table info (_ id integer primary key autoincrement, name varchar (20), phone varchar (11) for creating a table through SQLiteDatabase )) ");} // The onUpgrade database will be executed only when the version number of the database changes. It is particularly suitable for modifying the table structure @ Override public void onUpgrade (SQLiteDatabase db, int oldVersion, int newVersion) {// add a phone field // db.exe cSQL ("alter table info add phone varchar (11 )");}}
New package -- New Class --- used to add, delete, modify, and query
Package com. itheima. crud. dao; import android. content. context; import android. database. cursor; import android. database. sqlite. SQLiteDatabase; import com. itheima. crud. mySqliteOpenHelper; import com. itheima. crud. bean. infoBean; public class InfoDao {private MySqliteOpenHelper mySqliteOpenHelper; public InfoDao (Context context) {// create a help class Object mySqliteOpenHelper = new MySqliteOpenHelper (context);} public void Add (InfoBean bean) {// sqliteDatabase object is required to execute the SQL statement // call the getReadableDatabase method to initialize the database creation SQLiteDatabase db = mySqliteOpenHelper. getWritableDatabase (); // SQL: SQL statement, bindArgs: The placeholder value in the SQL statement db.exe cSQL ("insert into info (name, phone) values (?,?); ", New Object [] {bean. name, bean. phone}); // closes the database object db. close ();} public void del (String name) {// sqliteDatabase object is required to execute the SQL statement // call the getReadableDatabase method to initialize the database creation SQLiteDatabase db = mySqliteOpenHelper. getReadableDatabase (); // SQL: SQL statement, bindArgs: The placeholder value in the SQL statement db.exe cSQL ("delete from info where name = ?; ", New Object [] {name}); // closes the database Object db. close ();} public void update (InfoBean bean) {// sqliteDatabase object is required to execute the SQL statement // call the getReadableDatabase method to initialize the database creation SQLiteDatabase db = mySqliteOpenHelper. getReadableDatabase (); // SQL: SQL statement, bindArgs: The placeholder value in the SQL statement db.exe cSQL ("update info set phone =? Where name = ?; ", New Object [] {bean. phone, bean. name}); // closes the database object db. close ();} public void query (String name) {// sqliteDatabase object is required to execute the SQL statement // call the getReadableDatabase method to initialize the database creation SQLiteDatabase db = mySqliteOpenHelper. getReadableDatabase (); // SQL: SQL statement, selectionArgs: the value of the placeholder query condition. A cursor object Cursor cursor = db is returned. rawQuery ("select _ id, name, phone from info where name = ?; ", New String [] {name}); // parse the data in Cursor if (cursor! = Null & cursor. getCount ()> 0) {// checks whether data exists in the cursor. // traverses the result set cyclically and obtains the content of each row while (cursor. moveToNext () {// condition. Can the cursor locate the next row? // get the data int id = cursor. getInt (0); String name_str = cursor. getString (1); String phone = cursor. getString (2); System. out. println ("_ id:" + id + "; name:" + name_str + "; phone:" + phone) ;}cursor. close (); // close the result set} // close the database object db. close ();}}
The name phone information package contains a class
Package com. itheima. crud. dao; import android. content. context; import android. database. cursor; import android. database. sqlite. SQLiteDatabase; import com. itheima. crud. mySqliteOpenHelper; import com. itheima. crud. bean. infoBean; public class InfoDao {private MySqliteOpenHelper mySqliteOpenHelper; public InfoDao (Context context) {// create a help class Object mySqliteOpenHelper = new MySqliteOpenHelper (context);} public void Add (InfoBean bean) {// sqliteDatabase object is required to execute the SQL statement // call the getReadableDatabase method to initialize the database creation SQLiteDatabase db = mySqliteOpenHelper. getWritableDatabase (); // SQL: SQL statement, bindArgs: The placeholder value in the SQL statement db.exe cSQL ("insert into info (name, phone) values (?,?); ", New Object [] {bean. name, bean. phone}); // closes the database object db. close ();} public void del (String name) {// sqliteDatabase object is required to execute the SQL statement // call the getReadableDatabase method to initialize the database creation SQLiteDatabase db = mySqliteOpenHelper. getReadableDatabase (); // SQL: SQL statement, bindArgs: The placeholder value in the SQL statement db.exe cSQL ("delete from info where name = ?; ", New Object [] {name}); // closes the database Object db. close ();} public void update (InfoBean bean) {// sqliteDatabase object is required to execute the SQL statement // call the getReadableDatabase method to initialize the database creation SQLiteDatabase db = mySqliteOpenHelper. getReadableDatabase (); // SQL: SQL statement, bindArgs: The placeholder value in the SQL statement db.exe cSQL ("update info set phone =? Where name = ?; ", New Object [] {bean. phone, bean. name}); // closes the database object db. close ();} public void query (String name) {// sqliteDatabase object is required to execute the SQL statement // call the getReadableDatabase method to initialize the database creation SQLiteDatabase db = mySqliteOpenHelper. getReadableDatabase (); // SQL: SQL statement, selectionArgs: the value of the placeholder query condition. A cursor object Cursor cursor = db is returned. rawQuery ("select _ id, name, phone from info where name = ?; ", New String [] {name}); // parse the data in Cursor if (cursor! = Null & cursor. getCount ()> 0) {// checks whether data exists in the cursor. // traverses the result set cyclically and obtains the content of each row while (cursor. moveToNext () {// condition. Can the cursor locate the next row? // get the data int id = cursor. getInt (0); String name_str = cursor. getString (1); String phone = cursor. getString (2); System. out. println ("_ id:" + id + "; name:" + name_str + "; phone:" + phone) ;}cursor. close (); // close the result set} // close the database object db. close ();}}
Xml design of four buttons
<?xml version="1.0"?>-<LinearLayout tools:context=".MainActivity" android:orientation="vertical" android:paddingTop="@dimen/activity_vertical_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingBottom="@dimen/activity_vertical_margin" android:layout_height="match_parent" android:layout_width="match_parent" xmlns:tools="http://schemas.android.com/tools" xmlns:android="http://schemas.android.com/apk/res/android"><Button android:id="@+id/bt_add" android:layout_height="wrap_content" android:layout_width="fill_parent" android:text="@string/add"/><Button android:id="@+id/bt_del" android:layout_height="wrap_content" android:layout_width="fill_parent" android:text="@string/del"/><Button android:id="@+id/bt_update" android:layout_height="wrap_content" android:layout_width="fill_parent" android:text="@string/update"/><Button android:id="@+id/bt_query" android:layout_height="wrap_content" android:layout_width="fill_parent" android:text="@string/query"/></LinearLayout>
Instructor notes
#2 add, delete, modify, and query a database in Android
1. Create a help class object, call the getReadableDatabase method, and return a SqliteDatebase object
2. Use the SqliteDatebase object to call execSql () for addition, deletion, and modification, and call the rawQuery Method for query.
* ***** Features: addition, deletion, modification, and no return value. You cannot determine whether the SQL statement is successfully executed. Manual SQL statement writing, easy to write errors
Private MySqliteOpenHelper mySqliteOpenHelper;
Public InfoDao (Context context ){
// Create a help Class Object
MySqliteOpenHelper = new MySqliteOpenHelper (context );
}
Public void add (InfoBean bean ){
// SqliteDatabase object is required for SQL statement execution
// Call the getReadableDatabase method to initialize database creation
SQLiteDatabase db = mySqliteOpenHelper. getReadableDatabase ();
// SQL: SQL statement, bindArgs: placeholder value in SQL statement
Db.exe cSQL ("insert into info (name, phone) values (?,?); ", New Object [] {bean. name, bean. phone });
// Closes the database object
Db. close ();
}
Public void del (String name ){
// SqliteDatabase object is required for SQL statement execution
// Call the getReadableDatabase method to initialize database creation
SQLiteDatabase db = mySqliteOpenHelper. getReadableDatabase ();
// SQL: SQL statement, bindArgs: placeholder value in SQL statement
Db.exe cSQL ("delete from info where name = ?; ", New Object [] {name });
// Closes the database object
Db. close ();
}
Public void update (InfoBean bean ){
// SqliteDatabase object is required for SQL statement execution
// Call the getReadableDatabase method to initialize database creation
SQLiteDatabase db = mySqliteOpenHelper. getReadableDatabase ();
// SQL: SQL statement, bindArgs: placeholder value in SQL statement
Db.exe cSQL ("update info set phone =? Where name = ?; ", New Object [] {bean. phone, bean. name });
// Closes the database object
Db. close ();
}
Public void query (String name ){
// SqliteDatabase object is required for SQL statement execution
// Call the getReadableDatabase method to initialize database creation
SQLiteDatabase db = mySqliteOpenHelper. getReadableDatabase ();
// SQL: SQL statement, selectionArgs: value of the placeholder of the query condition. A cursor object is returned.
Cursor cursor = db. rawQuery ("select _ id, name, phone from info where name =? ", New String [] {name });
// Parse data in Cursor
If (cursor! = Null & cursor. getCount ()> 0) {// checks whether data exists in the cursor.
// Cyclically traverse the result set to obtain the content of each row
While (cursor. moveToNext () {// condition, can the cursor locate to the next row?
// Obtain data
Int id = cursor. getInt (0 );
String name_str = cursor. getString (1 );
String phone = cursor. getString (2 );
System. out. println ("_ id:" + id + "; name:" + name_str + "; phone:" + phone );
}
Cursor. close (); // close the result set.
}
// Closes the database object
Db. close ();
}