android 数据库学习实例代码分享

倒腾了一天终于摸透了这一块内容。

【插入,更新,删除,查询】四个基本功能已经完全没问题了,其他的就是怎么跟控件绑定的问题了



截图如下,简单说一下实现的功能

四个输入框

输入名字(字符串),攻击(整数),敏捷(整数),智力(整数);

四个按钮

插入:输入上面四个数据之后,新增一条数据信息

更新:输入上面四个数据之后,针对【名字】信息相同的修改其他属性数据

删除:输入【名字】信息之后,删除同名数据信息

查询:查询显示全部数据信息

附注说明一下,下面写了一个滑动条,可以滑动最下面一块数据显示(TextView)


这就是大概的思路,不过目前也有没有解决的问题,就是怎么抽出来一块进行单独操作,这个需要涉及到【ContentProvider】以后再说


重要代码如下:

四个按钮:

insertButton.setOnClickListener(new OnClickListener() {			
			public void onClick(View v) {
					name0=nameET.getText().toString();
					att0=Integer.parseInt(attET.getText().toString());
					agi0=Integer.parseInt(agiET.getText().toString());
					int0=Integer.parseInt(intET.getText().toString());
					if (checkOk(name0,att0,agi0,int0)) {
						insertDataEX(sqlhelper, name0, att0, agi0, int0);
				        result=queryData(sqlhelper);
						tView.setText("职业\t攻击\t速度\t智力\n"+result);
						
						nameET.setText(null);
						attET.setText("0");
						agiET.setText("0");
						intET.setText("0");
					}else {
						tView.setText("你输入的数据信息不符!");
					}							
			}
		});
		
		updateButton.setOnClickListener(new OnClickListener() {
			
			public void onClick(View v) {
				name0=nameET.getText().toString();
				att0=Integer.parseInt(attET.getText().toString());
				agi0=Integer.parseInt(agiET.getText().toString());
				int0=Integer.parseInt(intET.getText().toString());
				if (checkOk(name0,att0,agi0,int0)) {
					updateDataEX(sqlhelper, name0, att0, agi0, int0);
			        result=queryData(sqlhelper);
					tView.setText("职业\t攻击\t速度\t智力\n"+result);
					
					nameET.setText(null);
					attET.setText("0");
					agiET.setText("0");
					intET.setText("0");
				}else {
					tView.setText("你输入的数据信息不符!");
				}		
			}
		});
		
		deleteButton.setOnClickListener(new OnClickListener() {
			
			public void onClick(View v) {
				name0=nameET.getText().toString();
				if (name0!=null) {
					deleteDataEX(sqlhelper, name0);
			        result=queryData(sqlhelper);
					tView.setText("职业\t攻击\t速度\t智力\n"+result);
					
					nameET.setText(null);
					attET.setText("0");
					agiET.setText("0");
					intET.setText("0");
				}else {
					tView.setText("你输入的数据信息不符!");
				}		
			}
		});
		
		selectButton.setOnClickListener(new OnClickListener() {
			
			public void onClick(View v) {
		        result=queryData(sqlhelper);
				tView.setText("职业\t攻击\t速度\t智力\n"+result);
			}
		});

然后是相关用到的操作方法:

    public void insertData(SQLhelper sqLhelper){
		SQLiteDatabase db=sqLhelper.getWritableDatabase();
        System.out.println("2222");
		db.execSQL("insert into hero(name,attack,agile,intelligence) values('战士',3,5,7)");
        System.out.println("44444");
		ContentValues values=new ContentValues();
		values.put("name", "法师");
		values.put("attack", 4);
		values.put("agile", 4);
		values.put("intelligence", 3);
		db.insert("hero", "id", values);
		db.close();
	}
	
	public String queryData(SQLhelper sqLhelper){
		String result="";
		SQLiteDatabase db=sqLhelper.getReadableDatabase();
        System.out.println("555");
		Cursor cursor=db.query("hero", null, null, null, null, null, null);
		int nameIndex=cursor.getColumnIndex("name");
		int attIndex=cursor.getColumnIndex("attack");
		int agiIndex=cursor.getColumnIndex("agile");
		int intIndex=cursor.getColumnIndex("intelligence");
		
		for (cursor.moveToFirst();!(cursor.isAfterLast());cursor.moveToNext()) {
			result=result+cursor.getString(nameIndex)+" \t";
			result=result+cursor.getInt(attIndex)+" \t";
			result=result+cursor.getInt(agiIndex)+" \t";
			result=result+cursor.getInt(intIndex)+" \n";
		}
		cursor.close();
		db.close();
		return result;
	}
	
	public void insertDataEX(SQLhelper sqLhelper,String name1,int att0,int agi0,int int0){
		SQLiteDatabase db=sqLhelper.getWritableDatabase();
		ContentValues values=new ContentValues();
		values.put("name", name1);
		values.put("attack", att0);
		values.put("agile", agi0);
		values.put("intelligence", int0);
		db.insert("hero", "id", values);
		db.close();
	}
	
	public void updateDataEX(SQLhelper sqlhelper, String name0,
			int att0, int agi0, int int0) {
		SQLiteDatabase db=sqlhelper.getWritableDatabase();
		ContentValues values=new ContentValues();
		values.put("name", name0);
		values.put("attack", att0);
		values.put("agile", agi0);
		values.put("intelligence", int0);
  		db.update("hero", values, "name = '"+name0+"'", null);	
		db.close();
	}
	
	public void deleteDataEX(SQLhelper sqlhelper, String name0) {
		SQLiteDatabase db=sqlhelper.getWritableDatabase();
		ContentValues values=new ContentValues();
		values.put("name", name0);
  		db.delete("hero", "name = '"+name0+"'", null);
		db.close();
	}
其他一些代码也有重要的部分,不过暂时我只提供下载,就不在帖子里放出来了。


附带放出sqlhelper类,这个类提供了创建于删除的操作,其实可以考虑把几个操作都搬到这里来,会更合适一些

package sam.sucl.sqltest2;

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

public class SQLhelper extends SQLiteOpenHelper{
	public static final String tableName="hero";
	public static final String ID="id";
	public static final String NAME="name";
	public static final String ATT="attack";
	public static final String AGI="agile";
	public static final String INT="intelligence";

	public SQLhelper(Context context, String name, CursorFactory factory,
			int version) {
		super(context, name, factory, version);
	}

	public void onCreate(SQLiteDatabase db){
//		db.execSQL("create table if not exists "+tableName+"("+ID+" integer primary key,"+NAME+" varchar,"
//				+ATT+" integer,"+AGI+" integer,"+INT+" integer)");		
		db.execSQL("create table if not exists hero(id integer primary key,name varchar,attack integer,agile integer,intelligence integer)");		
	}

	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
		db.execSQL("drop table if exists"+tableName);
		onCreate(db);
	}

}
上面我想用两种方法来创建数据库,大家可以看到。



代码下载:http://www.apkbus.com/forum.php?mod=viewthread&tid=22072&extra=

安卓巴士论坛挂了下载,大家不妨去下

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值