Android SQLlite添加数据: 创建、添加、更新、删除、查询 .db文件

Android系统文件目录:

 

 

 

 

第一步

public class MySQLiteHelper extends SQLiteOpenHelper {
    /***
    *
    * integet: 整型
      real : 浮点型
      blob : 二进制
      text: 文本类型
      primary key 将id 列设为主键,并用autoincrement关键字表示id列是自增长的。
    *
    * ***/

    private String createSQL = "create table student(" +
            "id integer primary key  autoincrement not null ," +
            "age integer ," +
            "name text not null ," +
            "score real )";
    private Context context;


    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL(createSQL);
        Toast.makeText(context,"创建数据库成功",Toast.LENGTH_LONG).show();

    }

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

    }

    public MySQLiteHelper(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) {
        super(context, name, factory, version);
        this.context = context;
    }

}

第二步

@BindView(R.id.new_bu)
Button newBu;
@BindView(R.id.name_et)
EditText nameEt;
@BindView(R.id.et_bu)
Button etBu;
@BindView(R.id.inquire_bu)
Button inquireBu;
@BindView(R.id.modify_bu)
Button modifyBu;
@BindView(R.id.delete_bu)
Button deleteBu;
@BindView(R.id.text_tv)
TextView textTv;
@BindView(R.id.age_et)
EditText ageEt;
public SQLiteDatabase sqldb;
public static MySQLiteHelper myHelper;
@BindView(R.id.modify_et)
EditText modifyEt;

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.sqllite_layou);
    ButterKnife.bind(this);

    myHelper = new MySQLiteHelper(this, "student.db", null, 2);
}

@OnClick({R.id.new_bu, R.id.et_bu, R.id.inquire_bu, R.id.modify_bu, R.id.delete_bu})
public void onViewClicked(View view) {
    switch (view.getId()) {
        case R.id.new_bu://创建数据库,.db文件
            myHelper.getWritableDatabase();
            break;
        case R.id.et_bu://添加数据
            //获得SQLiteDatabase对象,读写模式
            sqldb = myHelper.getWritableDatabase();
            //ContentValues类似HashMap,区别是ContentValues只能存简单数据类型,不能存对象
            ContentValues values = new ContentValues();
            values.put("age", ageEt.getText().toString());
            values.put("name", nameEt.getText().toString());
            sqldb.insert("student", null, values);
            break;
        case R.id.inquire_bu://查找数据
            SQLiteDatabase database = myHelper.getReadableDatabase();
            Cursor cursor = database.query("student", null, null, null, null, null, null);
            if (cursor.moveToFirst()) {
                //遍历
                do {
                    String name = cursor.getString(cursor.getColumnIndex("name"));
                    textTv.setText(name);
                    Log.i("log_","name="+name);
                } while (cursor.moveToNext());
            }
            break;
        case R.id.modify_bu://更改数据
            String name = nameEt.getText().toString();
            String name_modify = modifyEt.getText().toString();
            SQLiteDatabase sqLite_modify = myHelper.getWritableDatabase();
            ContentValues contentValues = new ContentValues();
            contentValues.put("name",name_modify);
            sqLite_modify.update("student",contentValues,"name=? and id>?",new String[]{name,"6"});

            break;
        case R.id.delete_bu://删除
            SQLiteDatabase sqLite_delete = myHelper.getWritableDatabase();
            String name_delete = nameEt.getText().toString();
            sqLite_delete.delete("student","name=?",new String[]{name_delete});
            break;
    }
}

xml布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">

    <Button
        android:id="@+id/new_bu"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="创建"/>
    <EditText
        android:id="@+id/age_et"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="年龄"/>
    <EditText
        android:id="@+id/name_et"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="名字"/>
    <EditText
        android:id="@+id/modify_et"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="修改"/>
    <Button
        android:id="@+id/et_bu"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="添加"/>
    <Button
        android:id="@+id/inquire_bu"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="查询"/>
    <TextView
        android:id="@+id/text_tv"
        android:layout_width="match_parent"
        android:layout_height="55dp" />

    <Button
        android:id="@+id/modify_bu"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="修改"/>
    <Button
        android:id="@+id/delete_bu"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="删除"/>
</LinearLayout>

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值