第九单元作业第二题

main代码

package com.example.app2;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {

    private Button buttonInsertOne;
    private Button buttonInsertTwo;
    private Button buttonDeleteOne;
    private Button buttonDeleteTwo;
    private Button buttonUpdateOne;
    private Button buttonUpdateTwo;
    private Button buttonQueryOne;
    private Button buttonQueryTwo;
    private  SQLiteDatabase db;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        initView();
    }

    private void initView() {
        buttonInsertOne = (Button) findViewById(R.id.button_insert_one);
        buttonInsertTwo = (Button) findViewById(R.id.button_insert_two);
        buttonDeleteOne = (Button) findViewById(R.id.button_delete_one);
        buttonDeleteTwo = (Button) findViewById(R.id.button_delete_two);
        buttonUpdateOne = (Button) findViewById(R.id.button_update_one);
        buttonUpdateTwo = (Button) findViewById(R.id.button_update_two);
        buttonQueryOne = (Button) findViewById(R.id.button_query_one);
        buttonQueryTwo = (Button) findViewById(R.id.button_query_two);

        MySqlHelp mySqlHelp=new MySqlHelp(this,"student.db",null,1);
         db = mySqlHelp.getReadableDatabase();

         
        buttonInsertOne.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ContentValues contentValues = new ContentValues();
                contentValues.put("name","小蘑菇");
                contentValues.put("sex","男");
                db.insert("student",null,contentValues);
            }
        });
        buttonInsertTwo.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String sql="insert into student values(null,?,?)";
                db.execSQL(sql,new Object[]{"小明","男"});
            }
        });

        buttonQueryOne.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String sql="select * from student";
                Cursor cursor = db.rawQuery(sql, null);
                if (cursor!=null){
                    while (cursor.moveToNext()){
                        String name = cursor.getString(cursor.getColumnIndex("name"));
                        String sex = cursor.getString(cursor.getColumnIndex("sex"));
                        int id = cursor.getInt(cursor.getColumnIndex("id"));
                        Toast.makeText(MainActivity.this, "id:"+id+"--姓名:"+name+"--性别:"+sex, Toast.LENGTH_SHORT).show();
                    }
                }
                cursor.close();
            }
        });
        buttonQueryTwo.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Cursor cursor = db.query("student", null, null, null, null, null, null);
                if (cursor!=null){
                    while (cursor.moveToNext()){
                        String name = cursor.getString(cursor.getColumnIndex("name"));
                        String sex = cursor.getString(cursor.getColumnIndex("sex"));
                        int id = cursor.getInt(cursor.getColumnIndex("id"));
                        Toast.makeText(MainActivity.this, "2-->id:"+id+"--姓名:"+name+"--性别:"+sex, Toast.LENGTH_SHORT).show();
                    }
                }
                cursor.close();
            }
        });
        buttonDeleteOne.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                db.delete("student","id=?",new String[]{"11"});
            }
        });
        buttonDeleteTwo.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String sql="delete from student where name=?";
                db.execSQL(sql,new Object[]{"蘑菇3"});
            }
        });
        buttonUpdateOne.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ContentValues contentValues = new ContentValues();
                contentValues.put("name","可爱的蘑菇");
                db.update("student",contentValues,"id=?",new String[]{"2"});
            }
        });
        buttonUpdateTwo.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String sql="update student set name=? where id=?";
                db.execSQL(sql,new Object[]{"蛋白",6});
            }
        });

    }
}

mySqlHelp

package com.example.app2;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

import androidx.annotation.Nullable;

public class MySqlHelp extends SQLiteOpenHelper {


    public MySqlHelp(@Nullable Context context, @Nullable String name, @Nullable SQLiteDatabase.CursorFactory factory, int version) {
        super(context, name, factory, version);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
           //添加表
        db.execSQL("create table student(id integer primary key autoincrement,name varcher(20),sex varcher(10))");
        db.beginTransaction();
        for (int i = 0; i < 10; i++) {
            String sql="insert into student values(null,?,?)";
            db.execSQL(sql,new String[]{"蘑菇"+i,"女"});
        }
        db.setTransactionSuccessful();
        db.endTransaction();
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值