数据库使用

数据库的创建

package com.example.myday1night;

import org.greenrobot.greendao.annotation.Entity;
import org.greenrobot.greendao.annotation.Id;
import org.greenrobot.greendao.annotation.Property;
import org.greenrobot.greendao.annotation.Generated;

@Entity
public class MySQL {
    @Id(autoincrement = true)//代表ID自增
    private long id;
    @Property(nameInDb = "name")
    private String name;
    @Property(nameInDb = "price")
    private int price;
    @Property(nameInDb = "count")
    private int count;
    @Generated(hash = 1320634197)
    public MySQL(long id, String name, int price, int count) {
        this.id = id;
        this.name = name;
        this.price = price;
        this.count = count;
    }
    @Generated(hash = 2111380415)
    public MySQL() {
    }
    public long getId() {
        return this.id;
    }
    public void setId(long id) {
        this.id = id;
    }
    public String getName() {
        return this.name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public int getPrice() {
        return this.price;
    }
    public void setPrice(int price) {
        this.price = price;
    }
    public int getCount() {
        return this.count;
    }
    public void setCount(int count) {
        this.count = count;
    }

}

使用数据库Activity的代码

package com.example.myday1night;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import java.util.List;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    private EditText recy_name;
    private Button recy_insert;
    private RecyclerView recy;
    
    private Button recy_selete;
    private EditText recy_id;
    private Button quanxuan;


    DaoSession daoSession;
    MySQLDao mySQLDao;
    List<MySQL> selete;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        DaoMaster.DevOpenHelper helper = new DaoMaster.DevOpenHelper(this, "aaa.db");//创建数据库
        DaoMaster daoMaster = new DaoMaster(helper.getWritableDb());
        daoSession = daoMaster.newSession();
        mySQLDao = daoSession.getMySQLDao();
    }

    private void initView() {
        recy_name = (EditText) findViewById(R.id.recy_name);
        recy_insert = (Button) findViewById(R.id.recy_insert);
        recy = (RecyclerView) findViewById(R.id.recy);
        recy.setLayoutManager(new LinearLayoutManager(MainActivity.this));
        recy_insert.setOnClickListener(this);
        recy_selete = (Button) findViewById(R.id.recy_selete);
        recy_selete.setOnClickListener(this);
        recy_id = (EditText) findViewById(R.id.recy_id);
        recy_id.setOnClickListener(this);
        quanxuan = (Button) findViewById(R.id.quanxuan);
        quanxuan.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.recy_insert:
                MySQL mySQL = new MySQL(Integer.parseInt(recy_id.getText().toString()), recy_name.getText().toString(), 10000, 0);
                insert(mySQL);
                break;
            case R.id.recy_selete:
                selete= selete("");
                Toast.makeText(this, "" + selete.toString(), Toast.LENGTH_SHORT).show();
                MyAdapter myAdapter = new MyAdapter(selete, MainActivity.this);
                recy.setAdapter(myAdapter);
                break;
            case R.id.quanxuan:
//                for (int i = 0; i <selete.size() ; i++) {
//                    if (selete.get(i).getIsaaa()){
//                        selete.get(i).setIsaaa(false);
//                    }else{
//                        selete.get(i).setIsaaa(true);
//                    }
//
//                }
                break;
        }
    }

    public void insert(MySQL mySQL) {
        mySQLDao.insert(mySQL);
    }

    public List<MySQL> selete(String name) {
        List<MySQL> list = mySQLDao.queryBuilder().build().list();
        return list;
    }


}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值