• android SQLite数据库的基本操作


    SQLite是Android使用的轻量级的数据库,开发Android应用是对数据库的操作自然是必不可少。

    Android提供了一个SQLiteOpenHelper类来可以很方便的操作数据库,

    继承和扩展SQLiteOpenHelper类主要做的工作就是重写以下两个方法。 
           onCreate: 当数据库被首次创建时执行该方法,一般将创建表等初始化操作在该方法中执行。 
           onUpgrade:当打开数据库时传入的版本号与当前的版本号不同时会调用该方法。 


    下面是我写的一个SQLite基本操作的demo。

    主要包含两个java类——

    DBUtil类,继承自SQLiteOpenHelper,用以实现各种操作功能:

     1 package barry.android.db;
     2 
     3 import android.content.ContentValues;
     4 import android.content.Context;
     5 import android.database.Cursor;
     6 import android.database.sqlite.SQLiteDatabase;
     7 import android.database.sqlite.SQLiteOpenHelper;
     8 
     9 public class DBUtil extends SQLiteOpenHelper {
    10 
    11     private final static String DATABASE_NAME = "db2004";
    12     private final static int DATABASE_VERSION = 1;
    13     private static final String TABLE_NAME ="students";
    14     private static final String FILED_1 = "name";
    15     private static final String FILED_2 = "password";
    16 
    17     public DBUtil(Context context){
    18         super(context, DATABASE_NAME,null,DATABASE_VERSION);
    19         System.out.println("new DBUtil");
    20     }
    21 
    22     @Override
    23     public void onCreate(SQLiteDatabase db) {
    24         String sql = "CREATE TABLE "+TABLE_NAME+" ( "+FILED_1 +" TEXT, "+ FILED_2 +" TEXT );";
    25         db.execSQL(sql);
    26         System.out.println("oncreate创建表");
    27     }
    28 
    29     @Override
    30     public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    31         db.execSQL("DROP TABLE IF EXISTS "+TABLE_NAME);
    32         System.out.println("onUpgrade删除表");
    33         this.onCreate(db);
    34     }
    35     
    36     /**
    37      * 查询表中所有的数据
    38      * @return
    39      */
    40     public Cursor select(){
    41         return this.getReadableDatabase()
    42             .query(TABLE_NAME, null, null, null, null, null, null);        
    43     }
    44 
    45     /**
    46      * 插入一条数据到表中
    47      * @param name 字段一的值
    48      * @param password 字段二的值
    49      */
    50     public void insert(String name ,String password){
    51         ContentValues cv = new ContentValues();
    52         cv.put(FILED_1, name);
    53         cv.put(FILED_2, password);        
    54         this.getWritableDatabase().insert(TABLE_NAME, null, cv);
    55         this.getWritableDatabase().close();//关闭数据库对象 
    56     }    
    57     
    58     /**
    59      * 删除表中的若干条数据
    60      * @param name 一个包含所有要删除数据的"name"字段的数组
    61      */
    62     public void delete(String[] name){
    63         String where = FILED_1+" = ?";
    64         String[] whereValues = name; 
    65         this.getWritableDatabase().delete(TABLE_NAME, where, whereValues);
    66         this.getWritableDatabase().close();
    67     }
    68     
    69     /**
    70      * 更新表中的数据(修改字段二"password")
    71      * @param name 要更新的数据"name"字段值
    72      * @param newPassword 新的"password"字段
    73      */
    74     public void update(String name,String newPassword){        
    75         ContentValues cv = new ContentValues();
    76         cv.put(FILED_2, newPassword);        
    77         String where =FILED_1+" = ?";
    78         String[] whereValues= {name};        
    79         this.getWritableDatabase().update(TABLE_NAME, cv, where, whereValues);
    80         this.getWritableDatabase().close();
    81     }    
    82     
    83     /**
    84      * 清空表中的数据
    85      */
    86     public void clean (){
    87         this.getWritableDatabase().execSQL("DROP TABLE IF EXISTS "+TABLE_NAME);
    88         System.out.println("clean删除表");
    89         this.onCreate(this.getWritableDatabase());
    90         this.getWritableDatabase().close();
    91     }
    92 }

    以及调用DBUtil的Activity:

     1 package barry.android.db;
     2 
     3 import android.app.Activity;
     4 import android.database.Cursor;
     5 import android.os.Bundle;
     6 
     7 public class Demo04_helperActivity extends Activity {
     8     /** Called when the activity is first created. */
     9     @Override
    10     public void onCreate(Bundle savedInstanceState) {
    11         super.onCreate(savedInstanceState);
    12         setContentView(R.layout.main);
    13         
    14         
    15         DBUtil dbUtil = new DBUtil(this);
    16         dbUtil.insert("周杰伦", "jaychou");
    17         dbUtil.insert("韩寒", "twocolds");
    18         dbUtil.insert("郭德纲", "yunhejiuxiao");        
    19         
    20         System.out.println("***********************************全部数据息");
    21         printData(dbUtil);        
    22         
    23         dbUtil.delete(new String[]{"周杰伦"});
    24         
    25         System.out.println("***********************************删除'周杰伦'之后数据");
    26         printData(dbUtil); 
    27         
    28         dbUtil.update("郭德纲", "longtengsihai");;
    29         System.out.println("***********************************修改‘郭德纲’的密码为'longtengsihai'");
    30         printData(dbUtil); 
    31         
    32         dbUtil.clean();
    33         
    34     }
    35 
    36     private void printData(DBUtil dbUtil) {
    37         Cursor cursor = dbUtil.select();        
    38         if(cursor.moveToFirst()){
    39             System.out.println("当前表中的数据条数:"+cursor.getCount());
    40             do{
    41                 System.out.println(cursor.getString(0)+cursor.getString(1));                
    42             }while(cursor.moveToNext());
    43         }
    44         cursor.close();
    45     }
    46 }

    该程序所执行的操作为:

    1.在创建一个名为"db2004"的数据库,(即DBUtil的“DATABASE_NAME”字段)。

    2.当数据库被首次创建时执行DBUtil的onCreate方法,创建一张名为students的表,包含两个字段(name,password)。(即DBUtil的”TABLE_NAME、FILED_1、FILED_2”字段)。

    3.往数据库中插入三条数据“周杰伦、韩寒、郭德纲”。然后.查询出表中所有数据并打印。

    4.删除数据“周杰伦”。然后.查询出表中所有数据并打印。

    5.将数据“郭德纲”的password修改为"longtengsihai"。然后.查询出表中所有数据并打印。

    6.清除表中的所有数据,程序结束。

    执行的结果为:

    02-07 11:22:47.361: I/System.out(962): new DBUtil
    02-07 11:22:47.490: I/System.out(962): ***********************************全部数据息
    02-07 11:22:47.490: I/System.out(962): 当前表中的数据条数:3
    02-07 11:22:47.500: I/System.out(962): 周杰伦jaychou
    02-07 11:22:47.500: I/System.out(962): 韩寒twocolds
    02-07 11:22:47.500: I/System.out(962): 郭德纲yunhejiuxiao
    02-07 11:22:47.511: I/System.out(962): ***********************************删除'周杰伦'之后数据
    02-07 11:22:47.540: I/System.out(962): 当前表中的数据条数:2
    02-07 11:22:47.540: I/System.out(962): 韩寒twocolds
    02-07 11:22:47.550: I/System.out(962): 郭德纲yunhejiuxiao
    02-07 11:22:47.560: I/System.out(962): ***********************************修改‘郭德纲’的密码为'longtengsihai'
    02-07 11:22:47.590: I/System.out(962): 当前表中的数据条数:2
    02-07 11:22:47.590: I/System.out(962): 韩寒twocolds
    02-07 11:22:47.590: I/System.out(962): 郭德纲longtengsihai
    02-07 11:22:47.601: I/System.out(962): clean删除表
    02-07 11:22:47.610: I/System.out(962): oncreate创建表

    结果正确。

  • 相关阅读:
    MYSQL分页优化查询
    13_java之final|static|包|匿名对象|代码块|内部类
    12_java之构造方法|this|super
    11_java之接口和多态
    10_java之继承和抽象类
    09_java之面向对象概述
    08_java超市管理系统
    07_java之练习题
    06_java之类概述
    05_java之方法
  • 原文地址:https://www.cnblogs.com/ycxyyzw/p/4469375.html
Copyright © 2020-2023  润新知