为什么要用SQLite
通过观察可以发现,不管是聊天列表还是音乐列表,有一个共性:
数据量大和数据结构复杂
那么为什么我们要用SQLite,有这么几个原因
1.SharedPreferences是以xml形式储存数据的,只适合储存基本类型的数据
2.文件储存的内容在提取(解析)数据时,相对复杂
3.当数据量大、结构复杂时,如果使用SharedPreferences和文件储存对数据的操作将变得非常复杂,容易出错,效率低下,Android提供了SQLite数据存贮,帮助我们解决这些问题
什么是SQLite
1.SQLite数据库存储是安卓系统提供的存储方式之一
2.SQLite是专为嵌入式设备设计的一款轻量级数据库
3.SQLite占用资源非常低,在嵌入式设备中,只需要几百kb的内存
4.SQLite支持标准的SQL语句,遵循数据库的ACID失误
5.SQLite不需要安装,不需要用户名密码就能使用
如何创建数据库和数据表
SQLite常用的几种数据类型为Text文本型,integer整型,real浮点型,建表格式要用规定的格式,如下
create table product{
id integer primary key autoincrement not null,
name text,
price real,
description text,
pic_url text
}
创建数据库和数据表的步骤
1.新建类继承SQLiteOpenHelper
2.实现构造方法
3.重写onCreate方法
4.重写onUpgrade方法
5.实例SQLiteOpenHelper的子类对象
我们简单的举个例子
package com.example.asus.webapplication.table;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
/**
* Created by asus on 2018/6/13.
*/
public class MySQLiteHelper extends SQLiteOpenHelper {
private String sql="create table student(" +
"id integer primary key autoincrement not null," +
"name text," +
"age integer" +
")";
public MySQLiteHelper(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) {
super(context, name, factory, version);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(sql);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
}
SQLiteOpenHelper是一个数据库辅助类,用来管理数据库的创建和版本。通过继承这个类,实现他的一些方法对数据库进行一些操作。
这里要记住,onCreate方法只会在第一次创建执行时执行,之后不再执行。
简单的增删改查的实现
首先,我们顶一个简单的布局:
<?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.asus.webapplication.SqlActivity">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="30sp"
android:textColor="#123456"
android:id="@+id/ed1_sql"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="30sp"
android:textColor="#123456"
android:id="@+id/ed2_sql"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="增加"
android:id="@+id/btn1_sql"
android:textSize="30sp"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="修改"
android:id="@+id/btn2_sql"
android:textSize="30sp"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="删除"
android:id="@+id/btn3_sql"
android:textSize="30sp"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="查询"
android:id="@+id/btn4_sql"
android:textSize="30sp"/>
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/list_sql">
</ListView>
</LinearLayout>
这里我们添加了一个listview,用来将查询出来的数据显示在listview中,数据表的创建详细见上面的代码,我们在这分别创建四个方法用来实现功能
private void addStudent() {
String name = ed1.getText().toString();
int age = Integer.parseInt(ed2.getText().toString());
ContentValues values = new ContentValues();
values.put("name", name);
values.put("age", age);
MySQLiteHelper mySqliteHelper = new MySQLiteHelper(this, "student_db", null, 1);
SQLiteDatabase db = mySqliteHelper.getWritableDatabase();
db.insert("student", null, values);
Log.e(TAG, "selectStudent:" + name + " " + age);
}
这个方法用来添加
private void upDateStudent() {
String name = ed1.getText().toString();
String age = ed2.getText().toString();
MySQLiteHelper mySqliteHelper = new MySQLiteHelper(this, "student_db", null, 1);
SQLiteDatabase db = mySqliteHelper.getWritableDatabase();
ContentValues values = new ContentValues();
values.put("name", "xxz");
db.update("student", values, "name=? and age=?", new String[]{name, age});
}
这个是修改的方法
private void delStudent() {
String name = ed1.getText().toString();
String age = ed2.getText().toString();
MySQLiteHelper mySqliteHelper = new MySQLiteHelper(this, "student_db", null, 1);
SQLiteDatabase db = mySqliteHelper.getWritableDatabase();
db.delete("student", "name=?", new String[]{name});
}
删除方法
private void selectStudent() {
MySQLiteHelper mySqliteHelper = new MySQLiteHelper(this, "student_db", null, 1);
SQLiteDatabase db = mySqliteHelper.getWritableDatabase();
Cursor cursor = db.query("student", null, null, null, null, null, null);
cursor.moveToFirst();
studentList.clear();
do {
String name = cursor.getString(cursor.getColumnIndex("name"));
int id = cursor.getInt(cursor.getColumnIndex("id"));
int age = cursor.getInt(cursor.getColumnIndex("age"));
Student s=new Student(id,name,age);
studentList.add(s);
Log.e(TAG, "selectStudent:" + id + " " + name + " " + age);
} while (cursor.moveToNext());
SqlAdapter adapter=new SqlAdapter(SqlActivity.this,studentList);
listView.setAdapter(adapter);
}
以及查询方法
在这里,我们主要来看看下面这段代码
MySQLiteHelper mySqliteHelper = new MySQLiteHelper(this, "student_db", null, 1);
SQLiteDatabase db = mySqliteHelper.getWritableDatabase();
通过创建MySQLiteHelper对象,用SQLiteDatabase调用getWritableDatabase()方法来实现手动输入添加