xml布局构思
- 总体是垂直线性布局
- 里面放6个TextView,5个EditText,5个Button按钮
界面设计效果图
Mydatabase.java
- 首先要创建数据库并且将数据写入数据库
public class Mydatabase extends SQLiteOpenHelper {
static String name="hello.db";
static int version=1;
public Mydatabase(@Nullable Context context) {
super(context, name,null, version);
}
@Override
public void onCreate(SQLiteDatabase sqLiteDatabase) {
String sql = "create table user(车牌号 Integer,车主姓名 varchar(10),车主年龄 Integer)";
sqLiteDatabase.execSQL(sql);
}
@Override
public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {
}
}
MainActivity.java
- List item
public class MainActivity extends AppCompatActivity {
private EditText bianhao;
private EditText name;
private EditText age;
private EditText serch_number;
private EditText serch_name;
private TextView result;
private SQLiteDatabase database;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
bianhao = findViewById(R.id.edit_bianhao);
name = findViewById(R.id.edit_name);
age = findViewById(R.id.edit_age);
serch_number = findViewById(R.id.edit__serch_number);
serch_name = findViewById(R.id.edit_serch_name);
result = findViewById(R.id.result);
Mydatabase mydatabase = new Mydatabase(MainActivity.this);
database = mydatabase.getWritableDatabase();
}
public void insert(View view) {
try {
String sql = "select * from user where 车牌号 =?";
//游标
Cursor cursor = database.rawQuery(sql, new String[]{bianhao.getText().toString()});
if (cursor.getCount() == 0) {
ContentValues contentValues = new ContentValues();
contentValues.put("车牌号", Integer.parseInt(bianhao.getText().toString()));
contentValues.put("车主姓名", name.getText().toString());
contentValues.put("车主年龄", Integer.parseInt(age.getText().toString()));
database.insert("user", null, contentValues);//插入数据库的user表
} else {
Toast.makeText(getApplicationContext(), "数据已经存在,请重新输入编号", Toast.LENGTH_SHORT).show();
//插入同一个数据时弹出提示
}
} catch (Exception ignore) {
Toast.makeText(getApplicationContext(), "请正确输入后再进行添加!", Toast.LENGTH_SHORT).show();
//输入为空,且点击了添加按钮时报提示
}
}
public void delete(View view) {
try {
String sql = "delete from user where 车牌号=?";
database.execSQL(sql, new Object[]{Integer.parseInt(bianhao.getText().toString())});
Toast.makeText(getApplicationContext(), "数据删除成功!", Toast.LENGTH_SHORT).show();
} catch (Exception ignore) {
Toast.makeText(getApplicationContext(), "请输入后再进行删除!", Toast.LENGTH_SHORT).show();
//输入为空,且点击删除按钮后弹出提示
}
}
public void update(View view) {
try {
String sql = "update user set 车主姓名=?,车主年龄=? where 车牌号=?";
database.execSQL(sql, new Object[]{name.getText().toString(), Integer.parseInt(age.getText().toString()), Integer.parseInt(bianhao.getText().toString())});
Toast.makeText(getApplicationContext(), "数据修改成功!", Toast.LENGTH_SHORT).show();
} catch (Exception ignore) {
Toast.makeText(getApplicationContext(), "请输入后再进行修改!", Toast.LENGTH_SHORT).show();
//输入为空,且点击修改按钮时报提示
}
}
public void findID1(View view) {
result.setText("");
String sql = "select * from user where 车牌号=?";
Cursor cursor = database.rawQuery(sql, new String[]{serch_number.getText().toString()});
while (cursor.moveToNext()) {
String xm = cursor.getString(cursor.getColumnIndex("车主姓名"));
int bh = cursor.getInt(cursor.getColumnIndex("车牌号"));
int nl = cursor.getInt(cursor.getColumnIndex("车主年龄"));
result.append(" 查找结果:"+ "\n车牌号" + bh + "\n车主姓名:" + xm + "\n车主年龄" + nl);
}
}
public void findID2(View view) {
result.setText("");
String sql = "select * from user where 车主姓名=?";
Cursor cursor = database.rawQuery(sql, new String[]{serch_name.getText().toString()});
while (cursor.moveToNext()) {
String xm = cursor.getString(cursor.getColumnIndex("车主姓名"));
int bh = cursor.getInt(cursor.getColumnIndex("车牌号"));
int nl = cursor.getInt(cursor.getColumnIndex("车主年龄"));
result.append(" 查找结果:"+ "\n车牌号" + bh + "\n车主姓名:" + xm + "\n车主年龄" + nl);
}
}
}
APP运行图
- 新增车辆信息
- 添加完查询添加结果,确认数据存入数据库了
- 更改车主姓名
- 新增车辆信息,以便于删除
- 删除成功