如何使用sqlite插入和检索数据显示按钮

时间:2017-01-15 05:31:41

标签: java android sqlite

点击按钮获取后,我无法在button中显示所有数据。请帮帮我。

 public class DBHelper extends SQLiteOpenHelper {
    private static final int VERSION = 1;
    private static final String DATABASE = "Test";
    private static final String TABLE_NAME = "savedata";
    private static final String KEY_ID = "id";
    private static final String KEY_NAME = "name";
    private static final String KEY_SEX = "sex";


    public DBHelper(Context context) {
        super(context, DATABASE, null,VERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        String sql = "CREATE TABLE IF NOT EXISTS " +TABLE_NAME + "("
                + KEY_ID + " INTEGER PRIMARY KEY AUTOINCREMENT,"
                + KEY_NAME + " TEXT,"
                + KEY_SEX + " TEXT)";
        db.execSQL(sql);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        db.execSQL("DROP TABLE IF EXISTS" +TABLE_NAME);
        onCreate(db);

    }
    public void Insertdata(Data data){
        SQLiteDatabase db = getWritableDatabase();
        ContentValues values = new ContentValues();
        values.put(KEY_NAME, data.getName());
        values.put(KEY_SEX, data.getSex());
        db.insert(TABLE_NAME, null, values);

    }
    public List<Data> getAllData(){
        List<Data> listData = new ArrayList<Data>();
        SQLiteDatabase db = this.getWritableDatabase();
        Cursor c = db.rawQuery("SELECT * FROM " +TABLE_NAME, null);
        if (c.moveToFirst()){
            do {
                Data data = new Data();
                data.setId(c.getInt(0));
                data.setName(c.getString(1));
                data.setSex(c.getString(2));
                listData.add(data);

            }while (c.moveToNext());
        }
        return listData;
    }
}

     protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        name = (EditText)findViewById(R.id.edit_name);
        sex = (EditText)findViewById(R.id.edit_sex);
        btn_get = (Button)findViewById(R.id.btn_get);
        btn_name = (Button)findViewById(R.id.btn_name);
        btn_sex = (Button)findViewById(R.id.btn_sex);
        btn_id = (Button)findViewById(R.id.btn_id);
        db = new DBHelper(this);
        _name = name.getText().toString();
        _sex = sex.getText().toString();
        data = new Data(_name,_sex);
        btn_save =( Button)findViewById(R.id.btn_save);

        SaveData();
        getData();

    }
    public void SaveData(){
        btn_save.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                try{
                    db.Insertdata(data);
                    Toast.makeText(getApplicationContext(), "SuccessFull",Toast.LENGTH_SHORT).show();
                }catch (Exception e){
                    Toast.makeText(getApplicationContext(), e.toString(), Toast.LENGTH_SHORT).show();
                }

            }
        });
    }
    public void getData(){
        btn_get.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                try {
                  if (currentData == null){
                      Toast.makeText(getApplicationContext(),"Null", Toast.LENGTH_SHORT).show();
                 }else {
                       currentData = db.getAllData();
                       btn_id.setText(String.valueOf(data.getId()));
                       btn_name.setText(data.getName());
                       btn_sex.setText(data.getSex());
                 }

                }catch (Exception e){
                    Toast.makeText(getApplicationContext(), e.toString(), Toast.LENGTH_SHORT).show();
                }
            }
        });
    }

1 个答案:

答案 0 :(得分:0)

你搞错了。这就是我所能建议的 -    1.创建一个类(例如“MyClass”),它将扩展AppCompatActivity并包含视图。    2.您已经为数据库操作创建了一个名为DBHelper的类。它扩展了SQLiteOpenHelper。    3.在MyClass的onCreate方法中创建DBHelper类的对象并访问您的数据库。 MyClass中的onCreate方法看起来与此类似 -

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    name = (EditText)findViewById(R.id.edit_name);
    sex = (EditText)findViewById(R.id.edit_sex);
    btn_get = (Button)findViewById(R.id.btn_get);
    btn_name = (Button)findViewById(R.id.btn_name);
    btn_sex = (Button)findViewById(R.id.btn_sex);
    btn_id = (Button)findViewById(R.id.btn_id);
    db = new DBHelper(this);
    _name = name.getText().toString();
    _sex = sex.getText().toString();
    data = new Data(_name,_sex);
    btn_save =( Button)findViewById(R.id.btn_save);
    btn_save.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            try{
                db.Insertdata(data);
                Toast.makeText(getApplicationContext(),      "SuccessFull",Toast.LENGTH_SHORT).show();
            }catch (Exception e){
                Toast.makeText(getApplicationContext(), e.toString(), Toast.LENGTH_SHORT).show();
            }

        }
    });

    btn_get.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            try {
                if (currentData == null){
                    Toast.makeText(getApplicationContext(),"Null", Toast.LENGTH_SHORT).show();
                }else {
                    currentData = db.getAllData();
                    btn_id.setText(String.valueOf(data.getId()));
                    btn_name.setText(data.getName());
                    btn_sex.setText(data.getSex());
                }

            }catch (Exception e){
                Toast.makeText(getApplicationContext(), e.toString(), Toast.LENGTH_SHORT).show();
            }
        }
    });
}

希望这可能有所帮助。