adding data
Add the following code in the view layout
<Button
android:id="@+id/add_data"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="ADD data"
/>
Add the following code in MainActivity.java
Button addData=(Button) findViewById(R.id.add_data);
addData.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
SQLiteDatabase db=dbHelper.getWritableDatabase();
ContentValues values=new ContentValues();
values.put("name","The Da Vinci Code");
values.put("author","DanBrown");
values.put("pages",454);
values.put("price",16.96);
db.insert("Book",null,values);
values.clear();
}
});
Introduction to insert method:
db.insert("Book",null,values);
The first parameter is the name of the table, the second parameter is used to automatically assign a value to some nullable columns without adding data when adding data is not specified, and the third is an ContentValues ;
object.
operation result
update data
Add the following code in the view layout
<Button
android:id="@+id/update_data"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="update_data"
/>
Add the following code in MainActivity.java
Button updateData=(Button) findViewById(R.id.update_data);
updateData.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
SQLiteDatabase db=dbHelper.getWritableDatabase();
ContentValues values=new ContentValues();
values.put("price",10.99);
db.update("Book",values,"name=?",new String[]{
"The Da Vinci Code"});
values.clear();
}
});
Introduction to update method:
db.update("Book",values,"name=?",new String[]{
"The Da Vinci Code"});
The first parameter is the table name, the second is the ContentValues ;
object, and the third and fourth parameters specify which row to update to!
operation result
delete data
Add the following code in the view layout
<Button
android:id="@+id/delete_data"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="delete_data"
/>
Add the following code in MainActivity.java
Button deleteData=(Button) findViewById(R.id.delete_data);
deleteData.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
SQLiteDatabase db=dbHelper.getWritableDatabase();
db.delete("Book","pqge>?",new String[]{
"400"});
}
});
operation result
Query data
yyouuyu We have deleted a piece of data in the Book, we first click Add, and then query
and add the following code in the view layout
<Button
android:id="@+id/query_data"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="query_data"
/>
Add the following code in MainActivity.java
Button queryButton=(Button) findViewById(R.id.query_data);
queryButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
SQLiteDatabase db=dbHelper.getWritableDatabase();
//查询表中所有数据
Cursor cursor=db.query("Book",null,null,null,null,null,null);
if(cursor.moveToFirst()){
Log.d("MainActivity","Book name is"+cursor.getString(cursor.getColumnIndex("name")));
Log.d("MainActivity","Book author is"+cursor.getString(cursor.getColumnIndex("author")));
Log.d("MainActivity","Book pages is"+cursor.getInt(cursor.getColumnIndex("pages")));
Log.d("MainActivity","Book price is"+cursor.getDouble(cursor.getColumnIndex("price")));
}
}
});
operation result