I am using a prebuilt database stored in sdcard.I wanted to insert some values in that. while running insert code 0 byte journal file is building in sdcard but data intended to insert is not visible when i am looking into the database,why??? plz help..there is no error or warning.
public void saveRecords(String site_reading, String Demand,String remarks){
SQLiteDatabase mDb = mDbHelper.getWritableDatabase();
ContentValues values = new ContentValues();
values.put("site_reading", site_reading); // inserting a string
values.put("demand", Demand); // inserting an int
values.put("remark", remarks);
Log.e("in save records", site_reading + remarks + Demand);
mDb.beginTransaction();
mDb.insert("mst_mrt_1", null, values);
mDb.setTransactionSuccessful();
mDb.endTransaction();
}
And I am calling above method in an another Class as
public void add_data(View v){
d_name = veri.dataname;
TestAdapter mDbHelper = new TestAdapter(this,d_name);
mDbHelper.createDatabase();
String reading1 = info_reading.getText().toString();
String Demand1 = info_demand.getText().toString();
String remark1 = list.getSelectedItem().toString();
mDbHelper.saveRecords(reading1,Demand1, remark1);
mDbHelper.close();
}
Related
Im using the following code to add values to db.
public void addComplianceAcceptability(ComplianceAcceptability complianceAcceptability) {
SQLiteDatabase sqLiteDatabase = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put(KEY_ID, complianceAcceptability.getId());
contentValues.put(KEY_COUNTRY_ID, complianceAcceptability.getCountryId());
contentValues.put(KEY_DOMAIN_ID, complianceAcceptability.getDomainId());
contentValues.put(KEY_UNIT_ID, complianceAcceptability.getUnitId());
contentValues.put(KEY_COMPLIANCE_ID, complianceAcceptability.getComplianceId());
contentValues.put(KEY_COMPLIANCE_NAME, complianceAcceptability.getCompliancenName());
contentValues.put(KEY_COMPLIANCE_FREQUENCY, complianceAcceptability.getComplianceFrequency());
contentValues.put(KEY_COMPLIANCE_APPLICABLE, complianceAcceptability.getComplianceApplicable());
contentValues.put(KEY_COMPLIANCE_OPTED, complianceAcceptability.getComplianceOpted());
sqLiteDatabase.insert(TABLE_COMPLIANCE_ACCEPTABILITY, null, contentValues);
sqLiteDatabase.close();
}
Imm trying to access the values using the following code,
public List<ComplianceAcceptability> getAllAcceptability() {
List<ComplianceAcceptability> complianceAcceptabilityList = new ArrayList<>();
String selectQuery = "SELECT * FROM " + TABLE_COMPLIANCE_ACCEPTABILITY;
SQLiteDatabase db = this.getWritableDatabase();
Cursor cursor = db.rawQuery(selectQuery, null);
if (cursor.moveToFirst()) {
do {
ComplianceAcceptability complianceAcceptability = new ComplianceAcceptability();
complianceAcceptability.setId(Integer.parseInt(cursor.getString(0)));
complianceAcceptability.setCountryId(cursor.getString(1));
complianceAcceptability.setDomainId(cursor.getString(2));
complianceAcceptability.setUnitId(cursor.getString(3));
complianceAcceptability.setComplianceId(cursor.getString(4));
complianceAcceptability.setCompliancenName(cursor.getString(5));
complianceAcceptability.setComplianceFrequency(cursor.getString(6));
complianceAcceptability.setComplianceApplicable(cursor.getString(7));
complianceAcceptability.setComplianceOpted(cursor.getString(8));
} while (cursor.moveToNext());
}
return complianceAcceptabilityList;
}
The problem is that it returns an empty array. But when I try to print the contentValues within addComplianceStatus, it of course prints the array of all values.
You never add anything to complianceAcceptabilityList. You just assign it once, then do nothing to it before returning it. Maybe you want to add complianceAcceptability into it at each iteration through the cursor?
Here you are not adding the complianceAcceptability object to the complianceAcceptabilityList, your modified code:
if (cursor.moveToFirst()) {
do {
ComplianceAcceptability complianceAcceptability = new ComplianceAcceptability();
complianceAcceptability.setId(Integer.parseInt(cursor.getString(0)));
complianceAcceptability.setCountryId(cursor.getString(1));
complianceAcceptability.setDomainId(cursor.getString(2));
complianceAcceptability.setUnitId(cursor.getString(3));
complianceAcceptability.setComplianceId(cursor.getString(4));
complianceAcceptability.setCompliancenName(cursor.getString(5));
complianceAcceptability.setComplianceFrequency(cursor.getString(6));
complianceAcceptability.setComplianceApplicable(cursor.getString(7));
complianceAcceptability.setComplianceOpted(cursor.getString(8));
complianceAcceptabilityList.add(complianceAcceptability);
} while (cursor.moveToNext());
}
I want to update the row values with 0 or 1. Here is my update method.Where is wrong with my code .
dbhelper.updateStarFlag(actid, strStarFlagMark);
public boolean updateStarFlag(String str_ActivityId, String str_StarFlag)
{
SQLiteDatabase db1 = getReadableDatabase();
ContentValues args = new ContentValues();
args.put(COL_AllPost_PostActivityId, str_ActivityId);
args.put(COL_AllPost_StarFlag, str_StarFlag);
int i = db1.update(Table_AllPost_Table, args, COL_AllPost_PostActivityId + "='"+str_ActivityId+"'", null);
return i > 0;
}
If you want to update something, you need a writable database. I changed the query to look a little more like in the documentation for SQLiteDatabase
public boolean updateStarFlag(String str_ActivityId, String str_StarFlag)
{
SQLiteDatabase db1 = getWriteableDatabase();
ContentValues args = new ContentValues();
args.put(COL_AllPost_PostActivityId, str_ActivityId);
args.put(COL_AllPost_StarFlag, str_StarFlag);
int i = db1.update(Table_AllPost_Table, args, COL_AllPost_PostActivityId + "= ? ", new String[]{str_ActivityId});
return i > 0;
}
To update something in database, you need writeable database, not just readable.
Change below line:
SQLiteDatabase db1 = getReadableDatabase();
to :
SQLiteDatabase db1 = getWriteableDatabase();
Hope, this helps you.
I'm having some trouble saving a string with an accent to my database and retrieving it.
This is my function that saves a new location to the database. It gets 'myId' and 'location' and inserts them. The println there shows the item as I expect, with the accent. The example I'm using is Mazzarrà.
public long createLocationRecord(String location, int myId) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(KEY_OWMID, myId);
values.put(KEY_NAME, location);
System.out.println("newItem in DB = "+location);
long callSQL = db.insertWithOnConflict(TABLE_LOCATION, null, values, SQLiteDatabase.CONFLICT_IGNORE);
if(callSQL==-1)
db.update(TABLE_LOCATION, values, KEY_OWMID + '=' + owmId, null);
return callSQL;
}
This is my function to retrieve all location items. The println here prints out Mezzarra, without the accented à. Am I missing something? Do I need to make a change to my database? It's just a regular Android SQLite DB that I'm opening via SQLiteBrowser.
public ArrayList<String> getLocationList() {
ArrayList<String> list = new ArrayList<String>();
SQLiteDatabase db = this.getWritableDatabase();
String selectQuery = "SELECT * "
+ "FROM " + TABLE_LOCATION
+ " ORDER BY _id ASC";
Cursor c = db.rawQuery(selectQuery, null);
if (c != null)
c.moveToFirst();
if (c.moveToFirst()) {
do {
System.out.println("newItem GETTING FROM DB = "+c.getString(c.getColumnIndex(KEY_NAME)));
list.add(c.getString(c.getColumnIndex(KEY_NAME)));
} while (c.moveToNext());
}
c.close();
return list;
}
Thanks for any help anyone can provide.
Hi i have issue with database SQLITE android
please check my code for reference
when i am going to insert values it returns -1 that i come to know from debug
which is not inserting values so tell me what is wrong
i am inserting value from list view.
public void add_device(String data,
ArrayList<HashMap<String, String>> jsonlist) {
try {
SQLiteDatabase db = this.getReadableDatabase();
// database = this.getWritableDatabase();
for (HashMap<String, String> map : jsonlist) {
ContentValues values = new ContentValues();
values.put(SAVE_COLUMN_NAME, data);
values.put(SAVE_COLUMN_KEY, map.get(SAVE_COLUMN_KEY));
values.put(SAVE_COLUMN_VALUE, map.get(SAVE_COLUMN_VALUE));
#SuppressWarnings("unused")
Long int1 = db.insert(SAVE_TABLE_NAME, null, values);
Log.i("insserted value ", int1 + "");
}
}
/*
* for(HashMap<String, String> map : mylist){ ContentValues cv = new
* ContentValues(); cv.put(FILE_NAME, map.get(FILE_NAME)); cv.put(DESC,
* map.get(DESC)); cv.put(UPLOADED_BY, map.get(DATE_UPLOADED));
* cv.put(ACTION, map.get(FILE_NAME)); cv.put(ID, map.get(ID));
* cv.put(FILE_URI, map.get(FILE_URI)); db.insert("tablename", null,
* cv); }
*/
catch (Exception e) {
// TODO: handle exception
}
}
thanks in advance
I am guessing that the "-1" value is coming from your Log.i(...) message, so the -1 is the return value of the db.insert(...) call. The -1 indicates an error occurred.
Instead of using insert, user insertOrThrow(...) and look at the exception for clues as to why there is a problem.
-1 is returned when you are inserting the records.
This can be due to violation of table properties (like conflict between type of data you are inserting and the type of column in table ,is "key" and "Value" column in table are of string type) also there can be several other reasons for this like you may be missing any table column value in insert operation or value of 'SAVE_COLUMN_NAME','SAVE_COLUMN_KEY','SAVE_COLUMN_VALUE' doesn't matches with respective column names in table .
[while running app open separate command prompt and write adb logcat ) and show the result here (specifically when you try to insert the record) so that we have more information related to issue]
[EDIT]
OK so i think i have found the problem
SQLiteDatabase db = this.getReadableDatabase();
and you are inserting statement
Long int1 = db.insert(SAVE_TABLE_NAME, null, values);
INSERT SHOULD BE DONE USING WRITABLE DATABASE NOT READABLE DATABASE.
Here SQLiteDatabase db is a Readable Database it should be a Writable database
change
SQLiteDatabase db = this.getReadableDatabase();
to
SQLiteDatabase db = this.getWritableDatabase();
I have made changes like
public void add_device(String data,ArrayList<HashMap<String, String>> jsonlist) {
try {
database = this.getWritableDatabase();
for (HashMap<String, String> map : jsonlist) {
ContentValues values = new ContentValues();
values.put(SAVE_COLUMN_NAME, data);
values.put(SAVE_COLUMN_KEY, map.get(SAVE_COLUMN_KEY));
values.put(SAVE_COLUMN_VALUE, map.get(SAVE_COLUMN_VALUE));
Long int1 = database.insertOrThrow(SAVE_TABLE_NAME, null,
values);
Log.i("insserted value ", int1 + "");
}
}
/*
* for(HashMap<String, String> map : mylist){ ContentValues cv = new
* ContentValues(); cv.put(FILE_NAME, map.get(FILE_NAME)); cv.put(DESC,
* map.get(DESC)); cv.put(UPLOADED_BY, map.get(DATE_UPLOADED));
* cv.put(ACTION, map.get(FILE_NAME)); cv.put(ID, map.get(ID));
* cv.put(FILE_URI, map.get(FILE_URI)); db.insert("tablename", null,
* cv); }
*/
catch (Exception e) {
}
}
I am new to android , as I am trying to update data using context menu item "EDIT". Using EDIT I will go to EditText layout then I will press update Note, But it's not updating the EditText Data.
My Code Database:
public void updateNote(String filename, String filedata, String duedate){
SQLiteDatabase db = this.getWritableDatabase();
ContentValues updatenote = new ContentValues();
updatenote.put(Col_File_Name, filename);
updatenote.put(Col_File_Data, filedata);
updatenote.put(Col_Due_Date, duedate);
db.update(Table_Notepad, updatenote, Col_ID + "=?", null);
db.close();
}
Activity:
case R.id.Save:
String get_title = Title_Edit.getText().toString();
String get_content = Content_Edit.getText().toString();
String get_duedate = duedatetext.getText().toString();
if(!get_title.equals("") || !get_content.equals(""))
{
if (!isEdit) {
//if it isn't edit mode we just add a new note to db
Database_Notepad db = new Database_Notepad(Add_Task.this);
db.Create_Note(get_title, get_content, get_duedate);
} else {
//if this is edit mode, we just update the old note
Database_Notepad db = new Database_Notepad(Add_Task.this);
db.updateNote(get_title, get_content, get_duedate);
db.close();
}
}
Please Suggest, I think I need to correct database code. Please suggest. Thanks
Your function updateNote need Record ID which you want to update.
Like
public void updateNote(int id, String filename, String filedata, String duedate){
instead
public void updateNote(String filename, String filedata, String duedate){
and compare that id which your entire record, it will check and update your particular record, change your function function something like below:
public void updateNote(int id, String filename, String filedata, String duedate){
SQLiteDatabase db = this.getWritableDatabase();
ContentValues updatenote = new ContentValues();
updatenote.put(Col_File_Name, filename);
updatenote.put(Col_File_Data, filedata);
updatenote.put(Col_Due_Date, duedate);
db.update(Table_Notepad, updatenote, "rowid= "+id,, null);// rowid or name of your id column
db.close();
}
and pass the id too along with others parameter.
Use
db.updateNote(id, get_title, get_content, get_duedate);
instead
db.updateNote(get_title, get_content, get_duedate);
In updateNode(), you need to change this update or insert values:
public void updateNote(long rowId, String filename, String filedata, String duedate){
SQLiteDatabase db = this.getWritableDatabase();
ContentValues updatenote = new ContentValues();
updatenote.put(Col_File_Name, filename);
updatenote.put(Col_File_Data, filedata);
updatenote.put(Col_Due_Date, duedate);
if (rowId >=0)
db.update(Table_Notepad, updatenote, Col_ID + "=?", new String[] {""+rowId});
else
db.insert(Table_Notepad, null, updatenote);
db.close();
}
Then, if you are creating a new note you can call:
updateNote(-1, filename, filedata, duedate);
and if you are updating an existing one, you will have to pass in the ID:
updateNote(noteRowId, filename, filedata, duedate);.
Source: check these Android docs.
Need to pass row id to update in last argument
db.update(Table_Notepad, updatenote, Col_ID + "=?", new String[]{id});
Happy coding