如何在不使用 ROW_ID 的情况下制作UPDATE
功能。由于某些原因,SQL表中的 ROW_ID 给了我多个错误。没有它,它的工作完美。
我需要的是如何使用另一个约束在更新中使用where参数,例如假设,注册号。
public class Work {
public static final String KEY_NAME = "name";
public static final String KEY_REG = "reg";
public static final String KEY_QUIZ1 = "q1";
public static final String KEY_QUIZ2 = "q2";
public static final String KEY_QUIZ3 = "q3";
public static final String KEY_CAT1 = "c1";
public static final String KEY_CAT2 = "c2";
public static final String KEY_TEE = "t";
private DbHelper ourHelper;
private final Context ourContext;
private static SQLiteDatabase ourDatabase;
private static final String DATABASE_NAME = "data";
private static final String DATABASE_TABLE = "stable";
private static final int DATABASE_VERSION = 1;
private static class DbHelper extends SQLiteOpenHelper{
public DbHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
// TODO Auto-generated constructor stub
}
@Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
db.execSQL("CREATE TABLE " + DATABASE_TABLE + " (" +
KEY_REG + " TEXT, " +
KEY_NAME + " TEXT, " +
KEY_QUIZ1 + " TEXT, " +
KEY_QUIZ2 + " TEXT, " +
KEY_QUIZ3 + " TEXT, " +
KEY_CAT1 + " TEXT, " +
KEY_CAT2 + " TEXT, " +
KEY_TEE + " TEXT);"
);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
db.execSQL("DROP TBALE IF EXISTS " + DATABASE_TABLE);
onCreate(db);
}
}
public Work(Context c){
ourContext = c;
}
public Work open() throws SQLException{
ourHelper = new DbHelper(ourContext);
ourDatabase = ourHelper.getWritableDatabase();
return this;
}
public void close(){
ourHelper.close();
}
public long createEntry(String string, String string2, String string3, String string4, String string5, String string6, String string7, String string8) {
// TODO Auto-generated method stub
ContentValues cv = new ContentValues();
cv.put(KEY_REG, string);
cv.put(KEY_NAME, string2);
cv.put(KEY_QUIZ1, string3);
cv.put(KEY_QUIZ2, string4);
cv.put(KEY_QUIZ3, string5);
cv.put(KEY_CAT1, string6);
cv.put(KEY_CAT2, string7);
cv.put(KEY_TEE, string8);
return ourDatabase.insert(DATABASE_TABLE, null, cv);
}
public String getq1(int j) {
// TODO Auto-generated method stub
int i=0;
String[] columns = new String[]{KEY_REG, KEY_NAME, KEY_QUIZ1, KEY_QUIZ2, KEY_QUIZ3, KEY_CAT1, KEY_CAT2, KEY_TEE};
Cursor c = ourDatabase.query(DATABASE_TABLE, columns, null, null, null, null, null);
String res = "";
int iQ1=c.getColumnIndex(KEY_QUIZ1);
for(c.moveToFirst();i<j && !c.isAfterLast();i++,c.moveToNext())
{}
res = c.getString(iQ1);
return res;
}
public String getq2(int j) {
// TODO Auto-generated method stub
int i=0;
String[] columns = new String[]{KEY_REG, KEY_NAME, KEY_QUIZ1, KEY_QUIZ2, KEY_QUIZ3, KEY_CAT1, KEY_CAT2, KEY_TEE};
Cursor c = ourDatabase.query(DATABASE_TABLE, columns, null, null, null, null, null);
String res = "";
int iQ1=c.getColumnIndex(KEY_QUIZ2);
for(c.moveToFirst();i<j && !c.isAfterLast();i++,c.moveToNext())
{}
res = c.getString(iQ1);
return res;
}
public String getq3(int j) {
// TODO Auto-generated method stub
int i=0;
String[] columns = new String[]{KEY_REG, KEY_NAME, KEY_QUIZ1, KEY_QUIZ2, KEY_QUIZ3, KEY_CAT1, KEY_CAT2, KEY_TEE};
Cursor c = ourDatabase.query(DATABASE_TABLE, columns, null, null, null, null, null);
String res = "";
int iQ1=c.getColumnIndex(KEY_QUIZ3);
for(c.moveToFirst();i<j && !c.isAfterLast();i++,c.moveToNext())
{}
res = c.getString(iQ1);
return res;
}
public String getc1(int j) {
// TODO Auto-generated method stub
int i=0;
String[] columns = new String[]{ KEY_REG, KEY_NAME, KEY_QUIZ1, KEY_QUIZ2, KEY_QUIZ3, KEY_CAT1, KEY_CAT2, KEY_TEE};
Cursor c = ourDatabase.query(DATABASE_TABLE, columns, null, null, null, null, null);
String res = "";
int iQ1=c.getColumnIndex(KEY_CAT1);
for(c.moveToFirst();i<j && !c.isAfterLast();i++,c.moveToNext())
{}
res = c.getString(iQ1);
return res;
}
public String getc2(int j) {
// TODO Auto-generated method stub
int i=0;
String[] columns = new String[]{ KEY_REG, KEY_NAME, KEY_QUIZ1, KEY_QUIZ2, KEY_QUIZ3, KEY_CAT1, KEY_CAT2, KEY_TEE};
Cursor c = ourDatabase.query(DATABASE_TABLE, columns, null, null, null, null, null);
String res = "";
int iQ1=c.getColumnIndex(KEY_CAT2);
for(c.moveToFirst();i<j && !c.isAfterLast();i++,c.moveToNext())
{}
res = c.getString(iQ1);
return res;
}
public String gette(int j) {
// TODO Auto-generated method stub
int i=0;
String[] columns = new String[]{KEY_REG, KEY_NAME, KEY_QUIZ1, KEY_QUIZ2, KEY_QUIZ3, KEY_CAT1, KEY_CAT2, KEY_TEE};
Cursor c = ourDatabase.query(DATABASE_TABLE, columns, null, null, null, null, null);
String res = "";
int iQ1=c.getColumnIndex(KEY_TEE);
for(c.moveToFirst();i<j && !c.isAfterLast();i++,c.moveToNext())
{}
res = c.getString(iQ1);
return res;
}
public String getData() {
// TODO Auto-generated method stub
String[] columns = new String[]{KEY_REG, KEY_NAME, KEY_QUIZ1, KEY_QUIZ2, KEY_QUIZ3, KEY_CAT1, KEY_CAT2, KEY_TEE};
Cursor c = ourDatabase.query(DATABASE_TABLE, columns, null, null, null, null, null);
String res = "";
int ireg=c.getColumnIndex(KEY_REG);
int ina=c.getColumnIndex(KEY_NAME);
int iQ1=c.getColumnIndex(KEY_QUIZ1);
int iQ2=c.getColumnIndex(KEY_QUIZ2);
int iQ3=c.getColumnIndex(KEY_QUIZ3);
int ic1=c.getColumnIndex(KEY_CAT1);
int ic2=c.getColumnIndex(KEY_CAT2);
int it=c.getColumnIndex(KEY_TEE);
for(c.moveToFirst(); !c.isAfterLast(); c.moveToNext())
{
res+=c.getString(ireg)+" "+c.getString(ina)+" "+c.getString(iQ1)+" "+c.getString(iQ2)+" "+c.getString(iQ3)+" "+c.getString(ic1)+" "+c.getString(ic2)+" "+c.getString(it)+"\n";
}
return res;
}
public void updateEntry(int j, String g1, String g2, String g3, String g4,
String g5, String g6) {
// TODO Auto-generated method stub
ContentValues cvup= new ContentValues();
cvup.put(KEY_QUIZ1, g1);
cvup.put(KEY_QUIZ2, g2);
cvup.put(KEY_QUIZ3, g3);
cvup.put(KEY_CAT1, g4);
cvup.put(KEY_CAT2, g5);
cvup.put(KEY_TEE, g6);
ourDatabase.update(DATABASE_TABLE, cvup, WHERE, null);
}
}
答案 0 :(得分:0)
更新语句的WHERE子句在哪里
ourDatabase.update(DATABASE_TABLE, cvup, WHERE, null);
为您提供样本更新条款:
public boolean updateContact (Integer id, String name, String phone, String email)
{
SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put("name", name);
contentValues.put("phone", phone);
contentValues.put("email", email);
db.update("contacts", contentValues, "id = ? ", new String[] { Integer.toString(id) } );
return true;
}
请参阅here。
更新使用
private static final String DATABASE_NAME = "data.db"; //You forgot .db