android studio(as)实现简单的增删改查的功能

提示:若报错,检查包名是否一致,还有在AndroidManifest.xml中是否配置好

MainActivity.java文件代码

    package com.example.wzx.myapplication;
    
    import android.content.ContentValues;
    import android.content.Context;
    import android.database.Cursor;
    import android.database.sqlite.SQLiteDatabase;
    import android.database.sqlite.SQLiteOpenHelper;
    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
    import android.view.View;
    import android.widget.Button;
    import android.widget.EditText;
    import android.widget.TextView;
    import android.widget.Toast;
    
    public class MainActivity extends AppCompatActivity implements View.OnClickListener {
   
        MyHelper myHelper;
        private EditText mEtName;
        private EditText mEtPhone;
        private EditText mEtEmail;
        private TextView mTvShow;
        private Button mBtAdd;
        private Button mBtQuery;
        private Button mBtUpdate;
        private Button mBtDelete;
        public String name;
        public String phone;
        public String email;
        public SQLiteDatabase db;
        public ContentValues values;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
   
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            myHelper = new MyHelper(this);
            init();
        }
        //实例化界面,获取各个控件的值
        private void init() {
   
            mEtName = (EditText) findViewById(R.id.et_name);
            mEtPhone = (EditText) findViewById(R.id.et_phone);
            mEtEmail = (EditText) findViewById(R.id.et_email);
            mTvShow = (TextView) findViewById(R.id.ld_show);
            mBtAdd = (Button) findViewById(R.id.btn_add);
            mBtUpdate = (Button) findViewById(R.id.btn_update);
            mBtQuery = (Button) findViewById(R.id.btn_query);
            mBtDelete = (Button) findViewById(R.id.btn_delete);
            mBtAdd.setOnClickListener(this);
            mBtUpdate.setOnClickListener(this);
            mBtQuery.setOnClickListener(this);
            mBtDelete.setOnClickListener(this);
        }
    
        @Override
        //点击事件
        public void onClick(View v) {
   
            switch(v.getId()) {
   
  
是的,Android Studio项目可以实现增删改查功能。你可以使用SQLite数据库来存储数据,并使用Java代码来实现增删改查操作。这里是一个简单的示例: 1. 创建一个SQLite数据库和表格来存储数据。 ``` public class DBHelper extends SQLiteOpenHelper { private static final String DATABASE_NAME = "mydatabase.db"; private static final int DATABASE_VERSION = 1; private static final String TABLE_NAME = "mytable"; private static final String COLUMN_ID = "id"; private static final String COLUMN_NAME = "name"; private static final String COLUMN_AGE = "age"; private static final String CREATE_TABLE = "CREATE TABLE " + TABLE_NAME + "(" + COLUMN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT," + COLUMN_NAME + " TEXT," + COLUMN_AGE + " INTEGER)"; public DBHelper(Context context) { super(context, DATABASE_NAME, null, DATABASE_VERSION); } @Override public void onCreate(SQLiteDatabase db) { db.execSQL(CREATE_TABLE); } @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME); onCreate(db); } } ``` 2. 实现增删改查操作。 ``` public class MyDAO { private SQLiteDatabase db; private DBHelper dbHelper; public MyDAO(Context context) { dbHelper = new DBHelper(context); db = dbHelper.getWritableDatabase(); } public void addData(String name, int age) { ContentValues values = new ContentValues(); values.put(DBHelper.COLUMN_NAME, name); values.put(DBHelper.COLUMN_AGE, age); db.insert(DBHelper.TABLE_NAME, null, values); } public void deleteData(int id) { db.delete(DBHelper.TABLE_NAME, DBHelper.COLUMN_ID + "=?", new String[]{String.valueOf(id)}); } public void updateData(int id, String name, int age) { ContentValues values = new ContentValues(); values.put(DBHelper.COLUMN_NAME, name); values.put(DBHelper.COLUMN_AGE, age); db.update(DBHelper.TABLE_NAME, values, DBHelper.COLUMN_ID + "=?", new String[]{String.valueOf(id)}); } public List<MyData> getAllData() { List<MyData> dataList = new ArrayList<>(); Cursor cursor = db.query(DBHelper.TABLE_NAME, null, null, null, null, null, null); while (cursor.moveToNext()) { int id = cursor.getInt(cursor.getColumnIndex(DBHelper.COLUMN_ID)); String name = cursor.getString(cursor.getColumnIndex(DBHelper.COLUMN_NAME)); int age = cursor.getInt(cursor.getColumnIndex(DBHelper.COLUMN_AGE)); dataList.add(new MyData(id, name, age)); } return dataList; } } ``` 其中,MyData是一个简单的Java类,用于存储从数据库中读取的数据。 这只是一个简单的示例,实际实现中可能需要更多的代码和复杂的逻辑。
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值