安卓中数据库增删改查的方法


第一步:在xml布局中编辑

<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:orientation="vertical"

    tools:context=".MainActivity" >

   <EditText 

       android:id="@+id/et_name"

       android:layout_width="match_parent"

       android:layout_height="wrap_content"

       android:hint="请输入联系人的姓名"/>

   <EditText 

       android:layout_height="wrap_content"

       android:layout_width="match_parent"

       android:id="@+id/et_phone"

       android:hint="请输入联系人的电话"

       android:inputType="phone"/>

   <Button 

       android:layout_width="fill_parent"

       android:layout_height="wrap_content"

       android:text="添加"

      android:onClick="add"/>

   <Button 

       android:layout_width="match_parent"

       android:layout_height="wrap_content"

       android:text="删除"

       android:onClick="delete"/>

   <Button 

       android:layout_width="fill_parent"

       android:layout_height="wrap_content"

       android:text="修改"

       android:onClick="update"/>

   <Button 

       android:layout_width="fill_parent"

       android:layout_height="wrap_content"

       android:text="查询"

       android:onClick="query"/>

</LinearLayout>

第二步:建立一个数据库的帮助类

/**

 * 数据库创建的帮助类 类似File

 * @author Administrator

 *

 */

public class MyDBOpenHelper extends SQLiteOpenHelper {

/**

 * @param context 上下文

 * @param version

 */

public MyDBOpenHelper(Context context) {

//第二个参数数据库的名称

//第三个参数null代表的是默认的游标工厂

//第四个参数 是数据库的版本号  数据库只能升级,不能降级,版本号只能变大不能变小

super(context, "saiermeng.db", null, 2);

}

//Called when the database is created for the first time.

//当数据库第一次被创建的时候调用的方法,适合在这个方法里面把数据库的表结构定义出来.

@Override

public void onCreate(SQLiteDatabase db) {

System.out.println("oncreate 数据库被创建了. 哈哈哈哈,嘎嘎嘎------------");

//执行sql语句

db.execSQL("create table contactinfo (id integer primary key autoincrement, name varchar(20), phone varchar(20))");

}

//Called when the database needs to be upgraded.

//当数据库更新的时候调用的方法

@Override

public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

System.out.println("onUpgrade 数据库被更新了. oldVersion:"+oldVersion+"--------newVersion:"+newVersion+"----");

db.execSQL("alter table contactinfo add account varchar(20)");

}

}

第三步:另建一个包,创建一个联系人信息的类

/**

 * 联系人数据库表的访问类

 */

public class ContactInfoDao {

/**

 * 数据库打开的帮助类

 */

private MyDBOpenHelper helper;

/**

 * 在构造方法里面完成 必须要用的类的初始化

 * @param context

 */

public ContactInfoDao(Context context) {

helper = new MyDBOpenHelper(context);

}

 

/**

 * 添加一条记录

 * @param name 联系人姓名

 * @param phone 联系人电话

 * @return 返回的是添加后在数据库的行号  -1代表添加失败

 */

public long add(String name, String phone){

SQLiteDatabase db = helper.getWritableDatabase();

//db.execSQL("insert into contactinfo (name,phone) values (?,?)", new Object[]{name,phone});

ContentValues values = new ContentValues();

values.put("name", name);

values.put("phone", phone);

//内部是组拼sql语句实现的.

long rowid = db.insert("contactinfo", null, values);

//记得释放数据库资源

db.close();

return rowid;

}

/**

 * 根据姓名删除一条记录

 * @param name 要删除的联系人的姓名

 * @return 返回0代表的是没有删除任何的记录 返回整数int值代表删除了几条数据

 */

public int delete(String name){

//判断这个数据是否存在.

SQLiteDatabase db = helper.getWritableDatabase();

//db.execSQL("delete from contactinfo where name=?", new Object[]{name});

int rowcount = db.delete("contactinfo", "name=?", new String[]{name});

db.close();

//再从数据库里面查询一遍,name是否还在

return rowcount;

}

/**

 * 修改联系人电话号码

 * @param newphone 新的电话号码

 * @param name 要修改的联系人姓名

 * @return 0代表一行也没有更新成功, >0 整数代表的是更新了多少行记录

 */

public int update(String newphone , String name){

SQLiteDatabase db = helper.getWritableDatabase();

//db.execSQL("update contactinfo set phone =? where name=?", new Object[]{newphone,name});

ContentValues values = new ContentValues();

values.put("phone", newphone);

int rowcount =  db.update("contactinfo", values, "name=?", new String[]{name});

db.close();

return rowcount;

}

/**

 * 查询联系人的电话号码

 * @param name 要查询的联系人

 * @return 电话号码

 */

public String getPhoneNumber(String name){

String phone = null;

SQLiteDatabase db = helper.getReadableDatabase();

//Cursor  cursor = db.rawQuery("select phone from contactinfo where name=?", new String[]{name});

Cursor  cursor =  db.query("contactinfo", new String[]{"phone"}, "name=?", new String[]{name}, null, null, null);

if(cursor.moveToNext()){//如果光标可以移动到下一位,代表就是查询到了数据

phone = cursor.getString(0);

}

cursor.close();//关闭掉游标,释放资源

db.close();//关闭数据库,释放资源

return phone;

}

}

第四步:在MainActivity.java中编辑逻辑

public class MainActivity extends Activity {

private EditText et_name;

private EditText et_phone;

private ContactInfoDao dao;

 

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

et_name = (EditText) findViewById(R.id.et_name);

et_phone = (EditText) findViewById(R.id.et_phone);

dao = new ContactInfoDao(this);

}

 

/**

 * 添加一条联系人的信息

 *

 * @param view

 */

public void add(View view) {

String name = et_name.getText().toString().trim();

String phone = et_phone.getText().toString().trim();

if (TextUtils.isEmpty(name) || TextUtils.isEmpty(phone)) {

Toast.makeText(this, "不能为空", 0).show();

return;

} else {

long id = dao.add(name, phone);

if (id == -1) {

Toast.makeText(this, "添加失败", 0).show();

} else {

Toast.makeText(this, "添加成功,在数据库的第:"+id+"", 0).show();

}

}

}

 

/**

 * 删除一条记录

 *

 * @param view

 */

public void delete(View view) {

String name = et_name.getText().toString().trim();

if (TextUtils.isEmpty(name)) {

Toast.makeText(this, "姓名不能为空", 0).show();

return;

} else {

int count = dao.delete(name);

if(count==0){

Toast.makeText(this, "删除失败,没有记录", 0).show();

}else{

Toast.makeText(this, "删除成功,删除了"+count+"条记录", 0).show();

}

}

}

 

/**

 * 修改联系人的号码

 *

 * @param view

 */

public void update(View view) {

String name = et_name.getText().toString().trim();

String phone = et_phone.getText().toString().trim();

if (TextUtils.isEmpty(name) || TextUtils.isEmpty(phone)) {

Toast.makeText(this, "不能为空", 0).show();

return;

} else {

int count = dao.update(phone, name);

if(count==0){

Toast.makeText(this, "更新失败,没有记录", 0).show();

}else{

Toast.makeText(this, "更新成功,更新了"+count+"条记录", 0).show();

}

}

}

 

/**

 * 查询

 *

 * @param view

 */

public void query(View view) {

String name = et_name.getText().toString().trim();

if (TextUtils.isEmpty(name)) {

Toast.makeText(this, "姓名不能为空", 0).show();

return;

} else {

String result = dao.getPhoneNumber(name);

Toast.makeText(this, "号码:" + result, 0).show();

}

}

}

 

第一种:这四种方法都是通过SQL语句实现的

import android.content.Context;

import android.database.Cursor;

import android.database.sqlite.SQLiteDatabase;

 

import com.saiermeng.createdb.MyDBOpenHelper;

 

/**

 * 联系人数据库表的访问类

 */

public class ContactInfoDao {

/**

 * 数据库打开的帮助类

 */

private MyDBOpenHelper helper;

/**

 * 在构造方法里面完成 必须要用的类的初始化

 * @param context

 */

public ContactInfoDao(Context context) {

helper = new MyDBOpenHelper(context);

}

 

/**

 * 添加一条记录

 * @param name 联系人姓名

 * @param phone 联系人电话

 */

public void add(String name, String phone){

SQLiteDatabase db = helper.getWritableDatabase();

db.execSQL("insert into contactinfo (name,phone) values (?,?)", new Object[]{name,phone});

//记得释放数据库资源

db.close();

}

/**

 * 根据姓名删除一条记录

 * @param name 要删除的联系人的姓名

 */

public void delete(String name){

//判断这个数据是否存在.

SQLiteDatabase db = helper.getWritableDatabase();

db.execSQL("delete from contactinfo where name=?", new Object[]{name});

db.close();

//再从数据库里面查询一遍,name是否还在

}

/**

 * 修改联系人电话号码

 * @param newphone 新的电话号码

 * @param name 要修改的联系人姓名

 */

public void update(String newphone , String name){

SQLiteDatabase db = helper.getWritableDatabase();

db.execSQL("update contactinfo set phone =? where name=?", new Object[]{newphone,name});

db.close();

}

/**

 * 查询联系人的电话号码

 * @param name 要查询的联系人

 * @return 电话号码

 */

public String getPhoneNumber(String name){

String phone = null;

SQLiteDatabase db = helper.getReadableDatabase();

Cursor  cursor = db.rawQuery("select phone from contactinfo where name=?", new String[]{name});

if(cursor.moveToNext()){//如果光标可以移动到下一位,代表就是查询到了数据

phone = cursor.getString(0);

}

cursor.close();//关闭掉游标,释放资源

db.close();//关闭数据库,释放资源

return phone;

}

}

 

第二种:通过安卓中封装好的方法

import android.content.ContentValues;

import android.content.Context;

import android.database.Cursor;

import android.database.sqlite.SQLiteDatabase;

 

import com.saiermeng.createdb.MyDBOpenHelper;

 

/**

 * 联系人数据库表的访问类

 */

public class ContactInfoDao {

/**

 * 数据库打开的帮助类

 */

private MyDBOpenHelper helper;

/**

 * 在构造方法里面完成 必须要用的类的初始化

 * @param context

 */

public ContactInfoDao(Context context) {

helper = new MyDBOpenHelper(context);

}

 

/**

 * 添加一条记录

 * @param name 联系人姓名

 * @param phone 联系人电话

 * @return 返回的是添加后在数据库的行号  -1代表添加失败

 */

public long add(String name, String phone){

SQLiteDatabase db = helper.getWritableDatabase();

//db.execSQL("insert into contactinfo (name,phone) values (?,?)", new Object[]{name,phone});

ContentValues values = new ContentValues();

values.put("name", name);

values.put("phone", phone);

//内部是组拼sql语句实现的.

long rowid = db.insert("contactinfo", null, values);

//记得释放数据库资源

db.close();

return rowid;

}

/**

 * 根据姓名删除一条记录

 * @param name 要删除的联系人的姓名

 * @return 返回0代表的是没有删除任何的记录 返回整数int值代表删除了几条数据

 */

public int delete(String name){

//判断这个数据是否存在.

SQLiteDatabase db = helper.getWritableDatabase();

//db.execSQL("delete from contactinfo where name=?", new Object[]{name});

int rowcount = db.delete("contactinfo", "name=?", new String[]{name});

db.close();

//再从数据库里面查询一遍,name是否还在

return rowcount;

}

/**

 * 修改联系人电话号码

 * @param newphone 新的电话号码

 * @param name 要修改的联系人姓名

 * @return 0代表一行也没有更新成功, >0 整数代表的是更新了多少行记录

 */

public int update(String newphone , String name){

SQLiteDatabase db = helper.getWritableDatabase();

//db.execSQL("update contactinfo set phone =? where name=?", new Object[]{newphone,name});

ContentValues values = new ContentValues();

values.put("phone", newphone);

int rowcount =  db.update("contactinfo", values, "name=?", new String[]{name});

db.close();

return rowcount;

}

/**

 * 查询联系人的电话号码

 * @param name 要查询的联系人

 * @return 电话号码

 */

public String getPhoneNumber(String name){

String phone = null;

SQLiteDatabase db = helper.getReadableDatabase();

//Cursor  cursor = db.rawQuery("select phone from contactinfo where name=?", new String[]{name});

Cursor  cursor =  db.query("contactinfo", new String[]{"phone"}, "name=?", new String[]{name}, null, null, null);

if(cursor.moveToNext()){//如果光标可以移动到下一位,代表就是查询到了数据

phone = cursor.getString(0);

}

cursor.close();//关闭掉游标,释放资源

db.close();//关闭数据库,释放资源

return phone;

}

}

 


  • 1
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值