Android split function behavior - android

There is a txt file that looks like:
It is being splitted by a delimiter ":" well:
temp = strLine.split(":");
ContentValues initialValues = new ContentValues();
initialValues.put(TITLE, temp[0].trim());
initialValues.put(DESCRIPTION, temp[1].trim());
initialValues.put(GROUP, temp[2].trim());
initialValues.put(COL1, temp[3].trim());
initialValues.put(COL2, temp[4].trim());
initialValues.put(COL3, temp[5].trim());
initialValues.put(ADDRESS, temp[6].trim());
db.insert(DATABASE_TABLE, null, initialValues);
}
As you can see this script fills the database. After that I'm displaying a list of data retrieved from database.
But when the .txt does not contain data for ADDRESS field:
Then inserting function returns "ArrayIndexOutOfBoundsException" and the cursor becomes null.
It's OK for database to have null in ADDRESS column, so how to overcome this issue in initialValues.put(ADDRESS, temp[6].trim());
What can you suggest? Thanks

// try to replace this line
initialValues.put(ADDRESS, temp.length>6 ? temp[6].trim():"");

You can put condition for Address
temp = strLine.split(":");
ContentValues initialValues = new ContentValues();
initialValues.put(TITLE, temp[0].trim());
initialValues.put(DESCRIPTION, temp[1].trim());
initialValues.put(GROUP, temp[2].trim());
initialValues.put(COL1, temp[3].trim());
initialValues.put(COL2, temp[4].trim());
initialValues.put(COL3, temp[5].trim());
if(temp.length==7)
{
initialValues.put(ADDRESS, temp[6].trim());
}
else
{
initialValues.put(ADDRESS, "");
}
db.insert(DATABASE_TABLE, null, initialValues);

Related

Updating rows in Android SQlite

I'm using SQlite in my Android app and the task is - how can I update all the rows in one table?
I have a 1st column with name "cl_id" (integer numbers 1-2-3-4..) and after deleting some rows, I wan't to make a cycle to fill this 1st column with a new values to keep them in right order.
I was trying to execute:
data.put("cl_id",index);
db.update("mdb_table_contactList", data, null, null);
but it's updates all the values in the first column :(
To update the field in specific row, try this;
ContentValues data = new ContentValues();
data.put("cl_id",index);
db.update("mdb_table_contactList", data, "id="+_id, null);
db.close();
It's good practice to have a primary key for each row data.
Here _id would be your primary key.
"just give rowId and type of data that is going to be update in ContentValues."
public void updateStatus(String id , int status){
SQLiteDatabase db = this.getWritableDatabase();
ContentValues data = new ContentValues();
data.put("status", status);
db.update(TableName, data, "columnName" + " = "+id , null);
}
Try this it must work
void updateDatail(String id) {
ContentValues data = new ContentValues();
data.put("cl_id",index);
db.update("mdb_table_contactList", data, "id=?", new String[]{id});
db.close();
}

Edit all the values in a column in SQL

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

Names are getting overlapped in SQLite

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

Sqlite open helper insert else update?

I want to insert data successfully
Here is my code:
public void insertData(String strTableName,
ArrayList<HashMap<String, String>> arrListproductdatabase) {
db = this.getWritableDatabase();
ContentValues cv = new ContentValues();
for (int i = 0; i < arrListproductdatabase.size(); i++) {
// cv.put(columnName, arrListOfRecord.get(i).get("name"));
cv.put(columnproductname,
arrListproductdatabase.get(i).get("product"));
cv.put(columnproductprice,
arrListproductdatabase.get(i).get("price"));
cv.put(columnproductquantity,
arrListproductdatabase.get(i).get("quantity"));
cv.put(columnproductid,
arrListproductdatabase.get(i).get("productID"));
cv.put(columnresturantID,
arrListproductdatabase.get(i).get("resturantID"));
db.insert(strTableName, null, cv);
}
I want that when I have to press add button again, that time it should check if the product is already inserted, and in that condition it should update and all.
I don't want to create any duplicate value.
Any help would be appreciated!
you can check for the distinct values in the db. please follow the link to have more details
android check duplicate values before inserting data into database
Set 'Product' field as unique key. So when duplicate value arrives from standard insert, it will simply return -1 and the error message will be swallowed.
You can control the behavior by using insertWithOnConflict (String table, String nullColumnHack, ContentValues initialValues, int conflictAlgorithm) where you also specify a conflict algorithm that can be of values:
CONFLICT_ABORT
CONFLICT_FAIL
CONFLICT_IGNORE
CONFLICT_REPLACE
CONFLICT_ROLLBACK
Check out the reference for descrption of the conflict resolution types.
There is also an updateWithOnConflict
You can do that like this :
public boolean checkProduct(String product){
// shold open database here
Cursor mCursor =
db.query(true, DATABASE_TABLE, null, "product='" + product+"'", null, null, null, null, null);
if(mCursor != null){
mCursor.close();
// shold close database here
return true;
}
// shold close database first here also
return false;
}
Hope this helped you.

Data not inserting in Sqlite database in android

I am developing an application which requires use of an SQLite database. I have implemented fetching the data, but I am facing problems when I try to insert data. The problem that I am having is that the new data I enter is not stored, i.e nothing is new is being entered into the database.
This is my insert code, myDataBase is an instance of SQLiteDatabase.
public void insertTitle(String Recipe)
{
ContentValues initialValues = new ContentValues();
initialValues.put(COLUMN_NAME,value);
myDataBase.insert(ZRECIPE, null, initialValues);
}
Try it this way. You need first start transaction, then mark it as successful and end.
try {
myDataBase.beginTransaction();
myDataBase.insert(ZRECIPE, null, initialValues);
myDataBase.setTransactionSuccessful();
}
finally {
myDataBase.endTransaction();
}
You forgot to commit the transaction.
Try with this code this may help you
public void insertTitle(String Recipe)
{
ContentValues initialValues = new ContentValues();
initialValues.put(COLUMN_NAME,Recipe);
myDataBase.insert(ZRECIPE, null, initialValues);
}

Categories

Resources