标签:Toast sqlite String 安卓 车主 cursor 开发 sql name
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运行图
- 新增车辆信息
- 添加完查询添加结果,确认数据存入数据库了
- 更改车主姓名
- 新增车辆信息,以便于删除
- 删除成功
标签:Toast,sqlite,String,安卓,车主,cursor,开发,sql,name 来源: https://blog.csdn.net/weixin_45126177/article/details/105903103
本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享; 2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关; 3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关; 4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除; 5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。