Inserting and updating values in sqlite database in android - android

I created database for inserting and updating values which are came from server this is ok.
My problem is when updating my values if new values are came from server how can i mange both inserting and updating at a time. Please provide any suggestions to me. Thanks in Advance.

Your question is pretty broad however, the values that are coming across from the server need to have a unquie identifier if you are wanting to overwrite them later with new values. If you get values coming to you and want to overwrite them later you need to know which ones to overwrite hence you could go straight there with a unique identifier.

This is code for inserting values into database
public void insert(ContentValues values)
{
DataBase_Object.insert(TABLE_Name , null, values);
}
This is code for Update values into database
public void putQty(ContentValues values)
{
DataBase_Object.update(TABLE_Name, values ,
Where Condition, null);
}
Hope it helps.

I believe that you are asking how to insert new rows or overwrite your existing rows in one step. While that is possible in a single raw SQL as discussed in this answer, I found that it easier to do this in two steps in Android and preserving values in all your columns.
I posted my answer here.

Related

android sqlite have gap after delete row?

I have 4 EditTexts in dataBase
I delete the second one
The database is numbered 1,3,4
I now want it to be renumbered as 1,2,3.
I looked at many solutions
But I could not find a complete answer..
I do not know what should be in writing
I tried the following code .
But it did not happen
db.delete("SQLITE_SEQUENCE","NAME = ?",new String[]{TABLE_NAME});
Does anyone have the full answer ?
In general, this is poor design for a database. An ID should be consistent for a piece of data, regardless of its peers--it should not fluctuate just because another row was deleted.
That being said, you could always use an UPDATE statement to SET ID = ID-1 WHERE ID > (the ID of the row you deleted). This would work. I don't recommend this approach, but you can.
You can recover this space by executing the SQLite VACUUM command.
Call db.execSQL("VACUUM") after delete

Validate for empty row in Android

I have a table in which 5 rows are there. But I have to feed the value of row in the database in which values are filled. It can be 2nd, 3rd or all. So how would I find out and validate the same. The rows are not generating dynamically. The rows are already in layout with some ids. I know it's weird but requirement is this only. Please suggest me how can I do this.
For that you have some alternative,
I think you should insert default record into the field so that will be easy instead of validating, if you think that is good.
When get the result from the DB just put the single condition
String name = Db.get_name().toString();
if(name!=null)
{
Row_TextView.setText(name);
}
else
{
Row_TextView.setText("-"); // or what ever you want to set
}
- Here is database example
Try it i hope it works for you
What you need is a Cursor to read data from the db
Then loop through the fetched data (if 'getCount()>0' then while(!cursor.isAfterLast))
For each row check if the data is present in specific columns in your db and if not then do insert it in the db

Update ListView based on SQLite backed ContentProvider

I'm a new Android Developer and seem to have gotten in a little over my head. I am trying to make a listView update when I add more content to the list.
The ListView is based off of a SQLite database. I was able to get the ListView to be based on the SQLite database by making a ContentProvider for the SQLite database (which was suggested here). Now my issue is that I want to update the SQLite database and have it reflected on the ListView. I am using a loader and according to this if I implemented the loader right it will monitor the data.
I tried updating the SQLite database directly but, that didn't cause the ListView to update without closing and reopening. My instinct from there is that I should implement the insert method in my contentProvider. I did a very simple implementation:
#Override
public Uri insert(Uri uri, ContentValues contentValues) {
return ContentUris.withAppendedId(uri, mCollectionDB.insertCollection(contentValues));
}
Unfortunately, the result of this is my app crashes with a "java.lang.NullPointerException". This is especially confusing as using the exact same contentValues to make the .insertCollection call from my MainActivity works without issue.
The issue I'm really interested in is how to get my listView to update when I insert data into my SQLite database. If inserting into the ContentProvider is irrelevant then please ignore that. I'm not really sure where I went wrong so I'm not sure what other code may be useful, but I'm happy to edit in more code if it'll help.
You can check loader concept.
You can start with
http://www.vogella.com/articles/AndroidSQLite/article.html#todo
Following are other 2 good tutorials
http://mobile.tutsplus.com/tutorials/android/android-sdk_content-providers/
http://mobile.tutsplus.com/tutorials/android/android-sdk_loading-data_cursorloader/
Hey here is one example to insert data into sqlite database and display it in list view.
Have a look at it
The idea is simple insert data into database and on click of view button initialize List view with arraylist that contains data already inserted.
You can ask if you have any further queries.

Programatically update SQLite database in android?

I have around 2600 entries in my SQLite database. I need to delete all those entries and put in updated entries. I know that the database can be updated by changing the database version in the DatabaseHandler class, but that can only be done if the whole app is updated. I need to programatically update the database. Can this be done? I searched but couldn't find a satisfactory answer. Thanks.
I guess you can call a method some what like this
public void emptyDatabase() {
db = this.getWritableDatabase();
db.execSQL("DELETE FROM " + TABLE_Your_Tbl);
}
Even this will be able to delete those values quickly. Without updating the version number.
Plus as you mentioned that you have to delete the records and add new values.
It will be much faster that updating those values.

How to prevent sqlite primary key from autoincrementing on update?

So, the issue is simple. I am updating into a table, and the primary key is autoincrementing... This means that all related tables are screwed over. I am using sqlite, so I don't think there is any inherent mechanism that can catch this and related tables.
Due to a lack of support for foreign keys in the sqlite versions I must work with, I force the relations by adding the primary key returned from an insert into the parent table into the related tables when entering new data.
Is there an alternative to updating all related tables each time I do an update? Please say yes..
Edit: last edit was wrong, I got confused. I have simply pasted the code governing inserts/replacements below. I am replacing into, not updating into, typing error on my part.
//inserts
public long insertIntoCompany(String company, int subs) {
ContentValues cv = new ContentValues();
cv.put(company_column, company);
cv.put(company_subscribed_column, subs);
return db.replace(company_table, null, cv);
}
You should actually be using the SQL update query. See this answer.
If you paste your query we might be able to offer more help.

Categories

Resources