android中SQLite简单操作

首先要对数据库操作需要继承 :SQLiteOpenHelper

然后需要做的是和数据库进行连接:

        private static final String DB_NAME ="user.ttatx_db";
	private static final int VERSION = 1;//数据库版本
	
	public TtatxDatabaseHelper(Context context) { //构造函数 
		super(context,DB_NAME,null,VERSION);// 传入数据库名称和版本
		// TODO Auto-generated constructor stub
	}

这样,在这个类中所进行的操作就是对
user.ttatx_db
进行操作了

对于一个数据库,首先需要建表,继承了SQLiteOpenHelper会自动执行这个onCreate类进行建表:


然后是进行插入数据,插入数据的时候要十分注意引号的位置:

ttatx_db.execSQL("create table if not exists ttatx(_id integer primary key autoincrement,nianji integer not null,ke integer not null,hanzi text not null,pinyin text not null,zuci text not null,jieshi text not null)");
ttatx_db.execSQL("insert into ttatx(nianji,ke,hanzi,pinyin,zuci,jieshi) values(1,1,'一','yi','一个 一双 一条','数量的计数单位')");

对于查询,每次的查询结果保存在Cursor中:

下列代码是将得到Cursor通过一个类转成list,最后得到一个list的数据:

	@Override
	public void onCreate(SQLiteDatabase ttatx_db) {
		// TODO Auto-generated method stub
		ttatx_db.execSQL("create table if not exists ttatx" +
				"(_id integer primary key autoincr<pre name="code" class="java">	public Cursor queryTtatx(){//查询全部的
		Cursor c=getReadableDatabase().rawQuery("select * from ttatx",null);
		return new TtatxCursor(c);
	}
	
	public Cursor queryTtatxKe(int nianji,int ke){//查询一课
		//Cursor c=getReadableDatabase().rawQuery("select * from ttatx where nianji=4 and ke=1" ,null);
		Cursor c=getReadableDatabase().rawQuery("select * from ttatx " +
				"where nianji="+String.valueOf(nianji)+" "+"and"+" "+
						" ke="+String.valueOf(ke),null);
		return new TtatxCursor(c);
	}
	
	public Cursor queryTtatxzi(String hanzi){//查询 一个字
		Cursor c=getReadableDatabase().rawQuery("select * from ttatx " +
				"where hanzi="+"'"+hanzi+"'", null);
		if(c.getCount()==0)
			return null;
		return new TtatxCursor(c);
	}

ement," +"nianji integer not null,ke integer not null," +"hanzi text not null," +"pinyin text not null," +"zuci text not null," +"jieshi text not null)");insert(ttatx_db);}
 下列是corsor转list的类: 

下列代码是将得到Cursor通过一个类转成list,最后得到一个list的数据:

	@Override
	public void onCreate(SQLiteDatabase ttatx_db) {
		// TODO Auto-generated method stub
		ttatx_db.execSQL("create table if not exists ttatx" +
				"(_id integer primary key autoincr

	public Cursor queryTtatx(){//查询全部的
		Cursor c=getReadableDatabase().rawQuery("select * from ttatx",null);
		return new TtatxCursor(c);
	}
	
	public Cursor queryTtatxKe(int nianji,int ke){//查询一课
		//Cursor c=getReadableDatabase().rawQuery("select * from ttatx where nianji=4 and ke=1" ,null);
		Cursor c=getReadableDatabase().rawQuery("select * from ttatx " +
				"where nianji="+String.valueOf(nianji)+" "+"and"+" "+
						" ke="+String.valueOf(ke),null);
		return new TtatxCursor(c);
	}
	
	public Cursor queryTtatxzi(String hanzi){//查询 一个字
		Cursor c=getReadableDatabase().rawQuery("select * from ttatx " +
				"where hanzi="+"'"+hanzi+"'", null);
		if(c.getCount()==0)
			return null;
		return new TtatxCursor(c);
	}


ement," +
				"nianji integer not null,ke integer not null," +
				"hanzi text not null," +
				"pinyin text not null," +
				"zuci text not null," +
				"jieshi text not null)");
		insert(ttatx_db);
		
	}


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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值