This is how the table and row look like :
public static final String DATABASE_CREATE = "CREATE TABLE " + TABLE_NAME +
"(" + COLUMN_ID + " integer primary key autoincrement, " + COLUMN_INSTANT +
" TINYINT not null, " + COLUMN_CHANCE + " int not null);";
public static final String TABLE_INSERT = "INSERT INTO " + TABLE_NAME + " (" +
COLUMN_ID+"," + COLUMN_INSTANT +","+ COLUMN_CHANCE
+") VALUES (" +
"1," + "0," + "10"
+");";
And this is how I try to update them :
try {
open();
ContentValues values = new ContentValues();
values.put(column_name, value);
database.beginTransaction();
database.update(MySQLiteHelper.TABLE_NAME, values, MySQLiteHelper.COLUMN_ID + " = " + whereClause,null);
database.setTransactionSuccessful();
} catch (SQLException e) {
e.printStackTrace();
} finally {
database.endTransaction();
}
close();
The previous code would be equal to this query :
UPDATE options SET instant = 0 WHERE _id = 1
But the change is never applied. I check the table before and after the update but the values are the same, can anyone suggest anything?
You can make method to update...here is an example, change column name accordingly.
public int updateDetail(String new_name,String surname) {
ContentValues values = new ContentValues();
values.put("name", new_name);
return Db.update(TABLE_NAME ,values, SURNAME +"=?",new String[]{surname});
}
Related
Probably something simple but it's taking me a long time to figure out.
I want to save a rating bar rating and then load that rating when i re-open the actvity. But it is always returning 0.
I have a recipe table with a rating column:
final String CREATE_TABLE_RECIPES = "CREATE TABLE IF NOT EXISTS " +
TABLE_RECIPES + "(" +
RECIPE_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
RECIPE_NAME + " TEXT, " +
RECIPE_INSTRUCTIONS + " TEXT, " +
RECIPE_RATING + " FLOAT " +
")";
I have an onRatingBarChange Listener to save the rating to table:
(The values for id and rating are correct)
ratingbar.setOnRatingBarChangeListener(new RatingBar.OnRatingBarChangeListener() {
#Override
public void onRatingChanged(RatingBar ratingBar, float rating, boolean fromUser) {
db.addRating(id, rating);
}
});
The addRating method in db class:
public void addRating(int id, float rating) {
String SET_RATING =
"UPDATE " + TABLE_RECIPES +
" SET " + RECIPE_RATING + " = " + rating +
" WHERE " + RECIPE_ID + " = " + id;
SQLiteDatabase db = this.getWritableDatabase();
db.rawQuery(SET_RATING, null);
db.close();
}
and finally, the getRating method in db class:
public float getRating(int id) {
String GET_RATING =
"SELECT " + RECIPE_RATING +
" FROM " + TABLE_RECIPES +
" WHERE " + RECIPE_ID + " = " + id;
SQLiteDatabase db = this.getReadableDatabase();
Cursor c = db.rawQuery(GET_RATING, null);
c.moveToFirst();
float rating = c.getFloat(0);
Log.i("getRating", "gotRating: " + rating);
return rating;
}
The returned rating is always 0, so either it is not saving properly or it is not getting the value properly.
db.rawQuery(SET_RATING, null);
You should use execSQL() for UPDATE queries, not rawQuery(). (Or one of the convenience wrappers such as update().)
rawQuery() compiles the underlying SQL but does not execute it; execSQL() both compiles and runs the SQL.
I created one app for testing with two edittexts (Email and password) and 2 buttons (submit and viewdata). I was debug this application and it is working fine.
I wanted to update this application by adding one more Edittext (Name) and upgrade the SQLite Database in onUpgrade() method. The code is shown below.
public class DatabaseHelper extends SQLiteOpenHelper {
private static final String DATABASE_NAME="upgrade";
private static final int DATABASE_VERSION=2;
//details table details
public static final String TABLE_NAME_Details="details";
public static final String USERNAME="USERNAME";
public static final String PASSWORD="PASSWORD";
public static final String Name="Name"; //newly added column in version 2
public static final String TABLE_NAME_Details_temp="temp";
public static final String USERNAME_temp="USERNAME";
public static final String PASSWORD_temp="PASSWORD";
public static final String Name_temp="Name";
//create table statements for version 2
public static final String Create_Table_Details = "CREATE TABLE "
+ TABLE_NAME_Details + " (" + USERNAME + " TEXT PRIMARY KEY,"
+ PASSWORD + " TEXT ,"
+ Name + " TEXT"
+")";
/* creation for version 1
private static final String Create_Table_Details = "CREATE TABLE " +
TABLE_NAME_Details + "("
+ USERNAME + " TEXT PRIMARY KEY,"
+ PASSWORD + " TEXT"
+ ")";*/
//Alter table statements FOR onUpgrade()
//private static final String ALTER_Details = "ALTER TABLE " +
TABLE_NAME_Details + " ADD COLUMN" + Name + " TEXT";
public DatabaseHelper(Context context)
{
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
#Override
public void onCreate(SQLiteDatabase db) {
try{
db.execSQL(Create_Table_Details);
Log.d("database","installed successfully");
}catch (Exception e)
{
e.printStackTrace();
Log.e("/test","Exception due to"+e.toString());
}
}
#Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
{
String TEMP_CREATE_CONTACTS_TABLE = "CREATE TABLE " +
TABLE_NAME_Details_temp + "("
+ USERNAME_temp + " TEXT," + PASSWORD_temp + " TEXT )";
db.execSQL(TEMP_CREATE_CONTACTS_TABLE);
db.execSQL("INSERT INTO " + TABLE_NAME_Details_temp + " SELECT " +
USERNAME + ", "
+ PASSWORD + " FROM " + TABLE_NAME_Details);
db.execSQL("DROP TABLE "+ TABLE_NAME_Details);
String CREATE_CONTACTS_TABLE = "CREATE TABLE " + TABLE_NAME_Details + "("
+ USERNAME + " TEXT," + PASSWORD + " TEXT," + Name + " TEXT )";
db.execSQL(CREATE_CONTACTS_TABLE);
db.execSQL("INSERT INTO " + TABLE_NAME_Details + " SELECT " +
USERNAME_temp + ", "
+ PASSWORD_temp + ", " + Name_temp + ", " + null + " FROM " +
TABLE_NAME_Details_temp);
db.execSQL("DROP TABLE " + TABLE_NAME_Details);
}
public boolean insertData(String username, String password,String name)
{
try{
SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues=new ContentValues();
contentValues.put(USERNAME,username);
contentValues.put(PASSWORD,password);
contentValues.put(Name,name);
long result=db.insertOrThrow(TABLE_NAME_Details, null, contentValues);
if(result==-1)
return false;
else
return true;
}catch(Exception e)
{
e.printStackTrace();
Log.e("/test","Exception due to"+e.toString());
return false;
}
}
//update value
public boolean updatePassword(String LoggedUsername)
{
SQLiteDatabase db = null;
try {
db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(PASSWORD, "test");
if (db.isOpen()) {
db.update(TABLE_NAME_Details, values, USERNAME + "='" +
LoggedUsername+"'",null);
return true;
}
else
return false;
} catch (Exception e) {
Log.d("eEmp/DBUpdateUser ", e.toString());
return false;
}
}
//getting data from database
public Cursor getAllData()
{
SQLiteDatabase db = this.getWritableDatabase();
Cursor res = db.rawQuery("select * from "+TABLE_NAME_Details,null);
return res;
}
}
I installed the 1st version and working fine and then uninstalled.
After uninstallation, installed the 2nd version and it stores the data. it is also working fine.
When I am trying to update the 1st version with the 2nd version in debug mode, the app updated successfully and the data that entered in the 1st version is visible. and when I am trying to store the data in 2nd version, the data is not inserting to sqlite database. Why?
While debugging, insert method is called but here if(result==-1) I am getting -1 instead of 1. so, data control comes out from the method and the data is not inserting to database.
For example: In 1st version: I entered username: stackoverflow and password stackoverflow.
and I updated the app to 2nd version and updated successfully. Now I entered username: hello password:hello name:hello and then click on submit button.
After that If I click on viewdata button: it display username: stackoverflow password stackoverflow name:null. But not displaying the data inserted in 2nd version.
Any help would be appreciated. Please help me with the correct solution.
Thank you.
Try this..
// Upgrading database
#Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
String TEMP_CREATE_CONTACTS_TABLE = "CREATE TABLE " + TEMP_TABLE_CONTACTS + "("
+ KEY_ID + " INTEGER PRIMARY KEY," + KEY_NAME + " TEXT," + KEY_ADDRESS + " TEXT)";
db.execSQL(TEMP_CREATE_CONTACTS_TABLE);
// Create an temporaty table that can store data of older version
db.execSQL("INSERT INTO " + TEMP_TABLE_CONTACTS + " SELECT " + KEY_ID + ", "
+ KEY_NAME + ", " + KEY_ADDRESS + " FROM " + TABLE_CONTACTS);
// Insert data into temporary table from existing older version database (that doesn't contains ADDRESS2 //column)
db.execSQL("DROP TABLE "+ TABLE_CONTACTS);
// Remove older version database table
String CREATE_CONTACTS_TABLE = "CREATE TABLE " + TABLE_CONTACTS + "("
+ KEY_ID + " INTEGER PRIMARY KEY," + KEY_NAME + " TEXT," + KEY_ADDRESS + " TEXT," + KEY_ADDRESS2 + " TEXT )";
db.execSQL(CREATE_CONTACTS_TABLE);
// Create new table with ADDRESS2 column
db.execSQL("INSERT INTO " + TABLE_CONTACTS + " SELECT " + KEY_ID + ", "
+ KEY_NAME + ", " + KEY_ADDRESS + ", " + null + " FROM " + TEMP_TABLE_CONTACTS);
// Insert data ffrom temporary table that doesn't have ADDRESS2 column so left it that column name as null.
db.execSQL("DROP TABLE " + TEMP_TABLE_CONTACTS);
}
there is a typing mistake check it you column name is db is
public static final String Name="Name"; //newly added column in version 2
and you are using NAME_ change it while inserting data in sqlite
Try this
try{
SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues=new ContentValues();
contentValues.put(USERNAME,username);
contentValues.put(PASSWORD,password);
contentValues.put(Name,name);
long result=db.insert(TABLE_NAME_Details, null, contentValues);
if(result==-1)
return false;
else
return true;
}catch(Exception e)
{
e.printStackTrace();
Log.e("/test","Exception due to"+e.toString());
return false;
}
Don't catch exceptions in onCreate() and onUpgrade(). If there's a problem, the method must not return normally. Returning normally means everything was successful and the database schema is ready to use.
Notice the missing whitespace in your onUpgrade() ALTER TABLE:
private static final String ALTER_Details = "ALTER TABLE " + TABLE_NAME_Details + " ADD COLUMN" + NAME_ + " TEXT";
between COLUMN and NAME_. This causes the upgraded database to not work. Fresh install only goes thru onCreate() and the bug in onUpgrade() is not exposed.
Use insertOrThrow() instead of insert() to get more helpful error messages, such as "no such column".
I'm trying to write two objects called 'Friend' and 'Social' to the database. Friend writes no problem, but 'Activity' wont work.
DatabaseHandler code:
public DatabaseHandler(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
// Creating Tables
#Override
public void onCreate(SQLiteDatabase db) {
String CREATE_CONTACTS_TABLE = "CREATE TABLE " + TABLE_FRIENDS + "("
+ KEY_ID + " INTEGER PRIMARY KEY," + KEY_FIRSTNAME + " TEXT," + KEY_LASTNAME + " TEXT,"
+ KEY_EMAIL + " TEXT" + ")";
db.execSQL(CREATE_CONTACTS_TABLE);
String CREATE_ACTIVITIES_TABLE = "CREATE TABLE " + TABLE_ACTIVITIES + "("
+ KEY_ID + " INTEGER PRIMARY KEY," + KEY_FRIEND + " TEXT," + KEY_DESCRIPTION + " TEXT,"
+ KEY_DAY + " TEXT" + KEY_MONTH + " TEXT" + KEY_YEAR + " TEXT"+ KEY_FRIEND_ID + " TEXT" +")";
db.execSQL(CREATE_ACTIVITIES_TABLE);
}
// Upgrading database
#Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// Drop older table if existed
db.execSQL("DROP TABLE IF EXISTS " + TABLE_FRIENDS);
db.execSQL("DROP TABLE IF EXISTS " + TABLE_ACTIVITIES);
// Create tables again
onCreate(db);
}
/**
* All CRUD(Create, Read, Update, Delete) Operations
*/
// Adding new contact
void addContact(Friend friend) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(KEY_FIRSTNAME, friend.getFirstName());
values.put(KEY_LASTNAME, friend.getLastName());
values.put(KEY_EMAIL, friend.getEmail());
// Inserting Row
db.insert(TABLE_FRIENDS, null, values);
db.close(); // Closing database connection
}
// Adding new social
void addSocial(Social social) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(KEY_FRIEND_ID, social.getFriend_ID());
values.put(KEY_DESCRIPTION, social.getDescription());
values.put(KEY_FRIEND, social.getFriend());
values.put(KEY_DAY, social.getDay());
values.put(KEY_MONTH, social.getMonth());
values.put(KEY_YEAR, social.getYear());
// Inserting Row
db.insert(TABLE_ACTIVITIES, null, values);
db.close(); // Closing database connection
}
// Getting All Contacts
public List<Friend> getAllContacts() {
List<Friend> contactList = new ArrayList<Friend>();
// Select All Query
String selectQuery = "SELECT * FROM " + TABLE_FRIENDS;
SQLiteDatabase db = this.getWritableDatabase();
Cursor cursor = db.rawQuery(selectQuery, null);
// looping through all rows and adding to list
if (cursor.moveToFirst()) {
do {
Friend friend = new Friend();
friend.setId(Integer.parseInt(cursor.getString(0)));
friend.setFirstName(cursor.getString(1));
friend.setLastName(cursor.getString(2));
friend.setEmail(cursor.getString(3));
// Adding contact to list
contactList.add(friend);
} while (cursor.moveToNext());
}
// return contact list
return contactList;
}
// Getting All Activities
public List<Social> getAllActivities() {
List<Social> activityList = new ArrayList<Social>();
// Select All Query
String selectQuery = "SELECT * FROM " + TABLE_ACTIVITIES;
SQLiteDatabase db = this.getWritableDatabase();
Cursor cursor = db.rawQuery(selectQuery, null);
Social social = new Social();
// looping through all rows and adding to list
if (cursor.moveToFirst()) {
do {
social.setId(Integer.parseInt(cursor.getString(0)));
social.setFriend(cursor.getString(1));
social.setDescription(cursor.getString(2));
social.setDay(cursor.getString(3));
social.setMonth(cursor.getString(4));
social.setYear(cursor.getString(5));
social.setFriend_ID(cursor.getString(6));
// Adding contact to list
activityList.add(social);
} while (cursor.moveToNext());
}
return activityList;
}
This is in the activity which tries to add a new social object to the DB. It does not give me and errors but shows up blank when I try to display all social objects in the database.
Social social = new Social();
List<Friend> friends = db.getAllContacts();
for(Friend friend: friends)
{
if(friend.getFirstName().equalsIgnoreCase(String.valueOf(spinner2.getSelectedItem())))
{
social.setFriend(friend.getFirstName() + friend.getLastName());
social.setFriend_ID(String.valueOf(friend.getId()));
social.setDescription(rs3.getText().toString());
social.setDay(String.valueOf(simpleDatePicker.getDayOfMonth()));
social.setMonth(String.valueOf(simpleDatePicker.getMonth()));
social.setYear(String.valueOf(simpleDatePicker.getYear()));
found = true;
}
}
if(found)
{
db.addActivity(social);
}
The following code will not create the activities table as you would expect due to commas being omitted,
String CREATE_ACTIVITIES_TABLE = "CREATE TABLE " + TABLE_ACTIVITIES + "("
+ KEY_ID + " INTEGER PRIMARY KEY," + KEY_FRIEND + " TEXT," + KEY_DESCRIPTION + " TEXT,"
+ KEY_DAY + " TEXT" + KEY_MONTH + " TEXT" + KEY_YEAR + " TEXT"+ KEY_FRIEND_ID + " TEXT" +")";
db.execSQL(CREATE_ACTIVITIES_TABLE);
That is commas have been omitted after the column type (TEXT) for the KEY_DAY column, for the KEY_MONTH column and for the KEY_YEAR column.
Rather it would create a table along the lines of (noting that variables, such as KEY_ID would be replaced with the value of the respective variable, which could affect the outcome) :-
columns:-
KEY_ID (TYPE INTEGER and the PRIMARY KEY)
KEY_FRIEND (TYPE as TEXT)
KEY_DESCRIPTION (TYPE as TEXT)
KEY_DAY (TYPE as TEXT KEY_MONTH TEXT KEY_YEAR TEXTKEY_FRIEND_ID TEXT)
As such the insert would not insert a row as columns KEY_MONTH .... KEY_FRIEND_ID would not exist.
The corrected code could be :-
String CREATE_ACTIVITIES_TABLE =
"CREATE TABLE " + TABLE_ACTIVITIES +
"(" +
KEY_ID + " INTEGER PRIMARY KEY," +
KEY_FRIEND + " TEXT," +
KEY_DESCRIPTION + " TEXT," +
KEY_DAY + " TEXT," + //<<<<<<<< added comma
KEY_MONTH + " TEXT," + //<<<<<<<< added comma
KEY_YEAR + " TEXT," + //<<<<<<<< added comma
KEY_FRIEND_ID + " TEXT" +
")";
db.execSQL(CREATE_ACTIVITIES_TABLE);
After changing the code you would need to do one of the following (note data may be lost) :-
delete the App's Data
uninstall the App
increment the Database version
When one of the above has been done you can then rerun the App.
Note the above code has not been tested so it may have some errors.
Syntax is wrong in the create table SQL. Missing commas.
I have a serious problem with my android studio's app!
I have created a db with two table. The problem happen when I insert the data. Infact, when I insert the data in the form, the previous one are deleted!
------------For example-------------------
First insertion:
Name --> a
Password --> a
Second insertion:
Name --> b
Password --> b
But the first insertion is canceled!
I hope that anyone help me... I put some code to understand my problem:
DatabaseOperation.java
private static final String CREATETABLE_QUERY_USER =
"CREATE TABLE " + UserContact.DatabaseInfo.TABLE_USER + " ( "
+ UserContact.UserInfo.ID + " INTEGER PRIMARY KEY AUTOINCREMENT, "
+ UserContact.UserInfo.USERNAME + " TEXT NOT NULL, "
+ UserContact.UserInfo.PASSWORD + " TEXT NOT NULL, "
+ UserContact.UserInfo.EMAIL + " TEXT NOT NULL, "
+ UserContact.UserInfo.PHONE_NUMBER + " TEXT NOT NULL "
+ " );"
;
private static final String CREATETABLE_QUERY_PASSWORD =
"CREATE TABLE " + UserContact.DatabaseInfo.TABLE_PASS + " ( "
+ UserContact.PasswordTable.ID + " INTEGER PRIMARY KEY AUTOINCREMENT,"
+ UserContact.PasswordTable.NAME + " TEXT NOT NULL, "
+ UserContact.PasswordTable.POSTERPASSWORD + " INTEGER NOT NULL, "
+ UserContact.PasswordTable.DESCRIPTION + " TEXT NOT NULL, "
+ UserContact.PasswordTable.PASSWORD + " TEXT NOT NULL "
//+ " FOREIGN KEY(" + UserContact.PasswordTable.ID + ") REFERENCES " + UserContact.DatabaseInfo.TABLE_USER + "(" + UserContact.UserInfo.ID +")
+ " );"
;
public void addUser(SQLiteDatabase db, String username, String password, String email, String phone){
ContentValues contentValues = new ContentValues();
contentValues.clear();
contentValues.put(UserContact.UserInfo.USERNAME, username);
contentValues.put(UserContact.UserInfo.PASSWORD, password);
contentValues.put(UserContact.UserInfo.EMAIL, email);
contentValues.put(UserContact.UserInfo.PHONE_NUMBER, phone);
db.insert(UserContact.DatabaseInfo.TABLE_USER, null, contentValues);
}
public void addPassword(SQLiteDatabase db, String name, String note, String password, int posterpassword){
ContentValues contentValues = new ContentValues();
contentValues.clear();
if(password.length() > 0){
contentValues.put(UserContact.PasswordTable.POSTERPASSWORD, posterpassword);
contentValues.put(UserContact.PasswordTable.NAME, name);
contentValues.put(UserContact.PasswordTable.DESCRIPTION, note);
contentValues.put(UserContact.PasswordTable.PASSWORD, password);
db.insert(UserContact.DatabaseInfo.TABLE_PASS, null, contentValues);
Log.e("DATABASE OPERATION", " INSERT PASSWORD successfull in " + UserContact.DatabaseInfo.TABLE_PASS + "...");
}
}
NewContact.java
String name = usernameEditText.getText().toString();
String password = passwordEditText.getText().toString();
String mail = emailEditText.getText().toString();
String phone = phoneEditText.getText().toString();
//Add user to database
String dbFile = getDatabasePath(UserContact.DatabaseInfo.DB_NAME).getPath();
databaseOperations = new DatabaseOperations(context);
sqLiteDatabase = SQLiteDatabase.openOrCreateDatabase(dbFile, UserContact.DatabaseInfo.password, null);
sqLiteDatabase = databaseOperations.getWritableDatabase(UserContact.DatabaseInfo.password);
databaseOperations.addUser(sqLiteDatabase, name, password, mail, phone);
Log.e("New Contact: " , "Success! -> username: " + name + ", password: " + password);
//End to add user----------------------------------
//After registration, return to authentication page
Intent intent_authenticationPage = new Intent(getApplicationContext(), AuthenticationPage.class);
startActivity(intent_authenticationPage);
I am not getting your database structure but anyways try changing the CREATE TABLE syntax from
CREATE TABLE <table_name>(...)
to
CREATE TABLE IF NOT EXISTS <table_name>(...)
My table name is TX_TABLE and the column for insertion is TX_AMOUNT , being 'integer'. The insertion of values say like 1234.56 for 1st row and 12345.67 for 2nd row is correctly taking place for me as I could see it by putting breakpoints.
Now, if I retrieve the data from TX_AMOUNT column the values are shown like
1234.56 for the 1st row which is OK and for the 2nd row it shows as 12345.7
instead of 12345.67. If I enter a value with upto 15 digits without decimals
there is no problem. Only when values with decimals like in 12345.67 it shows
like 12345.7.
I give below my codes:
create table:
private static final String CREATE_TX_TABLE= "CREATE TABLE " + TX_TABLE + " (" +TX_UID+ " INTEGER PRIMARY KEY AUTOINCREMENT," + TX_TITLE_UID + " INTEGER," + TX_AMOUNT + " INTEGER," + TX_PARTICULARS + " VARCHAR(255)," + TX_DATE+" DATE," + TX_ID + " VARCHAR(255)," + TX_NAME + " VARCHAR(255)," + TX_CONTRA + " VARCHAR(255)," + TX_LISTVIEW_DATE + " DATE);";
insertion:
public void insertTxData(String inputparticulars, String inputamount, String showSelectedTitle, String inputID, String txid, String contraYorNo){
SQLiteDatabase db = helper.getWritableDatabase();
ContentValues contentvalues = new ContentValues();
if (inputparticulars.equals("") || inputparticulars.equals(null)){
inputparticulars= "particulars not entered";
}
contentvalues.put(VivzHelper.TX_PARTICULARS,inputparticulars);
// with breakpoint here, the values entered are perfectly getting in
contentvalues.put(VivzHelper.TX_AMOUNT, inputamount);
contentvalues.put(VivzHelper.TX_TITLE_UID, inputID);
contentvalues.put(VivzHelper.TX_ID, MainActivity.txid);
contentvalues.put(VivzHelper.TX_DATE, new SimpleDateFormat("yyyy-MM-dd").format(new Date()));
contentvalues.put(VivzHelper.TX_LISTVIEW_DATE, new SimpleDateFormat("dd-MM-yyyy").format(new Date()));
contentvalues.put(VivzHelper.TX_NAME,showSelectedTitle);
contentvalues.put(VivzHelper.TX_CONTRA,contraYorNo);
db.insert(VivzHelper.TX_TABLE, null, contentvalues);
String td = new SimpleDateFormat("dd-MM-yyyy").format(new Date());
}
data retrieval:
String name = titlesofReports.tagExpensesOrIncome;
SQLiteDatabase db = helper.getReadableDatabase();
String[] columns = { helper.TX_UID, helper.TX_NAME, helper.TX_PARTICULARS,
helper.TX_AMOUNT, helper.TX_DATE,helper.TX_CONTRA};
Cursor c = db.query(VivzHelper.TX_TABLE, columns, (helper.TX_ID + "='" + name + "' AND " + helper.TX_DATE + "= '" + td + "'"), null, null, null,null);
gt = 0;
gt_selectedItem_number = 0;
if (c != null) {
c.moveToFirst();
while (c.isAfterLast() == false) {
String v_uid=(c.getString(0));
String txTotal = (c.getString(3));
Float dd=Float.parseFloat(txTotal);
gt = gt + new Float(txTotal);
BigDecimal bdtxTotal= new BigDecimal(c.getString(3));
}
Using breakpoints, when I verified I could see all the 4 variables like
txTotal,dd1,dd,bdTxTotal are giving the same result like 12345.7 instead of 12345.67.