package cn.idiomlianliankan.dao.impl;
import java.util.ArrayList;
import java.util.List;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;
import cn.idiomlianliankan.dao.GameDao;
import cn.idiomlianliankan.domain.CheckPoint;
import cn.idiomlianliankan.domain.Idiom;
import cn.idiomlianliankan.domain.Word;
public class GameDaoImpl implements GameDao {
private SQLiteDatabase db;
public GameDaoImpl(){}
public GameDaoImpl(Context context,SQLiteDatabase db) {
this.db = db;
}
/**
* 添加记录
*/
@Override
public void addIdiom(CheckPoint checkpoint,Idiom idiom) {
Log.i("test", "1");
//插入checkpoint表
//判断checkpoint表里边有没有这个id,如果有就不插入,没有就插入
Cursor cursor = db.rawQuery("select * from checkpoint where checkId=?", new String[]{String.valueOf(checkpoint.getCheckId())});
if(!cursor.moveToNext()){
db.execSQL("insert into checkpoint(checkId) values(?)",
new Object[]{checkpoint.getCheckId()});
}
Log.i("test", "2");
//插入idiom表
db.execSQL("insert into idiom( idiomContent , idiomExplain , idiomProv , foreignCheckId) values(?,?,?,?)",
new Object[]{idiom.getIdiomContent(),idiom.getIdiomExplain(),idiom.getIdiomProv(),idiom.getCheckpoint().getCheckId()});
Log.i("test", "idiom:"+idiom.getIdiomContent()+idiom.getIdiomExplain()+idiom.getIdiomProv()+idiom.getCheckpoint().getCheckId());
//插入word表
idiom = getIdiom(idiom.getIdiomContent());
addWord(idiom);
Log.i("test", "4");
}
/**
* 把成语分解成四个word对象,然后插入到word表中
*/
public void addWord(Idiom idiom){
Log.i("test", "5");
String idiomContent = idiom.getIdiomContent();
for(int i =1;i<=4;i++){
char wordContent = idiomContent.charAt(i-1);
db.execSQL("insert into word(wordContent,wordLevel,foreignIdiomId) values(?,?,?)",new Object[]{wordContent , i ,idiom.getIdiomId()});
}
}
/**
* 根据idiomContent得到数据库中的idiom对象
*/
public Idiom getIdiom(String content){
Idiom idiom = new Idiom();
Cursor cursor = db.rawQuery("select * from idiom where idiomContent=?", new String[]{content});
if(cursor!=null && cursor.moveToFirst()){
int idiomId = cursor.getInt(cursor.getColumnIndex("idiomId"));
String idiomContent = content;
String idiomExplain = cursor.getString(cursor.getColumnIndex("idiomExplain"));
String idiomProv = cursor.getString(cursor.getColumnIndex("idiomProv"));
idiom.setIdiomId(idiomId);
idiom.setIdiomContent(idiomContent);
idiom.setIdiomExplain(idiomExplain);
idiom.setIdiomProv(idiomProv);
}
return idiom;
}
/**
* 根据idiomId得到数据库中的idiom对象
*/
public Idiom getIdiom(int idiomId){
Idiom idiom = new Idiom();
idiom.setIdiomId(idiomId);
Cursor cursor = db.rawQuery("select * from idiom where idiomId=?", new String[]{String.valueOf(idiomId)});
if(cursor!=null && cursor.moveToFirst()){
String idiomContent = cursor.getString(cursor.getColumnIndex("idiomContent"));
String idiomExplain = cursor.getString(cursor.getColumnIndex("idiomExplain"));
String idiomProv = cursor.getString(cursor.getColumnIndex("idiomProv"));
idiom.setIdiomContent(idiomContent);
idiom.setIdiomExplain(idiomExplain);
idiom.setIdiomProv(idiomProv);
}
return idiom;
}
/**
* 返回某一关卡的Idiom的List集合
*/
@Override
public List<Idiom> getIdioms(CheckPoint cp) {
List<Idiom> idioms = new ArrayList<Idiom>();
Log.i("lujing", "dao层");
Cursor cursor = db.rawQuery("select * from idiom where foreignCheckId=?", new String[]{String.valueOf(cp.getCheckId())});
while(cursor.moveToNext()){
int idiomId = cursor.getInt(cursor.getColumnIndex("idiomId"));
String idiomContent = cursor.getString(cursor.getColumnIndex("idiomContent"));
String idiomExplain = cursor.getString(cursor.getColumnIndex("idiomExplain"));
String idiomProv = cursor.getString(cursor.getColumnIndex("idiomProv"));
Idiom idiom = new Idiom(idiomExplain, idiomProv, idiomContent, cp);
idiom.setIdiomId(idiomId);
idioms.add(idiom);
}
cursor.close();
return idioms;
}
/**
* 通过Idiom对象,从数据库中得到四个Word对象
*/
@Override
public List<Word> getWordsByIdiom(Idiom idiom) {
List<Word> words = new ArrayList<Word>();
Cursor cursor = db.rawQuery("select * from word where foreignIdiomId=?", new String[]{String.valueOf(idiom.getIdiomId())});
while(cursor.moveToNext()){
int wordId = cursor.getInt(cursor.getColumnIndex("wordId"));
String wordContent = cursor.getString(cursor.getColumnIndex("wordContent"));
int wordLevel = cursor.getInt(cursor.getColumnIndex("wordLevel"));
Word word = new Word(wordLevel, wordContent.charAt(0), idiom);
word.setWordId(wordId);
words.add(word);
}
cursor.close();
return words;
}
/**
* 通过id找到word对象
*/
@Override
public Word findWord(int id) {
Word word = new Word();
word.setWordId(id);
Cursor cursor = db.rawQuery("select * from word where wordId=?", new String[]{String.valueOf(id)});
while(cursor.moveToNext()){
String wordContent = cursor.getString(cursor.getColumnIndex("wordContent"));
int wordLevel = cursor.getInt(cursor.getColumnIndex("wordLevel"));
int foreignIdiomId = cursor.getInt(cursor.getColumnIndex("foreignIdiomId"));
Idiom idiom = new Idiom();
idiom = getIdiom(foreignIdiomId);
word.setIdiom(idiom);
word.setWordContent(wordContent.charAt(0));
word.setWordLevel(wordLevel);
}
return word;
}
}
- 1
- 2
前往页