Android-SQLiteOpenHelper里增刪改查


為什么要寫一篇,Android-SQLiteOpenHelper里增刪改查,的文章呢;

  因為之前的方式是:MySQLiteOpenHelper(只負責 生成打開據庫/生成打開表/升級表),在其他端:完成此數據庫表的增刪改查邏輯處理,這樣代碼有些分散

現在 MySQLiteOpenHelper(負責 生成打開據庫 / 生成打開表 / 升級表 / 完成此數據庫表的增刪改查邏輯處理 / 還有其他表處理功能增加等等),這一個類全包了

 


 

 

MySQLiteOpenHelperStudent

注意事項:繼承SQLiteOpenHelper抽象類 重寫的創表方法,此SQLiteDatabase db 不能關閉

  1 package liudeli.datastorage.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 import android.util.Log;
  9 
 10 import java.util.List;
 11 
 12 import liudeli.datastorage.entity.Student;
 13 
 14 /**
 15  *  數據庫表管理
 16  *  MySQLiteOpenHelperStudent
 17  *  (負責 生成打開據庫 / 生成打開表 / 升級表 /
 18  *        完成此數據庫表的增刪改查邏輯處理
 19  *       / 還有其他表處理功能增加等等)
 20  */
 21 public class MySQLiteOpenHelperStudent extends SQLiteOpenHelper {
 22 
 23     private final static String TAG = MySQLiteOpenHelperStudent.class.getSimpleName();
 24 
 25     /**
 26      * 數據庫名稱
 27      */
 28     private final static String DB_NAME = "student_info_manager.db";
 29 
 30     /**
 31      * 表名
 32      */
 33     private static final String TABLE_NAME = "studentTable";
 34 
 35     /**
 36      * 定義單例模式,可以被多次地方多次調用
 37      */
 38     private static MySQLiteOpenHelperStudent mySQLiteOpenHelperStudent;
 39 
 40     public static MySQLiteOpenHelperStudent getInstance(Context context) {
 41         if (null == mySQLiteOpenHelperStudent) {
 42             synchronized (MySQLiteOpenHelperStudent.class) {
 43                 if (null == mySQLiteOpenHelperStudent) {
 44                     mySQLiteOpenHelperStudent = new MySQLiteOpenHelperStudent(context, DB_NAME, null, 1);
 45                 }
 46             }
 47         }
 48         return mySQLiteOpenHelperStudent;
 49     }
 50 
 51     private MySQLiteOpenHelperStudent(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) {
 52         super(context, name, factory, version);
 53     }
 54 
 55 
 56     /**
 57      * 創表方法,注意:⚠️ 傳遞進來的 SQLiteDatabase database 不能關閉
 58      * @param database
 59      */
 60     private void createTable(SQLiteDatabase database) {
 61         String createTableSQL =  "CREATE TABLE IF NOT EXISTS " + TABLE_NAME + " (ID INTEGER PRIMARY KEY, "
 62                 + "name VARCHAR, "
 63                 + "age INTEGER, "
 64                 + "hobby  VARCHAR);";
 65         try {
 66             database.execSQL(createTableSQL);
 67         } catch (Exception e) {
 68             e.printStackTrace();
 69             Log.e(TAG, TAG + "創表異常:" + e.toString());
 70         }
 71 
 72         /**
 73          * 注意:⚠️
 74          * 這里不能關閉database
 75          * 一旦關閉就會 Caused by: java.lang.IllegalStateException:
 76          *             attempt to re-open an already-closed object: SQLiteDatabase:
 77          * 為什么不能在這里關閉呢?
 78          * 答:因為這個database,是在 public void onCreate ->傳遞過來的
 79          */
 80         /*finally {
 81             if (null != database) {
 82                 database.close();
 83             }
 84         }*/
 85     }
 86 
 87     /**
 88      * 刪除表,會把表給刪除,慎用
 89      * drop 表
 90      */
 91     public void dropTable() {
 92         SQLiteDatabase database = getWritableDatabase();
 93         String dropSQL = "drop table if exists " + TABLE_NAME;
 94         try {
 95             database.execSQL(dropSQL);
 96         } catch (Exception e) {
 97             e.printStackTrace();
 98             Log.d(TAG, "drop異常:" + e.toString());
 99         } finally {
100             if (null != database) {
101                 database.close();
102             }
103         }
104     }
105 
106     /**
107      * 清除表的數據
108      */
109     public void cleanUpData() {
110 
111         // delete from TableName;  //清空數據
112 
113         SQLiteDatabase database = getWritableDatabase();
114         String cleanUpDataSQL = "delete from " + TABLE_NAME;
115         try {
116             database.execSQL(cleanUpDataSQL);
117         } catch (Exception e) {
118             e.printStackTrace();
119             Log.d(TAG, "清除表的數據異常:" + e.toString());
120         } finally {
121             if (null != database) {
122                 database.close();
123             }
124         }
125     }
126 
127     /**
128      * 插入多條數據
129      * @param students 傳遞Student集合
130      */
131     public void insertData(List<Student> students) {
132         SQLiteDatabase database = getWritableDatabase();
133         try {
134             for (Student student : students) {
135                 ContentValues contentValues = new ContentValues();
136                 contentValues.clear();
137                 contentValues.put("name", student.getName());
138                 contentValues.put("age", student.getName());
139                 contentValues.put("hobby", student.getHobby());
140                 database.insert(TABLE_NAME, "_id", contentValues);
141             }
142         } catch (Exception e) {
143             e.printStackTrace();
144             Log.e(TAG, "insert多條異常:" + e.toString());
145         } finally {
146             if (null != database) {
147                 database.close();
148             }
149         }
150     }
151 
152     /**
153      * 插入單條
154      * @param contentValues 傳遞ContentValues
155      */
156     public void insertData(ContentValues contentValues) {
157         SQLiteDatabase database = getWritableDatabase();
158         try {
159             database.insert(TABLE_NAME, "_id", contentValues);
160         } catch (Exception e) {
161             e.printStackTrace();
162             Log.e(TAG, "insert單條異常:" + e.toString());
163         } finally {
164             if (null != database) {
165                 database.close();
166             }
167         }
168     }
169 
170     /**
171      * 查詢需要的列名
172      */
173     private String[] columns = new String[]{"name", "age", "hobby"};
174 
175     /**
176      * 查詢第一條數據
177      * @return 返回Student實體
178      */
179     public Student selectData() {
180         SQLiteDatabase database = getReadableDatabase();
181         Cursor cursor = null;
182         Student student = null;
183         try {
184             cursor = database.query(TABLE_NAME, columns, null, null, null, null, null);
185             if (cursor.moveToFirst()) {
186                 String name = cursor.getString(cursor.getColumnIndex("name"));
187                 int age = cursor.getInt(cursor.getColumnIndex("age"));
188                 String hobby = cursor.getString(cursor.getColumnIndex("hobby"));
189 
190                 student = new Student(name, age, hobby);
191             }
192         } catch (Exception e) {
193             e.printStackTrace();
194             Log.e(TAG, "select異常:" + e.toString());
195         } finally {
196             if (null != database) {
197                 database.close();
198             }
199 
200             if (null != cursor) cursor.close();
201         }
202         return student;
203     }
204 
205     /**
206      * 判斷第一條數據是否存在
207      * @return 存在返回true,否則返回false
208      */
209     public boolean isMoveToFirst() {
210         boolean result;
211         SQLiteDatabase database = getReadableDatabase();
212         Cursor cursor = database.query(TABLE_NAME,
213                 columns,
214                 null,
215                 null,
216                 null,
217                 null,
218                 null);
219         result = cursor.moveToFirst();
220         database.close();
221         cursor.close();
222         return result;
223     }
224 
225     /**
226      *    .........  還可以增加很多操作表相關的行為
227      */
228 
229     /**
230      * 繼承SQLiteOpenHelper抽象類 重寫的創表方法,此SQLiteDatabase db 不能關閉
231      * @param db
232      */
233     @Override
234     public void onCreate(SQLiteDatabase db) {
235         createTable(db);
236     }
237 
238     /**
239      * 繼承SQLiteOpenHelper抽象類 重寫的升級方法
240      * @param db
241      * @param oldVersion
242      * @param newVersion
243      */
244     @Override
245     public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
246     }
247 }

 

繼承SQLiteOpenHelper抽象類 重寫的創表方法,此SQLiteDatabase db 不能關閉

/**
     * 創表方法,注意:⚠️ 傳遞進來的 SQLiteDatabase database 不能關閉
     * @param database
     */
    private void createTable(SQLiteDatabase database) {
        String createTableSQL =  "CREATE TABLE IF NOT EXISTS " + TABLE_NAME + " (ID INTEGER PRIMARY KEY, "
                + "name VARCHAR, "
                + "age INTEGER, "
                + "hobby  VARCHAR);";
        try {
            database.execSQL(createTableSQL);
        } catch (Exception e) {
            e.printStackTrace();
            Log.e(TAG, TAG + "創表異常:" + e.toString());
        }

        /**
         * 注意:⚠️
         * 這里不能關閉database
         * 一旦關閉就會 Caused by: java.lang.IllegalStateException:
         *             attempt to re-open an already-closed object: SQLiteDatabase:
         * 為什么不能在這里關閉呢?
         * 答:因為這個database,是在 public void onCreate ->傳遞過來的
         */
        /*finally {
            if (null != database) {
                database.close();
            }
        }*/
    }

一旦關閉,就會報以下錯誤:

Caused by: java.lang.IllegalStateException: attempt to re-open an already-closed object: SQLiteDatabase:

 

 

Student實體:

package liudeli.datastorage.entity;

public class Student {

    private String name;
    private int age;
    private String hobby;

    public Student() {
    }

    public Student(String name, int age, String hobby) {
        this.name = name;
        this.age = age;
        this.hobby = hobby;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public String getHobby() {
        return hobby;
    }

    public void setHobby(String hobby) {
        this.hobby = hobby;
    }

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", age=" + age +
                ", hobby='" + hobby + '\'' +
                '}';
    }
}

 

測試代碼:

package liudeli.datastorage;

import android.app.Activity;
import android.content.ContentValues;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;

import liudeli.datastorage.db.MySQLiteOpenHelperStudent;
import liudeli.datastorage.entity.Student;

public class MySQLiteActivity extends Activity {

    private MySQLiteOpenHelperStudent mySQLiteOpenHelperStudent;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_sqlite);

        mySQLiteOpenHelperStudent = MySQLiteOpenHelperStudent.getInstance(this);

        /**
         * 初始化數據
         */
        ContentValues contentValues = new ContentValues();
        contentValues.put("name", "劉劉" + System.currentTimeMillis());
        contentValues.put("age", 26);
        contentValues.put("hobby", "愛寫博客" + System.currentTimeMillis());

        mySQLiteOpenHelperStudent.insertData(contentValues);
    }

    /**
     * 查詢
     * @param view
     */
    public void query(View view) {
        Student student = mySQLiteOpenHelperStudent.selectData();
        if (student != null) {
            Log.d("sql", "student.toString:" + student.toString());
        }
    }

    /**
     * 是否有第一條數據
     * @param view
     */
    public void query1(View view) {
        Toast.makeText(this, mySQLiteOpenHelperStudent.isMoveToFirst() + "", Toast.LENGTH_LONG).show();
    }

    /**
     * drop表 刪除表
     * @param view
     */
    public void  dropTable(View view) {
        mySQLiteOpenHelperStudent.dropTable();
    }

    /**
     * 插入很多數據
     * @param view
     */
    public void installMany(View view) {
        List<Student> students = new ArrayList<>();
        students.add(new Student("李李", 11, "AAAAAAA"));
        students.add(new Student("李李22", 222, "BBBB"));
        students.add(new Student("李李33", 333, "CCC"));
        students.add(new Student("李李44", 444, "DDD"));
        mySQLiteOpenHelperStudent.insertData(students);
    }

    /**
     * 清除表數據
     * @param view
     */
    public void cleanUpData(View view) {
        mySQLiteOpenHelperStudent.cleanUpData();
    }
}

 

測試代碼的布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="query"
        android:text="查詢"
        android:layout_weight="0"
        />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="是否有第一條數據"
        android:onClick="query1"
        android:layout_weight="0"
        />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="drop表 刪除表"
        android:onClick="dropTable"
        android:layout_weight="0"
        />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="插入很多數據"
        android:onClick="installMany"
        android:layout_weight="0"
        />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="cleanUpData"
        android:text="清除表數據"
        />

</LinearLayout>

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM