有时候,我们的SQL语句可能会比较复杂,用DAO的一些方法可能不能满足需要。这时候就需要我们用到一些原生的sql语句,还好ORMlite框架中提供了这样了方法。
如queryRaw,executeRaw等
以查询为例(queryRaw的使用):
先建一个Dictionary方法:
@DatabaseTable(tableName="Dictionary")
public class Dictionary
{
public static final String FIELD_ID = "_id";
@DatabaseField(columnName="_id", generatedId=true)
private Long id;
@DatabaseField(canBeNull=false, columnName="chinese", uniqueCombo=true)
private String chinese;
@DatabaseField(canBeNull=false, columnName="phonetic")
private String phonetic;
@DatabaseField(canBeNull=false, columnName="word", uniqueCombo=true)
private String word;
@DatabaseField(canBeNull=false, columnName="difficulty", uniqueCombo=true)
private String difficulty;
public Dictionary() {}
public Dictionary(String word, String translation, String phonetic)
{
this.word = word;
this.chinese = translation;
this.phonetic = phonetic;
}
public Long getId()
{
return this.id;
}
public String getLanguage()
{
return this.chinese;
}
public String getPhonetic()
{
return this.phonetic;
}
public String getWord()
{
return this.word;
}
public void setId(Long paramLong)
{
this.id = paramLong;
}
public void setLanguage(String paramString)
{
this.chinese = paramString;
}
public void setPhonetic(String phonetic)
{
this.phonetic = phonetic;
}
public void setWord(String paramString)
{
this.word = paramString;
}
public String getDifficulty() {
return difficulty;
}
public void setDifficulty(String difficulty) {
this.difficulty = difficulty;
}
然后建立查询
try {
Dao<Dictionary, String> gameDao = aDbLevelHelper.getDao(Dictionary.class);
String sql = "select * from Dictionary where difficulty='" + difficulty + "' and length(word) >= 2 and length(word)<= 7" ;
GenericRawResults<Dictionary> results = gameDao.queryRaw(sql, new RawRowMapper<Dictionary>() {
@Override
public Dictionary mapRow(String[] columnNames, String[] resultColumns) throws SQLException {
Dictionary dictionary = new Dictionary();
dictionary.setId(Long.parseLong(resultColumns[0]));
dictionary.setWord(resultColumns[1]);
dictionary.setPhonetic(resultColumns[2]);
dictionary.setLanguage(resultColumns[3]);
dictionary.setDifficulty(resultColumns[4]);
return dictionary;
}
});
List<Dictionary> dictionaryList = new ArrayList<Dictionary>();
Iterator<Dictionary> iterator = results.iterator();
while (iterator.hasNext()) {
Dictionary dictionary = iterator.next();
dictionaryList.add(dictionary);
}
} catch (Exception e1) {
e1.printStackTrace();
}
这样dictionaryList就得到了我们要的数据列表字段。
executeRaw的使用:
String sql = "delete from Dictionary";
DbGameHelper aDbLevelHelper = OpenHelperManagerEx.getHelper(_context, DbGameHelper.class);
try {
Dao<Dictionary, String> gameDao1 = aDbLevelHelper.getDao(Dictionary.class);
gameDao1.executeRaw(sql);
} catch (Exception e1) {
e1.printStackTrace();
}
这样就做我们想在做的操作了。
如queryRaw,executeRaw等
以查询为例(queryRaw的使用):
先建一个Dictionary方法:
@DatabaseTable(tableName="Dictionary")
public class Dictionary
{
public static final String FIELD_ID = "_id";
@DatabaseField(columnName="_id", generatedId=true)
private Long id;
@DatabaseField(canBeNull=false, columnName="chinese", uniqueCombo=true)
private String chinese;
@DatabaseField(canBeNull=false, columnName="phonetic")
private String phonetic;
@DatabaseField(canBeNull=false, columnName="word", uniqueCombo=true)
private String word;
@DatabaseField(canBeNull=false, columnName="difficulty", uniqueCombo=true)
private String difficulty;
public Dictionary() {}
public Dictionary(String word, String translation, String phonetic)
{
this.word = word;
this.chinese = translation;
this.phonetic = phonetic;
}
public Long getId()
{
return this.id;
}
public String getLanguage()
{
return this.chinese;
}
public String getPhonetic()
{
return this.phonetic;
}
public String getWord()
{
return this.word;
}
public void setId(Long paramLong)
{
this.id = paramLong;
}
public void setLanguage(String paramString)
{
this.chinese = paramString;
}
public void setPhonetic(String phonetic)
{
this.phonetic = phonetic;
}
public void setWord(String paramString)
{
this.word = paramString;
}
public String getDifficulty() {
return difficulty;
}
public void setDifficulty(String difficulty) {
this.difficulty = difficulty;
}
然后建立查询
try {
Dao<Dictionary, String> gameDao = aDbLevelHelper.getDao(Dictionary.class);
String sql = "select * from Dictionary where difficulty='" + difficulty + "' and length(word) >= 2 and length(word)<= 7" ;
GenericRawResults<Dictionary> results = gameDao.queryRaw(sql, new RawRowMapper<Dictionary>() {
@Override
public Dictionary mapRow(String[] columnNames, String[] resultColumns) throws SQLException {
Dictionary dictionary = new Dictionary();
dictionary.setId(Long.parseLong(resultColumns[0]));
dictionary.setWord(resultColumns[1]);
dictionary.setPhonetic(resultColumns[2]);
dictionary.setLanguage(resultColumns[3]);
dictionary.setDifficulty(resultColumns[4]);
return dictionary;
}
});
List<Dictionary> dictionaryList = new ArrayList<Dictionary>();
Iterator<Dictionary> iterator = results.iterator();
while (iterator.hasNext()) {
Dictionary dictionary = iterator.next();
dictionaryList.add(dictionary);
}
} catch (Exception e1) {
e1.printStackTrace();
}
这样dictionaryList就得到了我们要的数据列表字段。
executeRaw的使用:
String sql = "delete from Dictionary";
DbGameHelper aDbLevelHelper = OpenHelperManagerEx.getHelper(_context, DbGameHelper.class);
try {
Dao<Dictionary, String> gameDao1 = aDbLevelHelper.getDao(Dictionary.class);
gameDao1.executeRaw(sql);
} catch (Exception e1) {
e1.printStackTrace();
}
这样就做我们想在做的操作了。