赞
踩
- public class MainActivity extends AppCompatActivity {
- //将需要用到的类单独拉出来
- Button addBtn;
- Button queryallButton;
- Button queryoneButton;
- EditText nameField;
- EditText ageField;
- EditText highFiled;
- TextView resultField;
- TextView idField;
-
- DbAdapter dbAdapter;//声明
-
-
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- //实例化
- dbAdapter = new DbAdapter(this);
- dbAdapter.open(); //链接数据库
-
-
- //1.组件相关配置
- addBtn = findViewById(R.id.addbutton);
- nameField = findViewById(R.id.namefileld);
- ageField = findViewById(R.id.agefield);
- highFiled = findViewById(R.id.highfield);
- queryallButton = findViewById(R.id.queryallButton);
- queryoneButton = findViewById(R.id.queryone);
- idField = findViewById(R.id.idField);
- resultField = findViewById(R.id.result);
- //2.保存按键中断
- addBtn.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- //拿到数据
- String name = nameField.getText().toString();
- int age = Integer.parseInt(ageField.getText().toString());
- float high = Float.parseFloat(highFiled.getText().toString());
-
- Person p = new Person(name,age,high);
- dbAdapter.insert(p);
- }
- });
- //3.查询按键中断
- queryallButton.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- String result = "";
-
- Person[] ps = dbAdapter.getALLData();
- for(Person p:ps)
- {
- result += p.name + "," + p.age + "," + p.high + "\n";
- }
- resultField.setText(result);
-
- }
- });
- //4.根据ID查询
- queryoneButton.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- String result = "";
- int id = Integer.parseInt(idField.getText().toString());
- Person p = dbAdapter.queryById(id);
- result += p.name + "," + p.age + "," + p.high + "\n";
- resultField.setText(result);
-
-
- }
- });
-
-
-
- }
- }
- //通过这个文件与Sqlite打交道
- public class DbAdapter {
- SQLiteDatabase db;
- Context context;
-
- DBOpenHelper dBopenhelper;
- //创建一个名称为people的数据库
- public static final String mysql = "people";
-
- public DbAdapter(Context context) {
- this.context = context;
- }
- //打开数据库
- public void open(){
- dBopenhelper = new DBOpenHelper(
- context,mysql,null,1);
- //真正的实例化db
- db = dBopenhelper.getReadableDatabase();
- try {
- db = dBopenhelper.getWritableDatabase();
- } catch (SQLException e) {
- db = dBopenhelper.getReadableDatabase();
- }
- }
- //关闭数据库
- public void close(){
- if (db!= null)
- {
- db.close();
- db = null;
- }
- }
- //添加数据到表格------!!!!!!重点看看!!!!!!!!!!!!
- public void insert(Person p) {
- ContentValues cv = new ContentValues();
- cv.put("name",p.name);
- cv.put("age",p.age);
- cv.put("high",p.high);
- db.insert(mysql,null,cv);
-
- }
-
- //读取数据用到------!!!!!!重点看看!!!!!!!!!!!!
- public Person[] getALLData() {
- //cursor为指针
- Cursor cursor = db.query(mysql,new String[]{"_id","name","age","high"},
- null,null,null,null,null);
- return convertToPerson(cursor);
- }
-
- @SuppressLint("Range")
- //------!!!!!!重点看看!!!!!!!!!!!!
- //这个函数是用来处理从Sqlite中获取的数据
- private Person[] convertToPerson(Cursor cursor) {
- int rowCount = cursor.getCount();
- if(rowCount == 0 || !cursor.moveToFirst())
- {
- return null;
- }
-
- Person[] ps = new Person[rowCount];
- for(int i=0;i<rowCount;i++)
- {
- ps[i] = new Person();
- //重点掌握!!!!!!!!!!!!!!!!!!!!!!!!!!!
- ps[i].name = cursor.getString(cursor.getColumnIndex("name"));
- ps[i].age = cursor.getInt(cursor.getColumnIndex("age"));
- ps[i].high = cursor.getFloat(cursor.getColumnIndex("high"));
- cursor.moveToNext();
-
- }
- return ps;
- }
-
- public Person queryById(int id) {
- //重点掌握!!!!!!!!!!!!!!!!!!!!!!!!!!!
- Cursor cursor = db.query(mysql,new String[]{"_id","name","age","high"},
- "_id="+id,null,null,null,null);
- Person[] ps = convertToPerson(cursor);
- if(ps != null)
- {
- return ps[0];
- }
- return null;
- }
-
- private class DBOpenHelper extends SQLiteOpenHelper {
- //构造方法
- public DBOpenHelper(@Nullable Context context, @Nullable String name, @Nullable SQLiteDatabase.CursorFactory factory, int version) {
- super(context, name, factory, version);
- }
- private static final String DB_CREATE =
- "create table "+ mysql +"("+
- "_id integer primary key autoincrement,"+
- "name text,"+
- "age integer,"+
- "high float)";
- //重要
- @Override
- public void onCreate(SQLiteDatabase db) {
- db.execSQL(DB_CREATE);//执行SQL代码
-
- }
-
- @Override
- public void onUpgrade(
- SQLiteDatabase sqLiteDatabase, int i, int i1) {
-
- }
- }
- }
- //描述数据表的结构
- public class Person {
- String name;
- int age;
- float high;
-
- public Person() {
-
- }
- //增加一个构造方法 -- 本质就是用来创建实例时进行赋值
- public Person(String name, int age, float high) {
- this.name = name;
- this.age = age;
- this.high = high;
- }
-
-
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。