项目结构
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/55fdd8d4432d48e4f4b810c2f5e77fcb.png)
效果展示
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/a73b8cdc0983081f60e8efbab7ade495.gif)
关键代码
- 数据库操作代码
package com.example.dell.mysqlite_crud;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;
public class MyDAO {
private SQLiteDatabase myDb;
private DbHelper dbHelper;
public MyDAO(Context context) {
dbHelper = new DbHelper(context,"test.db",null,1);
}
public Cursor allQuery(){
myDb = dbHelper.getReadableDatabase();
return myDb.rawQuery("select * from friends",null);
}
public int getRecordsNumber(){
myDb = dbHelper.getReadableDatabase();
Cursor cursor= myDb.rawQuery("select * from friends",null);
return cursor.getCount();
}
public void insertInfo(String name,int age){
myDb = dbHelper.getWritableDatabase();
ContentValues values = new ContentValues();
values.put("name", name);
values.put("age", age);
long rowid=myDb.insert(DbHelper.TB_NAME, null, values);
if(rowid==-1)
Log.i("myDbDemo", "数据插入失败!");
else
Log.i("myDbDemo", "数据插入成功!"+rowid);
}
public void deleteInfo(String selId){
String where = "_id=" + selId;
int i = myDb.delete(DbHelper.TB_NAME, where, null);
if (i > 0)
Log.i("myDbDemo", "数据删除成功!");
else
Log.i("myDbDemo"