安卓开发入门基础之基于sqlite开发的小型车辆信息登记系统
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了安卓开发入门基础之基于sqlite开发的小型车辆信息登记系统,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含4130字,纯文字阅读大概需要6分钟。
内容图文
![安卓开发入门基础之基于sqlite开发的小型车辆信息登记系统](/upload/InfoBanner/zyjiaocheng/874/c126c05371ff4e80b391c7f83abe5f53.jpg)
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运行图
- 新增车辆信息
- 添加完查询添加结果,确认数据存入数据库了
- 更改车主姓名
- 新增车辆信息,以便于删除
- 删除成功
内容总结
以上是互联网集市为您收集整理的安卓开发入门基础之基于sqlite开发的小型车辆信息登记系统全部内容,希望文章能够帮你解决安卓开发入门基础之基于sqlite开发的小型车辆信息登记系统所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。