绿豆通讯录
activity_tongxunlu.xml
android:layout_width="match_parent" android:layout_height="match_parent" android:background="#BBE0F2" android:padding="16dp" android:orientation="vertical"> android:layout_marginTop="130dp" android:layout_width="match_parent" android:layout_height="wrap_content"> android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="姓 名:" android:textSize="18sp" android:textColor="@color/black" android:textStyle="bold"/> android:id="@+id/et_name" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="请输入姓名" android:textSize="16sp" /> android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginBottom="10dp"> android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="电 话:" android:textSize="18sp" android:textColor="@color/black" android:textStyle="bold"/> android:id="@+id/et_phone" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="请输入手机号码" android:textSize="16sp"/> android:layout_width="match_parent" android:layout_height="wrap_content"> android:id="@+id/tv_show" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="25dp" android:textSize="20sp" />
java_tongxunlu.java
package com.example.chapter5.tongxunlu;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import com.example.chapter5.R;
public class java_tongxunlu extends AppCompatActivity implements
View.OnClickListener {
MyHelper myHelper;
private EditText mEtName;
private EditText mEtPhone;
private TextView mTvShow;
private Button mBtnAdd, mBtnQuery, mBtnUpdate, mBtnDelete;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_tongxunlu);
//创建一个MyHelper类的对象
myHelper = new MyHelper(this);
init();
}
private void init() {
mEtName = (EditText) findViewById(R.id.et_name);
mEtPhone = (EditText) findViewById(R.id.et_phone);
mTvShow = (TextView) findViewById(R.id.tv_show);
mBtnAdd = (Button) findViewById(R.id.btn_add);
mBtnQuery = (Button) findViewById(R.id.btn_query);
mBtnUpdate = (Button) findViewById(R.id.btn_update);
mBtnDelete = (Button) findViewById(R.id.btn_delete);
mBtnAdd.setOnClickListener(this);
mBtnQuery.setOnClickListener(this);
mBtnUpdate.setOnClickListener(this);
mBtnDelete.setOnClickListener(this);
}
/**
* 添加、查询、修改、删除 四个按钮的点击事件
*
* @param view
*/
@Override
public void onClick(View view) {
String name, phone;
SQLiteDatabase db;
ContentValues values;
switch (view.getId()) {
case R.id.btn_add: //添加数据
//通过getText()方法获取界面上输入的联系人的姓名和电话
name = mEtName.getText().toString();
phone = mEtPhone.getText().toString();
//获取可读写SQLitDatabase对象
db = myHelper.getWritableDatabase();
//创建ContentValues对象
values = new ContentValues();
//将数据添加到ContentValues对象
values.put("name", name);
values.put("phone", phone);
db.insert("information", null, values);
Toast.makeText(this, "信息已添加", Toast.LENGTH_SHORT).show();
db.close();
break;
case R.id.btn_query: //查询数据
//获取数据库对象db
db = myHelper.getReadableDatabase();
//通过数据库对象中的query()方法查询
Cursor cursor = db.query("information", null, null, null,
null, null, null);
//判断数据的数量是否为0,为0表示没有查询到数据
if (cursor.getCount() == 0) {
mTvShow.setText("");
Toast.makeText(this, "没有数据", Toast.LENGTH_SHORT).show();
} else {
//moveToFirst()将光标指向查询结果的第一个位置
cursor.moveToFirst();
mTvShow.setText("Name: " + cursor.getString(1) +
"; Tel: " + cursor.getString(2));
//调用while循环,遍历后续的联系人信息
while (cursor.moveToNext()) {
mTvShow.append("\n" + "Name: " + cursor.getString(1) +
"; Tel: " + cursor.getString(2));
}
}
cursor.close();
db.close();
break;
case R.id.btn_update: //修改数据
//获取数据库对象db
db = myHelper.getWritableDatabase();
//创建ContentValues对象values
values = new ContentValues(); //要修改的数据
//获取输入的电话号码(即要修改的信息),添加到values对象中
values.put("phone", phone = mEtPhone.getText().toString());
//更新联系人的电话号码,联系人的姓名为界面上R.id.et_name输入框中输入的姓名
db.update("information", values, "name=?",
new String[]{mEtName.getText().toString()});
Toast.makeText(this, "信息已修改", Toast.LENGTH_SHORT).show();
db.close();
break;
case R.id.btn_delete: //删除数据
//获取数据库对象db
db = myHelper.getWritableDatabase();
//删除information这个表中的信息
db.delete("information", null, null);
Toast.makeText(this, "信息已删除", Toast.LENGTH_SHORT).show();
//界面上R.id.tv_show显示一个空字符串
mTvShow.setText("");
db.close();
break;
}
}
// MyHelper类
class MyHelper extends SQLiteOpenHelper {
// 数据库名itcast.db
public MyHelper(Context context) {
super(context, "itcast.db", null, 1);
}
// 在数据库中创建了一个名为information的表
@Override
public void onCreate(SQLiteDatabase sqLiteDatabase) {
sqLiteDatabase.execSQL("CREATE TABLE information(_id INTEGER PRIMARY KEY AUTOINCREMENT, name VARCHAR(20), phone VARCHAR(20))");
}
@Override
public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {
}
}
}
1. SQLite数据库的创建
2. SQLite数据库的基本操作
3. 数据库中的事务
张三取1000元,王五存1000元
推荐文章
发表评论