####二、數據庫的建立簡介
Android 爲了讓咱們很是方便的管理一個數據庫,專門提供一個SQLiteOpenHelper幫助類,藉助這個類咱們能夠對數據庫進行建立,升級。java
1)SQLiteOpenHelper 是一個抽象類,這意味着咱們使用它的話就須要建立一個本身的幫助類來繼承它。
2) SQLiteOpenHelper 有兩個抽象方法 onCreat() onUpgrade() ,咱們必須在本身的幫助類裏面來重寫它,實現建立和升級的代碼邏輯。
3)SQLiteOpenHelper 還有兩個很是重要的實例方法,getReadableDataase() WritableDatabase() 這兩個方法都建立或打開一個數據庫(已存在),並返回一個能夠對數據庫進行讀寫的對象。
4)當磁盤滿的時候, getReadableDataase 將以只讀的形式返回數據庫對象,WritableDatabase 會出現錯誤mysql
5)SQLiteOpenHelper 有兩個構造方法能夠重寫,通常使用參數少一點的那個便可。四個參數:
第一個參數 Context : 必須有它才能對數據庫進行操做
第二個參數:數據庫名,建立數據庫時使用的名字
第三個參數:容許咱們在查詢時返回的Cursor ,通常傳入null
第四個參數:當前數據庫的版本號,可用於數據庫升級。android
6)建立的數據庫放在 /data/data/「package name」/database/ 目錄下面web
這裏咱們就建立一個數據庫實例:
數據庫名稱: BookStore.db
添加一張表:Book
表中有id(主鍵) 、 做者、 價格、 頁數、 書名 等列sql
create table Book ( id integer primary key autoincrement, author text, price real, pages integer, name text )
SQLite 的數據類型:
integer 表示整型
real 表示浮點型
text 表示文本類型
blob 表示二進制類型
上述語句咱們把primary key 列設爲主鍵, 並用autoincrement 關鍵字表示id 列是自增加的。數據庫
####三、一個示例架構
建立 一個數據庫 用於保存 學生的 : 姓名 性別 年齡 學號app
####四、代碼架構
ide
####五、主要代碼
activity_main.xml 文件svg
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context="com.example.menglux.mysqlitedata.MainActivity"> <Button android:id="@+id/creat_id" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="建立" android:textSize="30dp"/> <Button android:id="@+id/add_one_id" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="添加 one" android:textSize="30dp"/> <Button android:id="@+id/add_two_id" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="添加 two" android:textSize="30dp"/> <Button android:id="@+id/update_one_id" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="修改 one " android:textSize="30dp"/> <Button android:id="@+id/search_all_id" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="查找所有" android:textSize="30dp"/> <Button android:id="@+id/search_condition_id" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="條件查找" android:textSize="30dp"/> <Button android:id="@+id/delate_two_id" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="刪除 two" android:textSize="30dp"/> <Button android:id="@+id/delate_all_id" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="刪除 所有" android:textSize="30dp"/> </LinearLayout>
MainActivity.java
package com.example.menglux.mysqlitedata; import android.content.Context; import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.Button; public class MainActivity extends AppCompatActivity implements View.OnClickListener { private String TAG = "MainActivity: "; private Button buttonCreat, buttonAddOne, buttonAddTwo, buttonUpdate, buttonSearchAll, buttonSearchCondition,buttonDelateTwo,buttonDelateAll; private SQLiteDatabase db; private DataBaseOperation dop; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initvView(); //初始化組建信息 } //初始化組建信息 private void initvView() { buttonCreat = (Button) findViewById(R.id.creat_id); buttonAddOne = (Button) findViewById(R.id.add_one_id); buttonAddTwo = (Button) findViewById(R.id.add_two_id); buttonUpdate = (Button) findViewById(R.id.update_one_id); buttonSearchAll = (Button) findViewById(R.id.search_all_id); buttonSearchCondition = (Button) findViewById(R.id.search_condition_id); buttonDelateTwo = (Button) findViewById(R.id.delate_two_id); buttonDelateAll = (Button) findViewById(R.id.delate_all_id); buttonCreat.setOnClickListener(this); buttonAddOne.setOnClickListener(this); buttonAddTwo.setOnClickListener(this); buttonUpdate.setOnClickListener(this); buttonSearchAll.setOnClickListener(this); buttonSearchCondition.setOnClickListener(this); buttonDelateTwo.setOnClickListener(this); buttonDelateAll.setOnClickListener(this); dop = new DataBaseOperation(this, db); //實例化數據庫對象 } @Override public void onClick(View v) { switch(v.getId()) { case R.id.creat_id: //建立數據庫,若已經存在就打開 dop.create_db(); dop.close_db(); break; case R.id.add_one_id: //向數據庫添加 一個學生 one : 姓名 性別 年齡 學號 dop.create_db(); dop.insert_db("lum","boy",26,"528"); dop.close_db(); break; case R.id.add_two_id: //向數據庫添加 一個學生 two : 姓名 性別 年齡 學號 dop.create_db(); dop.insert_db("who","girl",24,"520"); dop.close_db(); break; case R.id.update_one_id: // 更新學生 根據學號 更新 one 的 姓名 年齡 dop.create_db(); dop.update_one("lumeng",28,"528"); dop.close_db(); break; case R.id.search_all_id: //查找數據庫所有信息 dop.create_db(); Cursor cursor = dop.query_db(); if (cursor.getCount() > 0) { //若是數據庫裏查詢到數據 while (cursor.moveToNext()) {// 光標移動成功 String str_name = cursor.getString(cursor .getColumnIndex("name")); // 得到姓名 String str_sex = cursor.getString(cursor .getColumnIndex("sex")); // 得到性別 int int_age = cursor.getInt(cursor .getColumnIndex("age")); // 得到年齡 String str_id = cursor.getString(cursor .getColumnIndex("id")); // 得到學號 System.out.println(TAG + "姓名: " + str_name + " 性別:" + str_sex + " 年齡:" + int_age + " 學號:" + str_id); } } dop.close_db(); break; case R.id.search_condition_id: //依照性別 boy 查找 學生 dop.create_db(); Cursor cursor_sex = dop.query_sex("boy"); if (cursor_sex.getCount() > 0) { //若是數據庫裏查詢到數據 while (cursor_sex.moveToNext()) {// 光標移動成功 String str_name = cursor_sex.getString(cursor_sex .getColumnIndex("name")); // 得到姓名 String str_sex = cursor_sex.getString(cursor_sex .getColumnIndex("sex")); // 得到性別 int int_age = cursor_sex.getInt(cursor_sex .getColumnIndex("age")); // 得到年齡 String str_id = cursor_sex.getString(cursor_sex .getColumnIndex("id")); // 得到學號 System.out.println(TAG + "姓名: " + str_name + " 性別:" + str_sex + " 年齡:" + int_age + " 學號:" + str_id); } } dop.close_db(); break; case R.id.delate_two_id: //根據學號 刪除學生 two dop.create_db(); dop.delate_two("520"); dop.close_db(); break; case R.id.delate_all_id: //刪除 所有學生 dop.create_db(); dop.delate_all(); dop.close_db(); break; default: break; } } }
DataBaseOperation.java
package com.example.menglux.mysqlitedata; import android.content.Context; import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; import android.widget.Toast; /** * Created by lum on 2018/5/6. */ public class DataBaseOperation { private final String TAG = "DataBaseOperation: "; private SQLiteDatabase db; private Context context; public DataBaseOperation(Context context, SQLiteDatabase db) { this.db = db; this.context = context; } //數據庫的打開或建立 db name student.db public void create_db() { db = SQLiteDatabase.openOrCreateDatabase(context.getFilesDir().toString() + "/student.db", null); if (db == null) { //判斷數據庫是否建立成功 System.out.println(TAG + "數據庫建立失敗" ); } //建立表,tab name 名稱爲 record ,主鍵id db.execSQL("create table if not exists record(_id integer primary key autoincrement," + "name varchar(30)," // 姓名 + "sex text," //性別 + "age integer," //年齡 + "id text" + ")");//學號 System.out.println(TAG + "數據庫建立成功" ); } //插入備忘錄信息到數據庫 public void insert_db(String name,String sex,int age,String id) { db.execSQL("insert into record(name,sex,age,id) values('" + name //姓名 + "','" + sex //性別 + "','" + age //年齡 + "','" + id //學號 + "');"); System.out.println(TAG + "插入新的數據庫信息" ); } //根據學號 更新學生 one 的 姓名 年齡 public void update_one( String name,int age , String id) { db.execSQL("update record set name='" + name + "',age='" + age + "'where id='" + id + "'"); System.out.println(TAG + "修改學生 one 資料" ); } //查詢全部內容 public Cursor query_db() { Cursor cursor = db.rawQuery("select * from record", null); System.out.println(TAG + "查找所有數據庫信息" ); return cursor; } //根據性別查找 public Cursor query_sex(String sex) { Cursor cursor = db.rawQuery("select * from record where sex='" + sex + "';", null); System.out.println(TAG + "根據性別查找" + sex ); return cursor; } // select * from 表名 where 學號 between '開始學號' and '結束學號' // 學號段查詢 public Cursor query_duing_id(String startid, String endid) { Cursor cursor = db.rawQuery("select * from record where id >='" + startid + "'and timeedit<='" + endid + "';", null); System.out.println(TAG + "學號段查詢" ); return cursor; } // select * from 表名 where content like '%abc%' //模糊查詢 查找全表中 姓名包含 關鍵字的學生 public Cursor query_content(String keword) { Cursor cursor = db.rawQuery("select * from record where name like '%" + keword + "%';", null); System.out.println(TAG + "關鍵字模糊查詢" ); return cursor; } //根據學號 刪除 學生 two public void delate_two( String id ) { db.execSQL("delete from record where id='" + id + "'"); System.out.println(TAG + "刪除學生 two"); } //刪除表所有內容 不刪除表 public void delate_all( ) { db.execSQL("delete from record" ); System.out.println(TAG + "清空表內容"); } // 關閉數據庫 public void close_db() { db.close(); System.out.println(TAG + "關閉數據庫"); } }
可用於在一個activity 建立數據庫,在另外一個activity 打開數據庫
文件參考:
android之存儲篇_SQLite數據庫_讓你完全學會SQLite的使用
http://www.javashuo.com/article/p-ueeanqil-ez.html