我想检查数据库中是否已有5行,但我不能使用execSQL方法调用SELECT,因为它返回void,而其他任何东西似乎都不适合if语句。那么有一个简单的解决方法还是我必须采取另一种方式?
//add a new row to the database
public void addProduct(Products product){
SQLiteDatabase db = this.getWritableDatabase();
if(db.execSQL("SELECT Count * FROM " + TABLE_PRODUCTS) == 5){
ContentValues values = new ContentValues();
values.put(COLUMN_PRODUCTNAME, product.get_productname());
db.insert(TABLE_PRODUCTS, null, values);
db.close();
}
}
答案 0 :(得分:3)
试试这个
module carona
open util/ordering[Time]
sig Time {}
sig Car {
passengers : set Passager
}
one sig UFCG{
students: set Student -> Time
}
abstract sig Student{
region: Region -> Time
}
sig Owner extends Student{
ownerCar : Car
}
sig Passager extends Student{
}
abstract sig Region{ }
one sig Norte,Sul,Leste,Oeste,Centro extends Region{ }
fact Owner{
all o: Owner | #o.ownerCar = 1
}
fact Passager{
all p:Passager | one p.~passengers
}
fact Car{
all c:Car| one c.~ownerCar
all c:Car| #c.passengers <= 4
all c:Car| c.passengers.region=(c.~ownerCar).region
}
fact UFCG {
all passager: Passager, ufcg : UFCG, t:Time | passager in (ufcg.students).t
all owner: Owner, ufcg: UFCG,t:Time | owner in (ufcg.students).t
}
fact Traces{
init[first]
all pre: Time-last | let pos = pre.next |
lone u:UFCG|
some s:Student|
addStudent[s,u,pre,pos]
}
pred init[t:Time]{
one u:UFCG | no (u.students).t
}
pred addStudent[s:Student, u: UFCG, before, after: Time]{
(s !in (u.students).before)
(u.students).after = (u.students).before + s
}
答案 1 :(得分:-1)
结果可能是这样的:
public int addProduct()
{
String selectQuery = "SELECT * FROM TABLE_PRODUCTS";
SQLiteDatabase database = this.dbOpenHelper.getReadableDatabase();
Cursor c = database.rawQuery(selectQuery, null);
c.moveToFirst();
int total = c.getCount();
c.close();
return total;
}