<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: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.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(); 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(); 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(); 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(); Cursor cusor = null; String text =""; cusor = db.query(DbDataBase.TABLE_NAME, null, null, null, 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; } } 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.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); 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); mTv.setText(mContactDao.Select(name)); } @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; } } }