首先要对数据库操作需要继承 :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);
}