Validate for empty row in Android - 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

Related

Update the database table with the same button that is used to insert

I have a really different situation. I have a form where user fills the data and insert into database. Now in the same form, I need to update his data also. Something like, there is a search user autocomplete text view. When user finds him then the data from database directly fills all the form fields. Now, either he submits data with the same info, if his info has not changed or, if the info has changed, he changes only some of the fields and then press the same button(submit) to update his data. I am doing so because I need to register the user in different particular session. He can register many times with same data. But when his some of the data is changed, I need to update his data but with registering him in the session with new id.
I do not want to provide you my codes here, I just need some technical help, how can I achieve the solution of this problem? If I am not wrong, I am thinking to use TextWatcher. I can implement textwatcher in each edittext form. When the text is changed, somehow the button(submit) that is used to insert get some connection with textwatcher, that it should update the database instead insert.
I am not sure if I am right. Help me please if you have some easy and reliable method to do. Thanks!!
You must have created a method in the Activity or a method in a separate class(I would prefer a separate class for database related methods). And you must keep some registration id or something which should match every time.
Suppose the registration id is named as ID. When you are inserting the data check within that insert method whether this ID matches with any previous records or not. If it matches then do update instead of insert.
Something like:
SQLiteDatabase objSqliteDB = DatabaseHelper.openDataBase();
objSqliteDB.beginTransaction();
SQLiteStatement stmtRecCount = objSqliteDB.compileStatement("select count(*) from ID where ID =?");
stmtRecCount.bindString(1, ID);
long count = stmtRecCount.simpleQueryForLong();
if(count != 0)
{
stmtUpdate.bindString(1, Name);
. . .
stmtUpdate.execute();
}
else
{
stmtInsert.bindString(1, Name);
. . .
stmtInsert.executeInsert();
}
Add whatever other fields you need to update
stmtInsert
&
stmtUpdate
will be yourinsert and update query

Delete row in sqlite database

I have a sqlite database which store different multiple user's data. I wanted to ask how to delete specific row in the database? I'm not really familiar with database type of stuff. Currently I have a delete function that used to delete one of the user's information in the database. But I'm not really sure how to delete the entire row. (I got emailid, first & last name and ssid in my database. )
public void delete_user(String ssid) {
Log.i(TAG,"delete_user ssid["+ssid+"]...");
String[] valuesWhere = new String[1];
valuesWhere[0] = ssid;
this.getWritableDatabase().delete("user", "ssid=?", valuesWhere);
}
Should I declare every value that I wanted to delete? I wanted to know whether is there another way that able to delete specific row of data. Any comments will be appreciated.
Should I declare every value that I wanted to delete? I wanted to know whether is there another way that able to delete specific row of data.
Your current code will delete every row that matches the ssid passed to delete_user(ssid). If ssid is a unique column (no duplicate values) it will delete one row at most.
But I'm not really sure how to delete the entire row.
Understand that SQLiteDatabase#delete() will never partially delete a row.

Managing a sqlite database using activity list

Ok, I have a database with id column as timestamp
I made an activity list from the db.
I want to manage the db (delete rows) using the list, but the thing is I don't want to
View the whole timestamp, in every row I'll put only the time with some info and
I want to group the list ,as in contacts grouped by alphabet, by the date.
First, how can I make group in an activity list? (Making groups to the output list not the db)
Second, what is the best way to implement this? When user chooses an item and confims delete
I should delete it from the db but I have only patial timestamp...
(My only link to the db is the timestamp - I don't actually know where to store it in the list and I don't want to put it as a string in the text view, do a substring to get it back - is there another way to do this?)
I tried to search tthe web for some examples but I only found a simple ones.
Thnx :-)
?
I think what you're trying to do is create a database of tasks identified by a timestamp. You probably don't want to use a timestamp as a unique ID for the row. Instead, use an integer and qualify it as "PRIMARY KEY" when you create the database.
group the list? I'm not sure why you want to do this in the structure of the database. It's more common to group the list in the output, and leave the db itself in as flat a structure as possible.
Retrieve the primary key when you display a list of tasks. When the user clicks a task, use the primary key to choose the task to delete. You don't have to display the primary key; it serves as a behind-the-scenes "link" between the displayed info and the db row.
http://www.vogella.com/articles/AndroidListView/article.html
I should use cursor adapter for managing db.
And this one for grouping a list:
http://code.google.com/p/android-amazing-listview/
Thnx for the efforts

Changing row data in SQLite table, without deleting and re-inserting updated values again

I made SQLite database table that stores records, is it possible to change some values
that stored inside the table without deleting and entering updated values with older one.
e.g.
Lets say I want to change the wage of row ID = 0 to from 23$ to 40$ without deleting
the first row and reinserting making the auto incremented ID change to 1,
is to possible?!
Yes, you should use an UPDATE statement.
Yes, check out the UPDATE syntax of SQLite.

SQLite on Android: insertWithOnConflict() returning ever-increasing results for same data sets

PROBLEM BACKGROUND:
I have two tables where the Primary Key's are Text fields.
When I repeatedly use SQLiteDatabase.insertWithOnConflict(myTable, null, myValues, SQLiteDatabase.CONFLICT_REPLACE) with the same data, I see ever increasing values being returned.
So it looks like INSERT is being performed (instead of REPLACE) for the same data sets.
PROBLEM:
However, looking at these tables with "SQLite Database Browser" I see the correct number of records that I'd expect when REPLACE is performed.
The root to my confusion is the description Android documentation that states :
*Returns
the row ID of the newly inserted row OR the primary key of the existing row if the input param 'conflictAlgorithm' = CONFLICT_IGNORE OR -1 if any error*
QUESTION:
Why is it that a REPLACE is done, but the row ID of the existing data isn't returned ?
Seems to me like it is doing exactly what you are asking it to? You are asking it to replace and it does exactly that. It will remove the old row and insert the new one.
It would leave the old row and return that ID if you instead passed CONFLICT_IGNORE.
try{
insertWithOnConflict(openHelper_ob.TABLE_NAME,null,contentValues,
SQLiteDatabase.CONFLICT_ABORT);
}
catch(SQLException ex)
{
Toast.makeText(getApplicationContext(),"name found before plz, change it", Toast.LENGTH_SHORT).show();
}

Categories

Resources