Is there a way to prevent the sqlite db to crash? I have a listview with items in it. When you click on a button al items are saved in a db. But when there are a lot of products in the list and you click verry fast on the save button the application freezes. And when I check my database afterwards all the items are saved multiple times.
Someone knows the answer?
Code:
With isProdAlreadyFavorite I check if the db already got an entry with the id...
When you click on the button he executes this function..
public void addFavProducten(ArrayList<Product> producten) {
db = this.getWritableDatabase();
db.beginTransaction();
try {
for (Product product : producten) {
if (!isProdAlreadyFavorite(product.getProductid())) {
ContentValues cv = new ContentValues();
cv.put(Constanten.FAV_PROD_COLUMN_PRODUCTID,
product.getProductid());
db.insert(Constanten.TABLE_FAVPROD, null, cv);
}
}
db.setTransactionSuccessful();
} catch (Exception e) {
db.endTransaction();
} finally {
db.endTransaction();
db.close();
}
}
Related
I'm creating a forum application and I currently if I delete a thread I'm deleting all threads.
Is there a good method or query to check if the UserId == ThreadId?
My current code:
public void deleteThread() {
SQLiteDatabase db = this.getWritableDatabase();
// Delete All Rows
db.delete(TABLE_THREAD, null, null);
db.close();
Log.d(TAG, "Deleted all Thread info from sqlite");
}
You need to pass correct value to the well-documented delete method to narrow down the scope of deletion to a subset of all entries in the DB table.
public void deleteThreadById(String threadId) {
SQLiteDatabase db = this.getWritableDatabase();
String whereClause = "threadId = " + threadId;
db.delete(TABLE_THREAD, whereClause, null);
db.close();
}
Deleting all threads of a given user via their userId would be similar but probably doesn't make sense in a forum software.
This is how SQL works in general and it's a bit scary you started development without familiarising yourself with the very basics.
Something like this;
public void deleteThread(String threadName) {
SQLiteDatabase db = this.getWritableDatabase();
try {
db.delete(MYDATABASE_TABLE, "name = ?", new String[]{threadName});
} catch (Exception e) {
e.printStackTrace();
} finally {
db.close();
}
}
Something long these lines, querying database to find the specific row that has column which matches the parameter.
For example to delete a row which the name column is "Hello World";
deleteThread("Hello World");
Hi guys i'have problem with this little block of code
// Insert a new contact in database
public void insertInSignature(String TITLE_SI) {
try {
// Open Android Database
db = databaseHelper.getWritableDatabase();
ContentValues initialValues = new ContentValues();
initialValues.put("TITLE_SI", TITLE_SI);
db.insert("DELIVERY_SLIP", null, initialValues);
} catch (SQLException sqle) {
Log.e(TAG, "insertUser Error");
Log.e(TAG, "Exception : " + sqle);
} finally {
// Close Android Database
databaseHelper.close();
}
}
I have unique constraint on my table "DELIVERY_SLIP
So , when i'm trying to insert some row which already exist , it return some error like "Oh shit , you're inserting the same , i'm sorry men , i can't do it"
http://cdn.imghack.se/images/3b51afd07d1f1a8bd021c9e9dfc57e98.png
Here my log
It's this line
databaseHelper.close();
When database helper close , this return the log.
I just want to avoid to log it, I already tested with a tryCatch on sqliteConstraintException
But, nothing worked.
Thanks by advance
Instead of insert(), use insertWithOnConflict() with some conflict resolution algorithm appropriate for your use, such as SQLiteDatabase.CONFLICT_IGNORE.
i am working on sqlite insertion using contentvalues with transaction . The following code does not generate any exception however the data is not inserted.
Did i miss somethings ? Thanks.
public boolean addRecord(Rec rec) {
SQLiteDatabase db = this.getWritableDatabase();
db.beginTransaction();
ContentValues values = new ContentValues();
values.put(KEY_ID, rec.get());
// Inserting Row
try {
db.insertOrThrow(TABLE_RECORDS, null, values);
} catch (SQLException e) {
e.printStackTrace();
}
db.endTransaction();
db.close();
return true;
}
After calling beginTransaction, you must call setTransactionSuccessful to ensure that the transaction gets committed. Without that call, any changes in the transaction are rolled back.
db.beginTransaction();
try {
...
db.setTransactionSuccessful();
} finally {
db.endTransaction();
}
This particular construction ensures that any exception in the actual database code will result in an rollback.
(If you get an exception, it is a bad idea to just swallow it; the entire function must fail.)
There's this method which i've created to know if a contact exists in my Sqlite database.
public Cursor doesContactExist(String name)
{
return db.rawQuery("select * from contacts where name = ?; ", new String[] {name});
}
But whenever it is called from another activity it crashes only at this point....
if(db.doesContactExist(name)==null){ <== crashes here
try {
db.open();
db.insertContact(name,cNumber);//name and number are Strings
listItems.add(name); //a List View array
db.close();
adapter.notifyDataSetChanged(); //adapter for ListView
}
catch (Exception e) {
//some code }
}
According to me the rawQuery is having some troulble, especially the sql String in it.
Any help please?
you try to make a select before your database is open:
if(db.doesContactExist(name)==null){ <== it is closed here
try {
db.open(); <== open the db here
change it to:
db.open();
if(db.doesContactExist(name)==null){
try {
I think it might help you..
db.open();
if(db.doesContactExist(name)==null)
{
try
{
// Your Code Here..
}
}
and in Database Class
public Cursor doesContactExist(String name)
{
return db.rawQuery("SELECT * FROM contacts WHERE name=?", new String[ {name.toString()});
}
use db.open() method before using any database function because it will open your connection with database.. and at last don't forget to close database like db.close() and if you are using Cursor anywhere in code please close this also. it not generates any error but it gives you warnings. :)
I've been stuck on this for a few hours now.
I have an Activity that calls a method to write some values into a database, it works, except for the fact that it overWrites the same row in the database over and over again. My database table does have an _id that is set to autoincrement.
try {
myDataBase.beginTransaction();
myDataBase.insert("camera_notes", null, camera_data);
myDataBase.setTransactionSuccessful();
} catch (SQLException e) {
Log.i(TAG,"#################Exception thrown from updateDataBaseNotes:################ "+e);
e.printStackTrace();
}
finally{
myDataBase.endTransaction();
}
close();
I have just tried adding the transaction code, but no luck so far.
Does anyone have any ideas or can point me in the right direction?
Thanks
ContentValues camera_data = new ContentValues();
camera_data.put("note_title", title);
camera_data.put("note_text", note);
camera_data.put("image_source", image_src);
camera_data.put("sound_source", recording_src);
Is that the only row? If so, it sounds like you're destroying the entire table each time before you're inserting the row.