android之数据库的增删改查

1  创建数据库:

package itcast.zz.cdemo;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

/**
 * Created by SensYang on 2017/06/12 14:54
 */

public class TaoSQLite extends SQLiteOpenHelper {
    public TaoSQLite(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) {
        super(context, name, factory, version);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL("create table shop(_id integer primary key,name varchar, price varchar, style varchat )");
    }

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

    }
}


2 第二步,搞一个布局来显示数据库:

<?xml version="1.0" encoding="utf-8"?>
<layout
    xmlns:android="http://schemas.android.com/apk/res/android"

    >

    <data>
        <variable
            name="main"
            type="itcast.zz.cdemo.MainActivity"/>
    </data>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@color/white"
        android:orientation="vertical"
        >

        <View
            android:layout_width="match_parent"
            android:layout_height="@dimen/ten"
            android:background="@color/fafafa"
            />

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="@dimen/fifty"
            android:background="@color/white"
            android:orientation="horizontal">


            <TextView
                android:onClick="@{main.add}"
                android:layout_width="0dp"
                android:layout_height="@dimen/fifty"
                android:layout_weight="1"
                android:gravity="center"
                android:text="增"
                android:textColor="@color/textcolor"
                android:textSize="@dimen/fifteen"
                />

            <View
                android:layout_width="@dimen/one"
                android:layout_height="match_parent"
                android:background="@color/bebebe"
                />

            <TextView
                android:onClick="@{main.delete}"
                android:layout_width="0dp"
                android:layout_height="@dimen/fifty"
                android:layout_weight="1"
                android:gravity="center"
                android:text="删"
                android:textColor="@color/textcolor"
                android:textSize="@dimen/fifteen"
                />

            <View
                android:layout_width="@dimen/one"
                android:layout_height="match_parent"
                android:background="@color/bebebe"
                />

            <TextView
                android:onClick="@{main.change}"
                android:layout_width="0dp"
                android:layout_height="@dimen/fifty"
                android:layout_weight="1"
                android:gravity="center"
                android:text="改"
                android:textColor="@color/textcolor"
                android:textSize="@dimen/fifteen"
                />

            <View
                android:layout_width="@dimen/one"
                android:layout_height="match_parent"
                android:background="@color/bebebe"
                />

            <TextView
                android:onClick="@{main.query}"
                android:layout_width="0dp"
                android:layout_height="@dimen/fifty"
                android:layout_weight="1"
                android:gravity="center"
                android:text="查"
                android:textColor="@color/textcolor"
                android:textSize="@dimen/fifteen"
                />
        </LinearLayout>

        <View
            android:layout_width="match_parent"
            android:layout_height="@dimen/ten"
            android:background="@color/fafafa"
            />

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="@color/white"
            android:orientation="vertical"
            >

            <android.support.v7.widget.RecyclerView
                android:id="@+id/recycler"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                />

        </LinearLayout>

    </LinearLayout>

</layout>

3 第三部,操作数据库:

package itcast.zz.cdemo;

import android.app.Activity;
import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;

import itcast.zz.cdemo.databinding.ActivityMainBinding;

public class MainActivity extends Activity {

    private TaoSQLite tao;
    private SQLiteDatabase sqLiteDatabase;
    private RecyclerView recyclerView;
    private ActivityMainBinding mainBinding;
    private HomeAdapter adapter;
    //需要传递的数组
    private List<Shop> s = new ArrayList<>();
    private List<Shop> shopList = new ArrayList<>();
    private Shop shop;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mainBinding = DataBindingUtil.setContentView(this,R.layout.activity_main);
        mainBinding.setMain(this);
        initView();
    }

    private void initView() {
        tao = new TaoSQLite(this,"tao.db",null,1);
        mainBinding.recycler.setLayoutManager(new LinearLayoutManager(this));
        adapter = new HomeAdapter(this,s);
        mainBinding.recycler.setAdapter(adapter);
    }
    public void add(View view){

        sqLiteDatabase = tao.getWritableDatabase();
        sqLiteDatabase.execSQL("insert into shop(name,price,style) values('卫生巾','7','日用型')");

        Toast.makeText(this,"增加",Toast.LENGTH_SHORT).show();
    }
    public void delete(View view){

        sqLiteDatabase = tao.getWritableDatabase();
        sqLiteDatabase.delete("shop","name = ?",new String[]{"卫生巾"});
        Toast.makeText(this,"删除",Toast.LENGTH_SHORT).show();
    }
    public void change(View view){
        Toast.makeText(this,"修改",Toast.LENGTH_SHORT).show();
//        String upmyinfo = "update shop set name = '安尔乐' , price = '10' where style = '日用型'";
        ContentValues value = new ContentValues();
        value.put("name","安尔乐");
        value.put("price","10");
        value.put("style","夜用型");
        sqLiteDatabase = tao.getWritableDatabase();
        sqLiteDatabase.update("shop",value,"name = ?",new String[]{"卫生巾"});
    }
    public void query(View view){
        adapter.clearItem();
        sqLiteDatabase = tao.getReadableDatabase();
        Cursor cursor = sqLiteDatabase.query("shop",null,null,null,null,null,null);
        while(cursor.moveToNext()){
            shop = new Shop();
            shop.setName(cursor.getString(cursor.getColumnIndex("name")));
            shop.setPrice(cursor.getString(cursor.getColumnIndex("price")));
            shop.setStyle(cursor.getString(cursor.getColumnIndex("style")));
            if (shopList.contains(shop)) {
                Toast.makeText(this,"重复了吆",Toast.LENGTH_SHORT).show();
            }else{
                shopList.add(shop);
            }
        }
        sqLiteDatabase.close();
        adapter.addAll(shopList);
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                adapter.notifyDataSetChanged();
            }
        });
        Toast.makeText(this,"查找",Toast.LENGTH_SHORT).show();
    }
}

最后的结果会有点意外,是因为每查找一次,ShopList就会重新叠加上一次查找到的数据。所以,查找一次再查找是不准确的,最简单的退出去重新进就好了。


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值