I have a little problem with inserting data in sqlite in Android. I wrote a method which do that with ContentValues,but it's not working properly. Here is the method :
DatabaseHelper.class
public boolean executeQuery(String tableName,String keys,Object value){
return execQuery(tableName,keys,value);
}
private static boolean execQuery(String tableName,String key,Object value){
sqliteDb = instance.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(key, value.toString());
sqliteDb.insert(tableName, null, values);
return true;
}
And I'm using this like that :
dbHelper.executeQuery("users", "seed", seed); // string
dbHelper.executeQuery("users", "id", id); // int
dbHelper.executeQuery("users", "name", name); // string
dbHelper.executeQuery("users", "lang", lang); // string
And the problem is that I this method insert all values as single row in database,but I want all data to be a part of one row.
What I have to do to get the things to work correctly?
I'm not really good with sqlite,so please excuse me if my question is a little silly...
Thanks in advance!
private static boolean execQuery(String tableName,String seed,String id,String name, String lang){
sqliteDb = instance.getWritableDatabase();
ContentValues values = new ContentValues();
values.put("seed", seed);
values.put("id", id);
values.put("name", name);
values.put("lang", lang);
sqliteDb.insert(tableName, null, values);
return true;
}
dbHelper.executeQuery("users",seed,id, name,lang); // string
EDITED
private void execQuery(String tableName,ContentValues val)
{
sqliteDb = instance.getWritableDatabase();
sqliteDb.insert(tableName, null, val);
return true;
}
call
ContentValues values = new ContentValues();
values.put("seed", seed);
values.put("id", id);
values.put("name", name);
values.put("lang", lang);
dbHelper.executeQuery("users",values); // string
You should use the same ContentValues object to add all your desired columns. Like this:
Content values= new ContentValues();
values.add("seed",seed);
values.add("id",id);
values.add("name",name);
values.add("lang",lang);
sqliteDb.insert(tableName,null,values);
you have to pass string key array and Object array
dbHelper.executeQuery("users", /*Array of seed,id,name,lang*/, /*Object Array of their value*/);
and use
values.put(key[i], value[i].toString()); for all i=0 to n
Related
How to check whether the value is inserted or not in Android?
I want to display
if inserted==true
return true;
else
return false;
This is my code:
SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
sqLiteDatabase.execSQL("INSERT INTO user VALUES (1,1,1,'admin','admin','21232f297a57a5a743894a0e4a801fc3','9986058114','8897456588','admin#admin.com','','Active','AD','admin')");
Check By DataBase : https://stackoverflow.com/a/21151598/8448886
You can also use Log to check data is inserted on or not on your DataBaseHelper class.
public boolean addData(String name , String sku, String upc , String price, String displaySize, int displaySizeYes, String status){
db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put(Constants.NAME, name );
contentValues.put(Constants.SKU, sku );
contentValues.put(Constants.UPC, upc );
contentValues.put(Constants.PRICE, price );
contentValues.put(Constants.DISPLAY_SIZE, displaySize );
contentValues.put(Constants.DISPLAY_SIZE_YES, displaySizeYes );
contentValues.put(Constants.STATUS, status );
long result = db.insert(Constants.TABLE_NAME,null,contentValues);
Log.e(TAG,""+upc+" Inserted");
if(result == -1) {
return false;
}else{
// Log.e(TAG,"value inserted");
return true;
}
}
public int numberOfRowsRecord() {
db = this.getReadableDatabase();
return (int) DatabaseUtils.queryNumEntries(db, RECORD_TABLE_NAME);
}
Add this code in your helper class
this function return either number of row affected by this...
so you can check you value is inserted or not...
If I use the following code
public boolean updateContact(Integer id, String name, String surname, byte[] image)throws SQLiteException
{
SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put("MyName", name);
contentValues.put("MySurname", surname);
contentValues.put("Photo", image);
db.update("details", contentValues, "_id = ? ", new String[] { Integer.toString(id) } );
return true;
}
then If I ommit to add one column it saves null values , How I can create an update only with those fields that are not empty?
Is it possible to add
db.update("details", contentValues, "_id = ? OR name=? OR surname=? ", new String[] { Integer.toString(id) } );
thank you
Is there any variable that is always set, like id? If so, you just have to check variables name and surname against null before adding them to contentValues:
if (name != null && !name.isEmpty()) {
contentValues.put("MyName", name);
}
I'm familiar with Oracle SQL/PL, but not with SQLite, I have managed to construct my database, with an addition operation. I was however wondering how I could implement a WHERE condition for adding data later on to a specific row.
Here is my addition code:
public void putInformation(DatabaseOperations dop, String name, String pass, String email){
SQLiteDatabase SQ = dop.getWritableDatabase();
ContentValues cv = new ContentValues();
cv.put(TableData.TableInfo.USER_NAME, name);
cv.put(TableData.TableInfo.USER_PASS, pass);
cv.put(TableData.TableInfo.USER_EMAIL, email);
long k = SQ.insert(TableData.TableInfo.TABLE_NAME,null, cv);
Log.d("Database Operations", "One Raw Inserted");
}
I think you are looking for an UPDATE query:
public void updateInformation(DatabaseOperations dop, String name, String pass, String email){
SQLiteDatabase SQ = dop.getWritableDatabase();
ContentValues cv = new ContentValues();
cv.put(TableData.TableInfo.USER_NAME, name);
cv.put(TableData.TableInfo.USER_PASS, pass);
cv.put(TableData.TableInfo.USER_EMAIL, email);
long k = SQ.update(TableData.TableInfo.TABLE_NAME, cv, TableData.TableInfo.USER_NAME+"=?",new String[]{name});
Log.d("Database Operations", k+" Rows Updated");
}
SQLiteDatabase.update docs
WHERE condition for adding data later on to a specific row. If I understand correctly then it will be update for sqlite database. For that you can refer below code.
public void updateInformation(DatabaseOperations dop, String name, String pass, String email){
SQLiteDatabase SQ = dop.getWritableDatabase();
ContentValues cv = new ContentValues();
cv.put(TableData.TableInfo.USER_NAME, name);
cv.put(TableData.TableInfo.USER_PASS, pass);
cv.put(TableData.TableInfo.USER_EMAIL, email);
long index = SQ.update(TableData.TableInfo.TABLE_NAME, cv, TableData.TableInfo.USER_NAME+"=?", new String[] {name});
}
You can't use WHERE condition on insert query. WHERE condition for adding data later on to a specific row. For this you can use UPDATE query.
LINKS:
http://www.tutorialspoint.com/sqlite/sqlite_update_query.htm
http://android-er.blogspot.in/2011/06/edit-row-in-sqlite-database-using.html
I am Creating DB with three fields name,id,mark. I created table and inserted values using
StudentDb.addStudentDetail(new StudentDetails(id,Name,0));
I used Update method Like:
public int updateStudentDetail(StudentDetails student) {
SQLiteDatabase db = this.getWritableDatabase();`enter code here`
ContentValues values = new ContentValues();
values.put("id", student.getID());
values.put("name", student.getName());
values.put("mark", student.getMark());
int i = db.update(TABLE_STUDENT, values, KEY_ID + " = ?",
new String[] { String.valueOf(student.getId()) });
db.close();
return i;
}
I call this method Like:
StudentDb.updateStudentDetail(student);
I don't know how to pass a value update method?
its very simple. Lets say you want to update the marks of a student. Implement this in your DatabaseHelper class:
public int udpateStudentMarks(final int studentID,final int marks){
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put("mark", marks);
int i = db.update(TABLE_STUDENT, values, KEY_ID + " = ?",
new String[] { String.valueOf(studentID) });
db.close();
return i;
}
now you can use it like this:
db.updateStudentMarks(studID,newMarks);
Hope it helps.
Hi I want to update an entry in my DB , I am giving my codes for the updateEntry function below..I want to update the password field I have tried something but it is not working
public String updateEntry(String Password) {
// Create object of content values
ContentValues updatedValues = new ContentValues();
// Assign values for each item
// updatedValues.put("USERNAME", User_name);
updatedValues.put("PASSWORD", Password);
String where = "PASSWORD=?";
db.update("LOGINDETAILS", updatedValues, where,
new String[] { Password });
return Password;
}
and this is the code I have written to update the entry :
String Passwordnew =loginDataBaseAdapter.updateEntry(Confirm_password);
Passwordnew=Confirm_password;
where I want update the password in DB with the confirm_password. I need some good suggestions.
public int UpdateContact(int id,String username,String password) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(USERNAME, username);
values.put(PASSWORD, password);
// updating Row
return db.update(LOGINDETAILS, values, KEY_ID + " = ?",
new String[] { id });
}
call this database function to your Activity
db.UpdateContact("1","dhaval","1234");
public int updateProfile(GetSet profile) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(ID, profile.getUniqueID());
values.put(NAME, profile.getName());
values.put(EMAIL, profile.getEmail());
values.put(DOB, profile.getDob());
values.put(PHONE, profile.getPhone());
values.put(PLACE, profile.getPlace());
// db.close();
// updating row
return db.update(TABLE_NAME, values, ID + " = ?",
new String[] { profile.getUniqueID() });
}
In the corresponding activity:
RemoteDatabase remote = new RemoteDatabase(this);
GetSet profile;
profile = new GetSet(setname, seteid, setphone, setplace,
setdob);
profile.setUniqueID(sdumid);
remote.updateProfile(profile);
remote.close();
Your Logic needs correction, Don't use Password for where clause, use field that is the primary key for your database, maybe username or an auto-increment id.
Then you can update using following code:
public boolean update(int id,String username,String password)
{
ContentValues cv = new ContentValues();
String where = id+"=?";
cv.put(USERNAME, username);
cv.put(PASSWORD, password);
return db.update(TABLE_NAME, cv, where,new int[] { id })>0;
}