SQLite是一款轻量级的关系型数据库,具有运行速度、占用资源少的特点。通常只需要几百KB的内存就够了,因此特别适合在移动设备上使用。SQLite不仅支持标准的SQL语法,还遵循数据库的ACID事务。Android为了让我们更方便的管理数据库,专门提供了一个SQLiteOpenHelper帮助类,借助这个类我们很容易对数据库进行创建和升级。
Android中实现SQLite数据库CRUD操作的方式有两种:
1:直接用SQL语句进行操作
2:调用Android提供的系统函数
本文为了更好的说明对数据库的增删改查操作,做了两个前提条件:
1: 定义了一个User.java类
package com.example.sqlite;
import android.R.integer;
public class User {
private String name;
private String phone;
private int age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "name=" + name + ", phone=" + phone + ", age=" + age + "+\n\n";
}
}
2:定义一个MySQLiteOpenHelper 并继承SQLiteOpenHelper
package com.example.sqlite;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
public class MySQLiteOpenHelper extends SQLiteOpenHelper{
/**
* @param context: 上下文
* @param name:数据库的名称
* @param factory: null代表使用系统默认的游标
* @param version:数据库文件的版本号,必须大于等于1
*/
public MySQLiteOpenHelper(Context context, String name, CursorFactory factory, int version) {
super(context, name, factory, version);
}
/**
* 用于创建表的结构
* 第一次使用MySQLiteOpenHelper类时,如果表结构没有被创建就执行一次,创建表的结构,如果表的结构创建了就不执行
*/
@Override
public void onCreate(SQLiteDatabase db) {
Log.d("tag", "oncrate");
String sql = "create table t_user (_id integer primary key,name varchar(20), phone varchar(20) ,age integer)";
db.execSQL(sql );
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
}
public void insertOne(View view){
//得到一个MySQLiteOpenHelper对象
MySQLiteOpenHelper sqLiteOpenHelper = new MySQLiteOpenHelper(this, DB_NAME, null, VERSION);
//拿到SQLiteDateBase对象
SQLiteDatabase dataBase = sqLiteOpenHelper.getReadableDatabase();
//向表中插入数据
String sql= "insert into t_user (name,phone,age) values (?,?,?)";
dataBase.execSQL(sql, new Object[]{"zhangsan"+new Random().nextInt(100),(5000+new Random().nextInt(100)+""),new Random().nextInt(100)});
//关闭资源
dataBase.close();
Toast.makeText(this, "方法1插入数据成功!", Toast.LENGTH_SHORT).show();
}
public void deleteOne(View view){
//得到一个MySQLiteOpenHelper对象
MySQLiteOpenHelper sqLiteOpenHelper = new MySQLiteOpenHelper(this, DB_NAME, null, VERSION);
SQLiteDatabase database = sqLiteOpenHelper.getWritableDatabase();
//删除age < 30 的user
String sql = "delete from t_user where age < ?";
database.execSQL(sql , new Object []{30});
//关闭资源
database.close();
Toast.makeText(this, "方法1删除数据成功!", Toast.LENGTH_SHORT).show();
}
public void updateOne(View view){
MySQLiteOpenHelper sqLiteOpenHelper = new MySQLiteOpenHelper(this, DB_NAME, null, VERSION);
//得到SQLiteDataBase对象
SQLiteDatabase database = sqLiteOpenHelper.getReadableDatabase();
//更新 name = zhangsan45时,age修改为58;
String sql = "update t_user set age = ? where name = ?";
database.execSQL(sql , new Object[]{58,"zhangsan45"});
//关闭资源
database.close();
Toast.makeText(this, "方法1更新数据成功!", Toast.LENGTH_SHORT).show();
}
public void queryOne(View view){
ArrayList userList = new ArrayList();
MySQLiteOpenHelper sqLiteOpenHelper = new MySQLiteOpenHelper(this, DB_NAME, null, VERSION);
//得到SQLiteDataBase对象
SQLiteDatabase database = sqLiteOpenHelper.getReadableDatabase();
//查询年龄大于50,且desc排序
String sql = "select name ,phone,age from t_user where age > ? order by age desc";
Cursor cursor = database.rawQuery(sql, new String[]{50+""});
while(cursor.moveToNext()){
User user = new User();
user.setName(cursor.getString(0));
user.setPhone(cursor.getString(1));
user.setAge(cursor.getInt(2));
userList.add(user);
}
//释放cursor
cursor.close();
//关闭资源
database.close();
//在一个TextView中显示查询到的数据
tv_data.setText(userList.toString());
Toast.makeText(this, "方法1查询数据成功!", Toast.LENGTH_SHORT).show();
}
public void insertTwo(View view){
MySQLiteOpenHelper sqLiteOpenHelper = new MySQLiteOpenHelper(this, DB_NAME, null, VERSION);
SQLiteDatabase database = sqLiteOpenHelper.getReadableDatabase();
ContentValues values = new ContentValues();
values.put("name", "lisi"+new Random().nextInt(100));
values.put("phone", "8000"+ new Random().nextInt(100));
values.put("age", new Random().nextInt(100));
long insertNum = database.insert("t_user", null, values );
Toast.makeText(this, "成功插入第"+insertNum+"条数据!", Toast.LENGTH_SHORT).show();
//关闭资源
database.close();
}
public void deleteTwo (View view){
MySQLiteOpenHelper sqLiteOpenHelper = new MySQLiteOpenHelper(this, DB_NAME, null, VERSION);
SQLiteDatabase database = sqLiteOpenHelper.getReadableDatabase();
//删除 age>90的user
database.delete("t_user", "age > ?", new String []{"90"});
Toast.makeText(this, "方法2删除数据成功!", Toast.LENGTH_SHORT).show();
//关闭资源
database.close();
}
public void updateTwo(View view){
MySQLiteOpenHelper sqLiteOpenHelper = new MySQLiteOpenHelper(this, DB_NAME, null, VERSION);
SQLiteDatabase database = sqLiteOpenHelper.getReadableDatabase();
//将name = zhangsan96 的age改为68
ContentValues values = new ContentValues();
values.put("age", "68");
int update = database.update("t_user", values , "name = ?", new String[]{"zhangsan96"});
Toast.makeText(this, "方法2更新数据成功!", Toast.LENGTH_SHORT).show();
//关闭资源
database.close();
}
public void queryTwo(View view){
MySQLiteOpenHelper sqLiteOpenHelper = new MySQLiteOpenHelper(this, DB_NAME, null, VERSION);
SQLiteDatabase database = sqLiteOpenHelper.getReadableDatabase();
//查询年龄大于60的user
String sql = "select name ,phone,age from t_user where age > ?";
Cursor cursor = database.rawQuery(sql , new String[]{"60"});
ArrayList list = new ArrayList();
while(cursor.moveToNext()){
User user = new User();
user.setName(cursor.getString(0));
user.setPhone(cursor.getString(1));
user.setAge(cursor.getInt(2));
list.add(user);
}
Toast.makeText(this, "方法2查询数据成功!", Toast.LENGTH_SHORT).show();
//关闭资源
database.close();
//显示查询到的数据
tv_data.setText(list.toString());
}
OK,本文到此结束。让我们共同学习,一起进步。