数据库的创建
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)
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:
break;
}
}
public void insert(MySQL mySQL) {
mySQLDao.insert(mySQL);
}
public List<MySQL> selete(String name) {
List<MySQL> list = mySQLDao.queryBuilder().build().list();
return list;
}
}