2020.8.27 Android学习案例
1.创建数据库 MyHelper.class
class MyHelper extends SQLiteOpenHelper {
public MyHelper(@Nullable Context context) {
super(context, "dnf", null, 1);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("CREATE TABLE rin(_id INTEGER PRIMARY KEY AUTOINCREMENT,name VARCHAR(20),tel VARCHAR(20) )");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
//当版本号更新,删除现有表,调用onCreate()方法重新创建一个表
db.execSQL("drop table rin");
onCreate(db);
}
}
2.主界面布局
<?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:padding="10dp"
android:orientation="vertical"
android:background="#06D6F1"
tools:context=".MainActivity">
<LinearLayout
android:layout_marginTop="130dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="姓名:"
android:textColor="#000"
android:textSize="25sp"/>
<EditText
android:id="@+id/etName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="25sp"
android:hint="请输入姓名。"/>
</LinearLayout>
<LinearLayout
android:layout_marginTop="10dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="密码:"
android:textColor="#000"
android:textSize="25sp"/>
<EditText
android:id="@+id/etPassword"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="25sp"
android:hint="请输入密码。"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
android:id="@+id/btnAdd"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#FF1100"
android:text="添加"/>
<Button
android:id="@+id/btnDel"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#DA00FF"
android:text="删除"/>
<Button
android:id="@+id/btnUp"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#FF3D00"
android:text="修改"/>
<Button
android:id="@+id/btnQuery"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#00FFE7"
android:text="查询"/>
</LinearLayout>
<TextView
android:id="@+id/tvContent"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="25dp"
android:textSize="25sp"/>
</LinearLayout>
3.主界面逻辑代码 MainActivity.java
/**
* 2020.8.27 Android sql练习
*/
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private EditText etName,etPassword;
private Button btAdd,btUp,btnDel,btnQuery;
private TextView tvContent;
MyHelper myHelper;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myHelper=new MyHelper(this);
init();
}
private void init() {
tvContent=findViewById(R.id.tvContent);
etName=findViewById(R.id.etName);
etPassword=findViewById(R.id.etPassword);
btAdd=findViewById(R.id.btnAdd);
btnDel=findViewById(R.id.btnDel);
btUp=findViewById(R.id.btnUp);
btnQuery=findViewById(R.id.btnQuery);
btAdd.setOnClickListener(this);
btnDel.setOnClickListener(this);
btUp.setOnClickListener(this);
btnQuery.setOnClickListener(this);
}
@Override
public void onClick(View v) {
String name,tel;
ContentValues values;
SQLiteDatabase db;
switch (v.getId()){
case R.id.btnAdd:
name=etName.getText().toString();//获取输入的姓名
tel=etPassword.getText().toString();//获取输入的手机号码
db=myHelper.getWritableDatabase();//通过getWritableDatabase()方法得到db对象
values=new ContentValues();//获得ContentValues对象
//这里的"name"和"tel"一定要和表里的名字一样
values.put("name",name);
values.put("tel",tel);
db.insert("rin",null,values);
Toast.makeText(this,"信息已添加",Toast.LENGTH_SHORT).show();
db.close();
break;
case R.id.btnQuery:
db=myHelper.getReadableDatabase();
Cursor cursor=db.query("rin",null,null,null,null
,null,null);
if(cursor.getCount()==0){
tvContent.setText("");
Toast.makeText(this,"数据库为空",Toast.LENGTH_SHORT).show();
}else {
cursor.moveToNext();//游标移动至下一个行信息
tvContent.setText("姓名:"+cursor.getString(1)+" "+"电话:"+cursor.getString(2));
}
while (cursor.moveToNext()){
tvContent.append("\n"+"姓名:"+cursor.getString(1)+" "+"电话:"+cursor.getString(2));
}
cursor.close();
db.close();
break;
}
}
}