您当前的位置:首页 > 计算机 > 编程开发 > 安卓(android)开发

Android SQLite增删改查基本操作

时间:01-12来源:作者:点击数:
SQLite增删改查基本操作并用listview显示出来:

如图:

在这里插入图片描述
1. 实现增删改查按钮 布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
   >
    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="add"
        android:onClick="add"/>
    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="delete"
        android:onClick="delete"/>
    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="alter"
        android:onClick="alter"/>
    <Button
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="query"
    android:onClick="query"/>

    <ListView
        android:id="@+id/lv_db"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>
</LinearLayout>
item布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">


<TextView
    android:id="@+id/tv_name"
    android:layout_width="match_parent"
    android:layout_weight="1"
    android:textSize="30dp"
    android:layout_height="wrap_content"
    android:text=""/>
    <TextView
        android:id="@+id/tv_phone"
        android:layout_width="match_parent"
        android:layout_weight="1"
        android:textSize="30dp"
        android:layout_height="wrap_content"
        android:text=""/>
</LinearLayout>
2.建立MyDBOpenhelper继承自SQLiteOpenHelper
public class MyDBOpenHelper extends SQLiteOpenHelper {
    
    private final String DB_FOOD = "create table food(_id integer primary key autoincrement,name varchar(20),phone varchar(20))";

    public MyDBOpenHelper( Context context) {
        super(context, "food.db", null, 1); }

    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL(DB_FOOD);
    }
    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    }
}
onupgrade这个方法只有版本号比之前大的时候才会执行
3.数据封装 我们有两个参数分别是name和phone

alt+insert之后选择

在这里插入图片描述
public class Food {
   private String name;
   private String phone;

    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;
    }
}
4.设定自己的适配器Myadpter继承自BaseAdaper

(具体方法之前的文章有介绍)ListView简单使用实例

public class MyAdapter extends BaseAdapter {
    private Context context;
    private List<Food> foodList;
    public MyAdapter(Context context,List<Food>foodList){
        this.context=context;
        this.foodList=foodList;
    }
    @Override
    public int getCount() {
        return foodList.size();
    }

    @Override
    public Object getItem(int position) {
        return null;
    }
    @Override
    public long getItemId(int position) {
        return 0;
    }
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView==null){
            convertView=View.inflate(context,R.layout.item,null);
             holder = new ViewHolder();
             holder.tv_name=(TextView)convertView.findViewById(R.id.tv_name);
            holder.tv_phone=(TextView)convertView.findViewById(R.id.tv_phone);
            convertView.setTag(holder);
        }else {
            holder = (ViewHolder) convertView.getTag();
        }
        holder.tv_name.setText(foodList.get(position).getName());
        holder.tv_phone.setText(foodList.get(position).getPhone());
        return convertView;}
    private static class ViewHolder{
        TextView tv_name;
        TextView tv_phone;
    }
}
5.实现功能
public class MainActivity extends AppCompatActivity {
    private MyDBOpenHelper myDBOpenHelper;
    private ListView lv_db;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        myDBOpenHelper = new MyDBOpenHelper(this);
        lv_db = (ListView) findViewById(R.id.lv_db);
    }
    public void add(View view) {
        SQLiteDatabase db = myDBOpenHelper.getReadableDatabase();
        ContentValues values = new ContentValues();
        values.put("name", "苹果");
        values.put("phone", "666");
        long result = db.insert("food", null, values);
        if (result > 0)
            Toast.makeText(this, "success", Toast.LENGTH_SHORT).show();
        else
            Toast.makeText(this, "fail", Toast.LENGTH_SHORT).show();
        db.close();
    }
    public void delete(View view) {
        SQLiteDatabase db = myDBOpenHelper.getReadableDatabase();
        int result = db.delete("food", "1", null);
        if (result > 0)
            Toast.makeText(this, "delete" + result + "row", Toast.LENGTH_SHORT).show();
        else
            Toast.makeText(this, "delete 0 row", Toast.LENGTH_SHORT).show();
        db.close();
    }
    public void alter(View view) {
        SQLiteDatabase db = myDBOpenHelper.getReadableDatabase();
        ContentValues values = new ContentValues();
        values.put("name", "葡萄");
        int result = db.update("food", values, "phone=?", new String[]{"666"});
        if (result > 0)
            Toast.makeText(this, "alter" + result + "rows", Toast.LENGTH_SHORT).show();
        else
            Toast.makeText(this, "alter 0 row", Toast.LENGTH_SHORT).show();
        db.close();
    }
    public void query(View view) {
        ArrayList<Food> foodList = new ArrayList<>();
        SQLiteDatabase db = myDBOpenHelper.getReadableDatabase();
        Cursor cursor = db.query("food", new String[]{"name", "phone"}, null, null, null, null, null);

        if (cursor != null && cursor.getCount() > 0) {
            while (cursor.moveToNext()) {
                String name = cursor.getString(0);/*开始读*/
                String phone = cursor.getString(1);
                Food food = new Food();
                food.setName(name);
                food.setPhone(phone);
                foodList.add(food);
                Log.v("MainActivity_query", "query:name=" + name + "/phone=" + phone);/*显示*/
            }
        }
        cursor.close();
        db.close();
        MyAdapter myAdapter = new MyAdapter(this,foodList);
        lv_db.setAdapter(myAdapter);
    }
}
6.来看下效果吧

点击add,显示

在这里插入图片描述

点击delete,显示

在这里插入图片描述

点击alter,显示

在这里插入图片描述
sql语句和android api语句优缺点:
execsql没有返回值 android api语句有返回值 可以判断成功不成功
api只能做单表操作 不能对表的结构增删改查
方便获取更多学习、工作、生活信息请关注本站微信公众号城东书院 微信服务号城东书院 微信订阅号
推荐内容
相关内容
栏目更新
栏目热门