效果图:
增:
更新【改】:
删:
MainActivity.java:
public class MainActivity extends AppCompatActivity {
private MyDatabaseHelper dbHelper;
private TextView textView;
private String info=""; //结果
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView = findViewById(R.id.tv_show);
dbHelper = new MyDatabaseHelper(this,"BookStore.db",null,3);
Button createDatabase = findViewById(R.id.btn_create_database);
createDatabase.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dbHelper.getWritableDatabase();
}
});
Button addData = (Button) findViewById(R.id.btn_add_data);
/**
* 增加数据
*/
addData.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
SQLiteDatabase db = dbHelper.getWritableDatabase();
ContentValues values = new ContentValues();
// 开始组装第一条数据
values.put("name", "蜡笔小新");
values.put("pages", 630);
values.put("price", 16.16);
db.insert("Book",null, values); // 插入第一条数据
values.clear();
// 开始组装第二条数据
values.put("name", "哆啦A梦");
values.put("pages", 490);
values.put("price", 19.95);
db.insert("Book",null, values); // 插入第二条数据
}
});
/**
* 更新数据
*/
Button updateData = findViewById(R.id.btn_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);
/**
* 第一个参数是:表名
* 第二个参数是:名字等于谁?
* 第三个参数是:哆啦A梦
*/
db.update("Book", values,"name = ?",new String[]{ "哆啦A梦" });
}
});
/**
* 删除数据
*/
Button deleteButton = findViewById(R.id.btn_dalete_data);
deleteButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
SQLiteDatabase db = dbHelper.getWritableDatabase();
/**
* 第一个参数是:表名
* 第二个参数是:页数大于几
* 第三个参数时:就是这个几的个数
*/
db.delete("Book","pages > ?", new String[]{ "500" });
}
});
/**
* 查询数据
*/
Button queryButton = findViewById(R.id.btn_query_data);
queryButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
textView.setText("");
SQLiteDatabase db = dbHelper.getWritableDatabase();
Cursor cursor = db.query("Book",null,null,null,null,null,null);
if (cursor.moveToNext()){
do {
// 遍历 Curson 对象,取出数据并打印
String name = cursor.getString(cursor.getColumnIndex("name"));
int pages = cursor.getInt(cursor.getColumnIndex("pages"));
double price = cursor.getDouble(cursor.getColumnIndex("price"));
info += "姓名为: " + name + ", 页数为: "+ pages + ", 价格为: " + price + "\n\n";
}while (cursor.moveToNext());
}
cursor.close();
textView.setText(info);
}
});
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<Button
android:id="@+id/btn_create_database"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="创建数据库"/>
<Button
android:id="@+id/btn_add_data"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="添加数据"/>
<Button
android:id="@+id/btn_update_data"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="更新数据"/>
<Button
android:id="@+id/btn_dalete_data"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="删除数据"/>
<Button
android:id="@+id/btn_query_data"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="查看数据"/>
<TextView
android:id="@+id/tv_show"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"/>
</LinearLayout>
教你们一个小技巧记住建表语句:
CREATE TABLE userq(_id INTEGER PRIMARY KEY AUTOINCREMENT
CREATE — 创建
TABLE — 表
userq — 表名
INTEGER — 整数 小写为:Integer
PRIMARY — 主 小写为:primary
KEY — 键 小写为: key
AUTOINCREMENT — 自动增量
点击运行即可!
最后,我真诚的希望能评论一句嘛,让我知道你来过,我会很开心的