android


Update SQL Database without using ROW_ID


How do I make an UPDATE function without having to use ROW_ID. For some reason including ROW_ID in the SQL Table is giving me multiple errors. Its working perfectly fine without it.
All I need is how to use the where parameter in update using another constraint, like suppose, Registration number.
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);
}
}
Where is the WHERE clause of your update statement
ourDatabase.update(DATABASE_TABLE, cvup, WHERE, null);
Giving you a sample update clause:
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;
}
See here.
UPDATE use
private static final String DATABASE_NAME = "data.db"; //You forgot .db

Related Links

I'm using Eclipse and Android and a lot of things are messed up in the MainActivity XML
RTSP obtaining DVR channels automatically
Android CameraAccessException with older Apis generating lint error
How to vertically center the items in the Android Toolbar View
Android Emulator for Visual Studio 2015 not launching
time out error while connecting url
React native slow loading image and application process
How to keep user Logged-In always “Connected with Server”
Error on Android Media player
I cant install the Intel HAXM,the installer welcome screen not appear when i clicking it
Use android phone to control a oled display
Why my application doesn't start after the installation in samsung knox?
Android Studio : Unable to resolve library class symbol
Android - TextInputLayout Icon error not missing
Gradle: add list of prebuilt static libraries instead of adding one by one
How do I use Viewholder with ExpandableListView?

Categories

HOME
batch-file
scikit-learn
gdb
rdbms
datastore
rsa
battery
solver
entity-framework-core
google-apps
google-admin-sdk
joomla3.0
drop-down-menu
endeca
android-securityexception
resultset
gource
mongodb-csharp
symbol
screen-readers
powershell-v5.0
fileserver
lilypond
simpleitk
squashfs
inno-download-plugin
angular-routing
hta
php-mysqlidb
object-storage
flask-sqlalchemy
highstock
roundup
maze
cognos-bi
catch-unit-test
hibernate-search
protege4
android-augmented-reality
news
setup.py
mcustomscrollbar
tasm
trigonometry
install.packages
dapper-simplecrud
httr
plan-9
proget
rserve
reindex
yii2-user
boo
cup
flyout
winusb
redbeard-ios
seek
signalr.client
macaulay2
observablecollection
swizzling
fink
invoke-sqlcmd
file-move
poppler
android-launcher
opserver
extjs-grid
coda
dulwich
xmltype
procedural-programming
rapidsvn
showdialog
punycode
nsmenu
reflector
memset
tlf
kogrid
route-provider
insert-id
http-status-code-410
device-width
rmiregistry
correlated-subquery
box2d-iphone
convention
massive
jqueryform
business-model

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App