Data not inserting in Sqlite database in android - 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);
}

Related

How to keep the database updated when the user enters new values in android studio?

I'm dealing with a simple app, which basically stores various dates. But when I enter a new date, I have to close the app and open it again. The same thing counts for changing the date values. I can only see the changes by closing the app and reopening it.
Here are the two main functions in my DB class;
public void addBirthday(Person person){ //Add a data to database
SQLiteDatabase db = this.getWritableDatabase();
ContentValues cv = new ContentValues();
cv.put(column_name, person.getName());
cv.put(column_sname, person.getSname());
cv.put(column_day, person.getDay());
cv.put(column_month, person.getMonth());
cv.put(column_year, person.getYear());
db.insert(TABLE_NAME, null, cv);
db.close();
}
public int updateBirthday(Person person) {
// 1. get reference to writable DB
SQLiteDatabase db = this.getWritableDatabase();
// 2. create ContentValues to add key "column"/value
ContentValues cv = new ContentValues();
cv.put(column_name, person.getName());
cv.put(column_sname, person.getSname());
cv.put(column_day, person.getDay());
cv.put(column_month, person.getMonth());
cv.put(column_year, person.getYear());
// 3. updating row
int i = db.update(TABLE_NAME, //table
cv, // column/value
column_id +" = ?", // selections
new String[] { String.valueOf(person.getId()) }); //selection args
// 4. close
db.close();
return i;
}
You will need to refresh the view (probably a ListView) where you display this data.
Get rid of the notifyDataSetChanged() and call requery() on your Cursor

Android split function behavior

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);

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 first use does not save record

I have a problem with my sqlite.
when i install the app and database is empty. the first entry is never stored.
there is any way to solve this???
this is the code to insert data :
final ContentValues values=new ContentValues();
values.put("cod_comp",pub_cod_comp);
values.put("telefone_comp", pub_telf_comp);
values.put("morada_comp", pub_morada_comp);
values.put("porta_n_comp", pub_porta_comp);
values.put("cod_postal_comp", pub_cp_comp);
dataBase.insert("Comprador",values);ยด
this the insert in my class database:
public void insert(String table, ContentValues values) throws SQLException {
mDataBase.insert(table, null, values);
thanks in advance

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.

Categories

Resources