第九次作业

所需截图:

 

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:background="@drawable/bg"
android:orientation="vertical"
tools:context="com.example.mysql3.MainActivity" >

<LinearLayout
android:layout_marginTop="100dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="用 户"/>
<EditText
android:id="@+id/name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="请输入用户姓名"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="电 话"/>
<EditText
android:id="@+id/phone"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="请输入手机号码"/>
</LinearLayout>
<LinearLayout
android:layout_marginTop="10dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:id="@+id/btn_add"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="增加"/>
<Button
android:id="@+id/btn_updata"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="修改"/>
<Button
android:id="@+id/btn_query"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="查询"/>
<Button
android:id="@+id/btn_delete"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="删除"/>
</LinearLayout>
<TextView
android:id="@+id/tv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>

</LinearLayout>

 

package com.example.mysql3;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;


public class MainActivity extends Activity implements View.OnClickListener {

private ContactDao mContactDao;
private Button mBtnadd;
private Button mBtnupdata;
private Button mBtnquery;
private Button mBtndelete;
private EditText mName;
private EditText mPhone;
private TextView mTv;
private String name="";
private String phone="";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mContactDao = new ContactDao(this);
init();
mTv.setText(mContactDao.Select(name));

}
private void init() {
mName = (EditText) findViewById(R.id.name);
mPhone = (EditText) findViewById(R.id.phone);
mBtnadd =(Button) findViewById(R.id.btn_add);
mTv = (TextView) findViewById(R.id.tv);
mBtnupdata =(Button) findViewById(R.id.btn_updata);
mBtnquery =(Button) findViewById(R.id.btn_query);
mBtndelete =(Button) findViewById(R.id.btn_delete);
mBtnadd.setOnClickListener(this);
mBtnupdata.setOnClickListener(this);
mBtnquery.setOnClickListener(this);
mBtndelete.setOnClickListener(this);
}
@Override
public void onClick(View v) {
name = mName.getText().toString();
phone = mPhone.getText().toString();
switch (v.getId()) {
case R.id.btn_add:
if(mContactDao.Insert(name, phone)){
Toast.makeText(this, "数据添加成功", 0).show();
mTv.setText(mContactDao.Select(name));
}else {
Toast.makeText(this, "数据添加失败", 0).show();
}

break;
case R.id.btn_query:
mTv.setText(mContactDao.Select(name));
break;
case R.id.btn_delete:
if(mContactDao.Delete(name)) {
Toast.makeText(this, "数据删除成功", 0).show();
mTv.setText(mContactDao.Select(name));
}else {
Toast.makeText(this, "数据删除失败", 0).show();
}
break;
case R.id.btn_updata:
if(mContactDao.Updata(name, phone)) {
Toast.makeText(this, "数据修改成功", 0).show();
mTv.setText(mContactDao.Select(name));
}else {
Toast.makeText(this, "数据修改失败", 0).show();
}
break;

}
}
}

 

package com.example.mysql3;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;
import android.provider.BaseColumns;

public class DbDataBase extends SQLiteOpenHelper implements BaseColumns{

static String DB_NAME = "itcase.db";
static int DB_VERSION = 1;
static String TABLE_NAME = "mysql";
static String _NAME = "name";
static String _PHONE= "phone";

public DbDataBase(Context context) {
super(context, DB_NAME, null, DB_VERSION);
// TODO Auto-generated constructor stub
}

@Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
db.execSQL("create table "+TABLE_NAME+"("+_ID+" integer primary key autoincrement,"+_NAME+" text ,"+_PHONE+" text)");

}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub

}

}

 

package com.example.mysql3;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

public class ContactDao {

private static DbDataBase mDbDataBase;

public ContactDao(Context c){
mDbDataBase = new DbDataBase(c);
}
public static boolean Insert(String name ,String phone) {
SQLiteDatabase db = mDbDataBase.getWritableDatabase();
// db.insert(table, nullColumnHack, values) 三个参数 分别是表名 返回的是一个 插入字段的id 但插入错误的时候 返回的时候-1
// nullColumnHack 比如 放了 username 那么usename 就不得为空 如果为空 那么就有字段可以为空
// valuse 是一个 ContentValues values = new ContentValues(); 一个对象 向里面填要填加的数据
ContentValues values = new ContentValues();
values.put(DbDataBase._NAME, name);
values.put(DbDataBase._PHONE, phone);
long InsertId = db.insert(DbDataBase.TABLE_NAME, null, values);
return InsertId!=-1;
}
public static boolean Updata(String name,String phone) {
SQLiteDatabase db = mDbDataBase.getWritableDatabase();
// db.update(table, values, whereClause, whereArgs); 四个参数 前两个同上 返回的是修改的那条数据的id 要是修改出错是 id=0
// 后两个whereClause 是sql语句中的where 语句 whereArgs是 where语句绑定值 where语句绑定值 new String[]{username}
ContentValues values = new ContentValues();
values.put(DbDataBase._PHONE, phone);
int Uqdata =db.update(DbDataBase.TABLE_NAME, values, DbDataBase._NAME+"=?", new String[]{name});
return Uqdata>0;
}
public static boolean Delete(String name) {
SQLiteDatabase db = mDbDataBase.getWritableDatabase();
// db.delete(table, whereClause, whereArgs) 三个参数 同上; 返回的是删除的那个id 要是删除出错 返回 0
int DeleteID =db.delete(DbDataBase.TABLE_NAME, DbDataBase._NAME+"=?", new String[]{name});
return DeleteID>0;
}
public static String Select (String name) {
SQLiteDatabase db=mDbDataBase.getReadableDatabase();
// db.query(table, columns, selection, selectionArgs, null, null, null); 后三参数个为空
// 第一个是表名 第二个是 返回的数据 第三个 查询条件 第四个是查询条件的值
Cursor cusor = null;
String text ="";

cusor = db.query(DbDataBase.TABLE_NAME, null, null, null, null, null, null);

// cusor = db.query(DbDataBase.TABLE_NAME, new String[] {DbDataBase._NAME,DbDataBase._PHONE},
// DbDataBase._NAME+"=?",new String[]{name}, null, null, null);

while (cusor.moveToNext()){
int nameIntdex = cusor.getColumnIndex(DbDataBase._NAME);
text+=("name: "+cusor.getString(nameIntdex)+"\n");
int phoneIndex = cusor.getColumnIndex(DbDataBase._PHONE);
text+=("phone: "+cusor.getString(phoneIndex)+"\n");
}
return text;


}
}

 

posted @ 2019-12-05 15:20  TIpengjie  阅读(64)  评论(0编辑  收藏  举报