求赞

基于Android的SQLite数据库操作

功能介绍一、创库二、基本操作1.实例化sql对象2.添加数据2.查询数据3.修改数据4.删除数据

功能介绍

本文的增删改查方法都Android自带的SQLiteOpenHelper,虽然直接用sql语句来映射相应的数据库操作比较直观且便于管理,但是学习总是一条漫长的过程。。。。。

相关文章:

Android 开发之 Sqlite数据库操作 Android Cursor(光标)解析 android query方法各个参数的含义

一、创库

引入SQLiteOpenHelper

class MyHelper extends SQLiteOpenHelper {

public MyHelper(Context context) {

super(context, "itcast.db", null, 1);

}

@Override

public void onCreate(SQLiteDatabase db) {

db.execSQL("CREATE TABLE information(_id INTEGER PRIMARY KEY AUTOINCREMENT, name VARCHAR(20), phone VARCHAR(20))");

}

@Override

public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

}

}

此时就可以通过MyHelper对象来使用创建的数据库了。

id(主键,自动添加)namephone

二、基本操作

1.实例化sql对象

MyHelper myHelper;

SQLiteDatabase db;

//需要的时候才建立连接

db = myHelper.getWritableDatabase();

//注意:每次使用过后记得关

db.close();

2.添加数据

public long insert (String table, String nullColumnHack, ContentValues values)

table 要插入数据的表的名称 values ContentValues对象,类似一个map通过键值对的形式存储值。

nullColumnHack 当values参数为空或者里面没有内容的时候,insert是会失败的(底层数据库不允许插入一个空行),为了防止这种情况,要在这里指定一个列名,到时候如果发现将要插入的行为空行时,就会将你指定的这个列名的值设为null,然后再向数据库中插入。

若不添加nullColumnHack则sql语句最终的结果将会类似insert into tableName()values();这是不允许的。 若添加上nullColumnHack则sql语句将会变成insert into tableName (nullColumnHack)values(null);这是可以的。

ContentValues values;

values = new ContentValues(); //创建ContentValues对象

values.put("name", name); //将数据添加到ContentValues对象

values.put("phone", phone);

db.insert("information", null, values);

2.查询数据

public Cursor query(String table, String[] columns, String selection, String[] selectionArgs, String groupBy, String having, String orderBy)

table :表名。相当于 select 语句 from 关键字后面的部分。如果是多表联合查询,可以用逗号将两个表名分开。 columns :要查询出来的列名。相当于 select 语句 select 关键字后面的部分。 selection :查询条件子句,相当于 select 语句 where 关键字后面的部分,在条件子句允许使用占位符 “?” selectionArgs :对应于 selection 语句中占位符的值,值在数组中的位置与占位符在语句中的位置必须一致,否则就会有异常。 groupBy :相当于 select 语句 group by 关键字后面的部分 having :相当于 select 语句 having 关键字后面的部分 orderBy :相当于 select 语句 order by 关键字后面的部分,如: personid desc, age asc; limit :指定偏移量和获取的记录数,相当于 select 语句 limit 关键字后面的部分。

查重

//给定name和phone的值,查是否重复

public boolean Findperpeture(String findName, String findTel) {

//数据源

SQLiteDatabase db = myHelper.getWritableDatabase();

//自带sql查询直接查

Cursor cursor = db.query("information",

new String[]{"name", "phone"},

"name=? and phone=?",

new String[]{findName, findTel},

null, null, null);

if (cursor.getCount() != 0) {

//有数据,说明数据冗余了。

return true;

} else {

return false;

}

}

单个查询

此处有三种光标的遍历方法

//单个查询

private String OnlySelect(String selectName, String selectTel) {

String info = "";

//数据源

SQLiteDatabase db = myHelper.getWritableDatabase();

if (selectTel != null && selectName.isEmpty()) {

info = "";

//查电话

Cursor cursor = db.query("information", new String[]{"name", "phone"}, "phone=?", new String[]{selectTel},

null, null, null);

//查到了

if (cursor.getCount() != 0) {

while (cursor.moveToNext()) {

info += "\n" + "Name : " + cursor.getString(0) + " ;Tel : " + cursor.getString(1);

}

}

//没查到

else {

info = "查无此人电话";

}

} else if (selectTel.isEmpty() && selectName != null) {

info = "";

//查姓名

Cursor cursor = db.query("information", new String[]{"name", "phone"}, "name=?", new String[]{selectName},

null, null, null);

//查到了

if (cursor.getCount() != 0) {

// 三种移动光标的遍历写法:

// 1.move()

// 游标的move方法,cursor.move()方法是根据当前位置移动的数量,而不是从第一条移动到指定位置。

// 当move(1)时,光标移动到第一个行,知道遍历到最后一行,返回False

// 注:不能写成for (int i = 1; i <= cursor.getCount(); i++)

// {cursor.move(i);}

// move(1)之后马上执行move(2),就会使得光标越界(总信息量为2)

// while (cursor.move(1))

// {

// info += "\n" + "Name : " + cursor.getString(0) + " ;Tel : " + cursor.getString(1);

// }

//2.moveToPosition();

//将光标移动到绝对位置

//基于1.的疑问,可以使用该方法使用for循环精准遍历想要的位置

//注意,是从0开始的。

// for (int i = 0; i < cursor.getCount(); i++)

// {

// cursor.moveToPosition(i);

// info += "\n" + "Name : " + cursor.getString(0) + " ;Tel : " + cursor.getString(1);

// }

//3.moveToNext()

//将光标移动到下一行

while (cursor.moveToNext()) {

info += "\n" + "Name : " + cursor.getString(0) + " ;Tel : " + cursor.getString(1);

}

}

//没查到

else {

info = "查无此人姓名";

}

} else if (selectTel != null && selectName != null) {

//精准查询 这里只有一条数据,因为之前添加的时候就不会添加两条数据一样的信息

Cursor cursor = db.query("information", new String[]{"name", "phone"}, "name=? and phone=?", new String[]{selectName, selectTel},

null, null, null);

//查到了

if (cursor.getCount() != 0) {

for (int i = 1; i <= cursor.getCount(); i++) {

cursor.move(i);

info += "\n" + "Name : " + cursor.getString(0) + " ;Tel : " + cursor.getString(1);

}

}

//没查到

else {

info = "查无此人";

}

}

return info;

}

3.修改数据

public int update(String table, ContentValues values, String whereClause, String[] whereArgs)

table表名 values修改的值 whereClause条件,相当于where whereArgs条件的值

修改phone的值(条件:name=?)

values = new ContentValues(); // 要修改的数据

values.put("phone", phone = mEtPhone.getText().toString());

db.update("information", values,

"name=?",

new String[]{mEtName.getText().toString()}); // 更新并得到行数

4.删除数据

public int delete(String table, String whereClause, String[] whereArgs)

table表名 whereClause条件,相当于where whereArgs条件的值

删除name=?的行

db.delete("information", "name=?", new String[]{name});

查看原文