实验4:Android数据存储和访问-书籍的增删改查

本次实验的任务是实现书籍的增删改查,其实我们可以对数据进行的操作也就无非四种,即CRUD。其中C代表添加(Create),R代表查询(Retrieve),U代表更新(Update),D代表删除(Delete)。每一种操作又各自对应了一种SQL命令,如果你比较熟悉SQL语言的话,一定会知道添加数据时使用insert,查询数据时使用select,更新数据时使用update,删除数据时使用delete。

一、实验效果图:

                                         

二、主要代码:

1、Java代码:

(1)MyDatabaseHelper.Java

Android为了让我们能够更加方便地管理数据库,专门提供了一个SQLiteOpenHelper帮助类,借助这个类就可以非常简单地对数据库进行创建和升级。

SQLiteOpenHelper中有两个抽象方法,分别是onCreate()和onUpgrade(),我们必须在自己的帮助类里面重写这两个方法,然后分别在这两个方法中去实现创建、升级数据库的逻辑。

  1. package com.example.databasetest;  
  2.   
  3. import android.content.Context;  
  4. import android.database.sqlite.SQLiteDatabase;  
  5. import android.database.sqlite.SQLiteDatabase.CursorFactory;  
  6. import android.database.sqlite.SQLiteOpenHelper;  
  7. import android.widget.Toast;  
  8.   
  9. public class MyDatabaseHelper extends SQLiteOpenHelper {  
  10.   
  11.     public static final String CREATE_BOOK = "create table book ("  
  12.             + "id integer primary key autoincrement, " + "author text, "  
  13.             + "price real, " + "pages integer, " + "name text)";  
  14.   
  15.     public static final String CREATE_CATEGORY = "create table Category ("  
  16.             + "id integer primary key autoincrement, " + "category_name text, "  
  17.             + "category_code integer)";  
  18.   
  19.     private Context mContext;  
  20.   
  21.     public MyDatabaseHelper(Context context, String name,  
  22.             CursorFactory factory, int version) {  
  23.         super(context, name, factory, version);  
  24.         mContext = context;  
  25.     }  
  26.   
  27.     @Override  
  28.     public void onCreate(SQLiteDatabase db) {  
  29.         db.execSQL(CREATE_BOOK);  
  30.         Toast.makeText(mContext, "Create succeeded", Toast.LENGTH_SHORT).show();  
  31.     }  
  32.   
  33.     @Override  
  34.     public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {  
  35.         db.execSQL("drop table if exists Book");  
  36.         db.execSQL("drop table if exists Category");  
  37.         onCreate(db);  
  38.     }  
  39.   
  40. }  
(2)MainActivity.java
  1. package com.example.databasetest;  
  2.   
  3. import android.app.Activity;  
  4. import android.content.ContentValues;  
  5. import android.database.Cursor;  
  6. import android.database.sqlite.SQLiteDatabase;  
  7. import android.os.Bundle;  
  8. import android.util.Log;  
  9. import android.view.View;  
  10. import android.view.View.OnClickListener;  
  11. import android.widget.Button;  
  12.   
  13. public class MainActivity extends Activity {  
  14.   
  15.     private MyDatabaseHelper dbHelper;  
  16.   
  17.     @Override  
  18.     protected void onCreate(Bundle savedInstanceState) {  
  19.         super.onCreate(savedInstanceState);  
  20.         setContentView(R.layout.activity_main);  
  21.         dbHelper = new MyDatabaseHelper(this"BookStore.db"null2);  
  22.   
  23.         /* 创建数据 */  
  24.         Button createDatabase = (Button) findViewById(R.id.create_database);  
  25.         createDatabase.setOnClickListener(new OnClickListener() {  
  26.             @Override  
  27.             public void onClick(View v) {  
  28.                 dbHelper.getWritableDatabase();  
  29.             }  
  30.         });  
  31.   
  32.         /* 添加数据 */  
  33.         Button addData = (Button) findViewById(R.id.add_data);  
  34.         addData.setOnClickListener(new OnClickListener() {  
  35.             @Override  
  36.             public void onClick(View v) {  
  37.                 SQLiteDatabase db = dbHelper.getWritableDatabase();  
  38.                 ContentValues values = new ContentValues();  
  39.                 // 开始组装第一条数据  
  40.                 values.put("name""The Da Vinci Code");  
  41.                 values.put("author""Dan Brown");  
  42.                 values.put("pages"454);  
  43.                 values.put("price"16.96);  
  44.                 db.insert("Book"null, values); // 插入第一条数据  
  45.                 values.clear();  
  46.                 // 开始组装第二条数据  
  47.                 values.put("name""The Lost Symbol");  
  48.                 values.put("author""Dan Brown");  
  49.                 values.put("pages"510);  
  50.                 values.put("price"19.95);  
  51.                 db.insert("Book"null, values); // 插入第二条数据  
  52.             }  
  53.         });  
  54.   
  55.         /* 更新数据 */  
  56.         Button updateData = (Button) findViewById(R.id.update_data);  
  57.         updateData.setOnClickListener(new OnClickListener() {  
  58.             @Override  
  59.             public void onClick(View v) {  
  60.                 SQLiteDatabase db = dbHelper.getWritableDatabase();  
  61.                 ContentValues values = new ContentValues();  
  62.                 values.put("price"10.99);  
  63.                 db.update("Book", values, "name = ?",  
  64.                         new String[] { "The Da Vinci Code" });  
  65.             }  
  66.         });  
  67.   
  68.         /* 删除数据 */  
  69.         Button deleteButton = (Button) findViewById(R.id.delete_data);  
  70.         deleteButton.setOnClickListener(new OnClickListener() {  
  71.             @Override  
  72.             public void onClick(View v) {  
  73.                 SQLiteDatabase db = dbHelper.getWritableDatabase();  
  74.                 db.delete("Book""pages > ?"new String[] { "500" });  
  75.             }  
  76.         });  
  77.   
  78.         /* 查询数据 */  
  79.         Button queryButton = (Button) findViewById(R.id.query_data);  
  80.         queryButton.setOnClickListener(new OnClickListener() {  
  81.             @Override  
  82.             public void onClick(View v) {  
  83.                 SQLiteDatabase db = dbHelper.getWritableDatabase();  
  84.                 // 查询Book表中所有的数据  
  85.                 Cursor cursor = db.query("Book"nullnullnullnullnull,  
  86.                         null);  
  87.                 if (cursor.moveToFirst()) {  
  88.                     do {  
  89.                         // 遍历Cursor对象,取出数据并打印  
  90.                         String name = cursor.getString(cursor  
  91.                                 .getColumnIndex("name"));  
  92.                         String author = cursor.getString(cursor  
  93.                                 .getColumnIndex("author"));  
  94.                         int pages = cursor.getInt(cursor  
  95.                                 .getColumnIndex("pages"));  
  96.                         double price = cursor.getDouble(cursor  
  97.                                 .getColumnIndex("price"));  
  98.                         Log.d("MainActivity""book name is " + name);  
  99.                         Log.d("MainActivity""book author is " + author);  
  100.                         Log.d("MainActivity""book pages is " + pages);  
  101.                         Log.d("MainActivity""book price is " + price);  
  102.                     } while (cursor.moveToNext());  
  103.                 }  
  104.                 cursor.close();  
  105.             }  
  106.         });  
  107.     }  
  108.   
  109. }
2、布局文件:

activity_main.xml:



  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     android:layout_width="match_parent"  
  3.     android:layout_height="match_parent"  
  4.     android:orientation="vertical" >  
  5.   
  6.     <Button  
  7.         android:id="@+id/create_database"  
  8.         android:layout_width="match_parent"  
  9.         android:layout_height="wrap_content"  
  10.         android:text="Create database" />  
  11.   
  12.     <Button  
  13.         android:id="@+id/add_data"  
  14.         android:layout_width="match_parent"  
  15.         android:layout_height="wrap_content"  
  16.         android:text="Add data" />  
  17.   
  18.     <Button  
  19.         android:id="@+id/update_data"  
  20.         android:layout_width="match_parent"  
  21.         android:layout_height="wrap_content"  
  22.         android:text="Update data" />  
  23.   
  24.     <Button  
  25.         android:id="@+id/delete_data"  
  26.         android:layout_width="match_parent"  
  27.         android:layout_height="wrap_content"  
  28.         android:text="Delete data" />  
  29.   
  30.     <Button  
  31.         android:id="@+id/query_data"  
  32.         android:layout_width="match_parent"  
  33.         android:layout_height="wrap_content"  
  34.         android:text="Query data" />  
  35.   
  36. </LinearLayout> 
三、体会总结 :

1、创建数据、添加数据、更新数据、删除数据都较为简单,在操作方法上有一定的相似性,而查询数据也是在CRUD中最复杂的一种操作。

2、使用SQL来完成CRUD操作:

(1)添加数据的方法如下:
db.execSQL("insert into Book (name, author, pages, price) values(?, ?, ?, ?)",
new String[] { "The Da Vinci Code", "Dan Brown", "454", "16.96" });
db.execSQL("insert into Book (name, author, pages, price) values(?, ?, ?, ?)",
new String[] { "The Lost Symbol", "Dan Brown", "510", "19.95" });

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值