Android SQLlite添加数据: 创建、添加、更新、删除、查询 .db文件
第一步
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>