Hello everybody I would like to insert multiple records to my sqlite table at once,
This is what I've tried:
DBHelper.class
public void insertToOUTTRANS(ArrayList<String> arrListDocumentNumber){
ContentValues cv = new ContentValues();
for(String text : arrListDocumentNumber){
cv.put(KEY_DOCUMENTNUMBER, text);
}
myDataBase.insert(TBL_OUTTRANS, cv, null);
}
But I'm getting error in this line:
myDataBase.insert(TBL_OUTTRANS, cv, null);
Error says:
The method insert(String, String, ContentValues) in the type SQLiteDatabase is not applicable for the arguments (String, ContentValues, null)
MainActivity.class:
for(int i = 0; i< arrDocumentNumber.length; i++) {
ArrayList<String> arrListDocumentNumber = new ArrayList<String>();
ArrayList<String> arrListUnitOfMeasure = new ArrayList<String>();
ArrayList<String> arrListLocationCode = new ArrayList<String>();
arrListDocumentNumber.put(tvItemCode.getText().toString());
arrListUnitOfMeasure.put(editText1.getText().toString));
arrListLocationCode.put(editText1.getText().toString));
}
But I'm kinda confused how to loop over the cursor to check for all items from the listview to save to my table. Any ideas? I would gladly appreciate your help. Thanks.
Try this
for(int i=0;i<arrListDocumentNumber.size();i++)
{
ContentValues initialValues=new ContentValues();
initialValues.put(KEY_DOCUMENTNUMBER, arrListDocumentNumber.get(i));
db.insert(TBL_OUTTRANS, null, initialValues);
}
The insert methods looks like..
insert(String, String, ContentValues)
So you need to pass arguments like..
myDataBase.insert(TBL_OUTTRANS, null, cv);
Check syntax here.
myDataBase.insert(TBL_OUTTRANS, null, cv);
instead of
myDataBase.insert(TBL_OUTTRANS, cv, null);
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 a field in my database, but the changes not saved to database.
I use The "update" method in database class but not work
Please
This is my Method
public void myupdatedb(int id,String value){
ContentValues cv = new ContentValues();
cv.put("fav", "1");
mydb.update("contents", cv, "id" + "=" + id , null);
}
and in the MainActivity:
db.myupdatedb(1,"1");
From Android documentation
public int update (String table, ContentValues values, String whereClause, String[] whereArgs)
You may include ?s in the where clause, which will be replaced by the values from whereArgs. The values will be bound as Strings.
Try
mydb.update("contents", cv, "id = ?", new String[]{Integer.toString(id)});
I can't find anything about this on the web.
I need to edit all values in a column in my database but I can't find a way.
I'm doing:
if (upgradeCursor.moveToFirst()){
do {
db.editListsColumn("0");
} while (upgradeCursor.moveToNext());
}
Where upgradeCursor:
return mDb.query(ProductsData.PRODUCTS_TABLE, null, null, null, null, null, null);
And editListsColumn("0"):
public void editListsColumn(String lists) {
mDb=mDbHelper.getWritableDatabase();
ContentValues cv = new ContentValues();
cv.put(ProductsMetaData.PRODUCT_PRODUCT, lists);
mDb.update(ProductsData.PRODUCTS_TABLE, cv, "lists=?", null);
}
I'm stuck here.
Any suggestion would be really appreciated at the moment! Thank you so much.
Have you tried a simple update without the where clause and without iterating the cursor. What I mean is simply call this:
mDb=mDbHelper.getWritableDatabase();
ContentValues cv = new ContentValues();
cv.put(ProductsMetaData.PRODUCT_PRODUCT, lists);
mDb.update(ProductsData.PRODUCTS_TABLE, cv, null, null);
Without:
if (upgradeCursor.moveToFirst()){
do {
db.editListsColumn("0");
} while (upgradeCursor.moveToNext());
}
From the documentation of the update(String table, ContentValues values, String whereClause, String[] whereArgs) method: passing NULL in whereClause will update all rows.
http://developer.android.com/reference/android/database/sqlite/SQLiteDatabase.html
In my Android App, I am trying to insert names in a column forcefully into database but In database only last name is showing i.e. Myself because names are overlapping. Actually My database portion is weak.
This is what I am doing in my Database Handler Class,
public void addPoliticianNames()
{
ContentValues values=new ContentValues();
values.put(KEY_POLITICIANNAMES, "I");
values.put(KEY_POLITICIANNAMES, "Me");
values.put(KEY_POLITICIANNAMES, "Myself");
db.insert(TABLE_POLLINGVOTES, null, values);
}
What to do for getting these names sequentially into the column (Here I have to insert names forcefully as I have mentioned above). So, Is there any method for this ? Is there any role of cursor in doing this job (like movetonext method or something like that) ?
Please help me,
Thanks.
you have to make one loop for inserting data into database. it will pick one by one value from array and put it into database.
Put this method in your database class
public void deleteTable()
{
database.delete(TABLE_POLLINGVOTES, null, null);
}
db.deleteTable(); // call it with this line for delete all records of your table
// for inserting data into table.
String[] a = {"I","Me","MySelf"};
ContentValues values = new ContentValues();
for (int i = 0; i < a.length; i++)
{
values.put(KEY_POLITICIANNAMES, a[i]);
db.insert(TABLE_POLLINGVOTES, null, values);
}
Hope it will help you.
If you want to insert three records, you have to use three insert calls.
For example:
ContentValues values=new ContentValues();
values.put(KEY_POLITICIANNAMES, "I");
db.insert(TABLE_POLLINGVOTES, null, values);
values.put(KEY_POLITICIANNAMES, "Me");
db.insert(TABLE_POLLINGVOTES, null, values);
values.put(KEY_POLITICIANNAMES, "Myself");
db.insert(TABLE_POLLINGVOTES, null, values);
#Shiv change the column name for every value
public void addPoliticianNames(String ss)
{
ContentValues values=new ContentValues();
values.put(KEY_POLITICIANNAMES, ss);
}
ss contain value firts time "I",second time "me" like that
as the title states I am having a little trouble linking my data from a database to a listview, the problem being that the adapter will set only the last row returned, as opposed to each row from the database table.
An example of what I am trying to achieve:
TABLE ANIMAL:
Monkey
Cat
Dog
Tiger
will show up as only Tiger on screen.
My method returning cursor from the database:
public Cursor retrieveAnimals(){
return = DB.query(ANIMAL_TABLE, new String[] {
KEY_ROWID,
KEY_ANIMALNAME,
},
null,
null,
null,
null,
null);
}
and setting the listview
dbm = new MyDBManager(this);
dbm.open();
dbm.deleteAnimals();
dbm.populateDB();
// after populating, set the adapter..
myCursor = dbm.getAllAnimals();
String[] columns = new String[] {"animal_name"};
int[] to = new int[] {R.id.animal};
SimpleCursorAdapter mAdapter = new SimpleCursorAdapter(this, R.layout.row, myCursor, columns, to);
this.setListAdapter(mAdapter);
I have a feeling my problem lies in the cursor position, in that it is moving automatically to the last row when setting the adapter, I have been trying to find a solution to my problem but with no luck.
Thanks in advance for any suggestions.
EDIT: Method to populate DB
public long populateDB(){
ContentValues initialValues = new ContentValues();
for(int i = 0; i < animalName.length; i++){
initialValues.put(KEY_ANIMALNAME, animalName[i]);
}
return DB.insert(ANIMAL_TABLE, null, initialValues);
}
In your retrieveAnimals(), the line should be return DB.query(...) (note the lack of an = between return and DB.query).
Apart from that your code looks fine. You should call startManagingCursor on the cursor (after calling getAllAnimals) to avoid memory leaks.
Edit: change your populate method to:
public long[] populateDB(){
ContentValues initialValues = new ContentValues();
long[] rowIds = new long[animalName.length];
for(int i = 0; i < animalName.length; i++){
// here we are using a single ContentValues object and inserting it
// into the DB before changing the ContentValues and inserting it again
initialValues.put(KEY_ANIMALNAME, animalName[i]);
rowIds[i] = DB.insert(ANIMAL_TABLE, null, initialValues);
}
return rowIds;
}
Alternatively (more memory usage, but useful perhaps if you need to modify some of the records before inserting them):
public void populateDB(){
ContentValues[] initialValues = new ContentValues[animalName.length];
for(int i = 0; i < animalName.length; i++){
// create a new ContentValues for each object
initialValues[i] = new ContentValues();
initialValues[i].put(KEY_ANIMALNAME, animalName[i]);
}
// now our contentvalues is filled we can insert them all
for(int i = 0; i < animalName.length; i++){
DB.insert(ANIMAL_TABLE, null, initialValues[i]);
}
}
Did you try
myCursor.moveToFirst()