布局
Activity
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
protected Button mInsertBtn;
protected Button mDeleteBtn;
protected Button mUpdateBtn;
protected Button mQueryBtn;
private DBHelper mHelper;
private SQLiteDatabase mDatabase;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
super.setContentView(R.layout.activity_main);
initView();
mHelper = new DBHelper(this);
mDatabase = mHelper.getWritableDatabase();
}
private void initView() {
mInsertBtn = (Button) findViewById(R.id.insert_btn);
mInsertBtn.setOnClickListener(MainActivity.this);
mDeleteBtn = (Button) findViewById(R.id.delete_btn);
mDeleteBtn.setOnClickListener(MainActivity.this);
mUpdateBtn = (Button) findViewById(R.id.update_btn);
mUpdateBtn.setOnClickListener(MainActivity.this);
mQueryBtn = (Button) findViewById(R.id.query_btn);
mQueryBtn.setOnClickListener(MainActivity.this);
}
@Override
public void onClick(View view) {
if (view.getId() == R.id.insert_btn) {
insertData();
} else if (view.getId() == R.id.delete_btn) {
deleteData();
} else if (view.getId() == R.id.update_btn) {
updateData();
} else if (view.getId() == R.id.query_btn) {
queryData();
}
}
// 表名
// null。數(shù)據(jù)庫如果插入的數(shù)據(jù)為null,會引起數(shù)據(jù)庫不穩(wěn)定。為了防止崩潰珊膜,需要傳入第二個參數(shù)要求的對象
// 如果插入的數(shù)據(jù)不為null醋虏,沒有必要傳入第二個參數(shù)避免崩潰飘蚯,所以為null
// 插入的數(shù)據(jù)
private void insertData() {
ContentValues values = new ContentValues();
values.put(DBHelper.NAME, "鹿晗");
values.put(DBHelper.AGE, 17);
mDatabase.insert(DBHelper.TABLE_NAME, null, values);
Toast.makeText(this, "插入成功", Toast.LENGTH_SHORT).show();
}
// 表名
// 刪除條件
// 滿足刪除的值
private void deleteData() {
int count = mDatabase.delete(DBHelper.TABLE_NAME, DBHelper.NAME + " = ?", new String[]{"鹿晗"});
Toast.makeText(this, "刪除數(shù)量:"+count, Toast.LENGTH_SHORT).show();
}
// 表名
// 修改后的數(shù)據(jù)
// 修改條件
// 滿足修改的值
private void updateData() {
ContentValues values = new ContentValues();
values.put(DBHelper.NAME, "小茗同學");
values.put(DBHelper.AGE, 18);
int count = mDatabase
.update(DBHelper.TABLE_NAME, values, DBHelper.NAME + " = ?", new String[]{"鹿晗"});
Toast.makeText(this, "修改成功:" + count, Toast.LENGTH_SHORT).show();
}
// 表名
// 查詢字段
// 查詢條件
// 滿足查詢的值
// 分組
// 分組篩選關(guān)鍵字
// 排序
private void queryData() {
Cursor cursor = mDatabase.query(DBHelper.TABLE_NAME,
new String[]{DBHelper.NAME, DBHelper.AGE},
DBHelper.AGE + " > ?",
new String[]{"16"},
null,
null,
DBHelper.AGE + " desc");// 注意空格睁本!
int nameIndex = cursor.getColumnIndex(DBHelper.NAME);
int ageIndex = cursor.getColumnIndex(DBHelper.AGE);
while (cursor.moveToNext()) {
String name = cursor.getString(nameIndex);
String age = cursor.getString(ageIndex);
Log.d("1507", "name: " + name + ", age: " + age);
}
}
}
創(chuàng)建數(shù)據(jù)庫
public class DBHelper extends SQLiteOpenHelper {
// 數(shù)據(jù)庫文件名
public static final String DB_NAME = "my_database.db";
// 數(shù)據(jù)庫表名
public static final String TABLE_NAME = "t_person";
// 數(shù)據(jù)庫版本號
public static final int DB_VERSION = 1;
public static final String NAME = "name";
public static final String AGE = "age";
public DBHelper(Context context) {
super(context, DB_NAME, null, DB_VERSION);
}
// 當數(shù)據(jù)庫文件創(chuàng)建時檀葛,執(zhí)行初始化操作,并且只執(zhí)行一次
@Override
public void onCreate(SQLiteDatabase db) {
// 建表
String sql =
"create table " +TABLE_NAME +
"(_id integer primary key autoincrement, " +NAME + " varchar, " +AGE + " varchar"+ ")";
db.execSQL(sql);
}
// 當數(shù)據(jù)庫版本更新執(zhí)行該方法
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
}