数据库:ORMLite中对复杂语句的处理

有时候,我们的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(); 
}
这样就做我们想在做的操作了。
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值