Android application using ORMlite with SQLite. Here my method that delete some rows from table:
private static boolean apply(Dao<NotificationInvoice, Integer> invoiceDao) {
boolean isSuccess = false;
try {
String sql ="DELETE from notification where ownerkey not in ("123","456")";
GenericRawResults<String[]> rawResults = invoiceDao.queryRaw(sql);
isSuccess = true;
} catch (SQLException e) {
if (BuildConfig.DEBUG)
Log.e(TAG, e.getMessage(), e);
}
return isSuccess;
}
It works. Successfully deletes N records from table. Nice.
The question is: How I can get count of deleted rows without doing a second query?
You can use DeleteBuilder of OrmLite.
DeleteBuilder deleteBuilder = invoiceDao.deleteBuilder();
deleteBuilder.where().notIn("ownerkey", new String[]{"123", "457"});
deleteCount = deleteBuilder.delete()
Related
I am using the following code to access sql server database table record. I can confirm that the table columns are retrieved. But due to some reasons, it does not retrieve any row. Am I missing anything?
I am using remote server MS SQL Server.
try {
try {
Class.forName("net.sourceforge.jtds.jdbc.Driver").newInstance();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
try {
Connection DbConn = DriverManager.getConnection("jdbc:jtds:sqlserver://ServerName/DBName;user=sa;password=password");
Statement stmt = DbConn.createStatement();
ResultSet reset = stmt.executeQuery("select * from tblUser");
String str = reset.getString(1);
DbConn.close();
} catch (SQLException e) {
}
} catch (Exception e) {
e.printStackTrace();
}
You need to call ResultSet#next() to advance the cursor to the first record of the result set. From the Javadoc:
Moves the cursor froward one row from its current position. A ResultSet cursor is initially positioned before the first row; the first call to the method next makes the first row the current row; the second call makes the second row the current row, and so on.
So your code should look like this:
try {
String cs = "jdbc:jtds:sqlserver://ServerName/DBName;user=sa;password=password";
Connection DbConn = DriverManager.getConnection(cs);
Statement stmt = DbConn.createStatement();
ResultSet reset = stmt.executeQuery("select * from tblUser");
while (reset.next()) {
String str = reset.getString(1);
// do something with this record
}
DbConn.close();
} catch (SQLException e) {
// handle exception here
}
The initial pointer is located before the first row, so if you want the first result like in your example, do:
if(reset.next()) {
String s = r.getString(1);
}
I have a method which reads data from file line by line and takes value between coma, then puts this value into INSERT query. Data in file saved in this way:
–,08:10,–,20:20,08:15,08:16,20:26,20:27,08:20,08:21,20:31,20:32,08:30,08:31,20:40,20:41,08:37,08:38,20:46
20:47,08:48,08:50,20:56,20:57,09:00,09:01,21:07,21:08
08:53,–,17:43,09:01,09:03,09:13,09:15,18:02,18:04,–,–,09:19,09:25
Here is actual my code:
public void insertTime(SQLiteDatabase database, String table) throws FileNotFoundException {
BufferedReader br = null;
String line;
try {
int j = 0;
br = new BufferedReader(new InputStreamReader(context.getAssets().open("time.txt")));
database.beginTransaction();
while ((line = br.readLine()) != null) {
j++;
String query = "INSERT INTO "+table+""+j+" (arrival, departure) VALUES (?,?)";
SQLiteStatement statement = database.compileStatement(query);
// use comma as separator
String[] time = line.split(",");
for(int i = 1; i < time.length; i+=2) {
statement.bindString(1,time[i-1]);//arrival
statement.bindString(2,time[i]);//departure
statement.executeInsert();
statement.clearBindings();
}
}
database.setTransactionSuccessful();
database.endTransaction();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (br != null) {
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
The problem is that data insert very slow, despite I use SQLiteStatement and transactions. For example, when I insert 69000 rows it takes about 65,929 seconds.
What have I to change in my code to improve speed of insertion ?
UPDATE
OK, I have simplified my code, I got rid of BufferedReader and now it looks like this
public void insertTime(SQLiteDatabase database) throws FileNotFoundException {
database.beginTransaction();
int r = 0;
while (r < 122) {
r++;
String query = "INSERT INTO table_1 (arrival, departure) VALUES (?,?)";
SQLiteStatement statement = database.compileStatement(query);
for(int i = 1; i < 1100; i++) {
statement.bindString(1,i+"");//arrival
statement.bindString(2,i+"");//departure
statement.executeInsert();
statement.clearBindings();
}
}
database.setTransactionSuccessful();
database.endTransaction();
}
But it still so long inserts data, more than 2 min. Do you have any ideas how to increase speed of my second example ?
Here is a very very detailed post on every method of increasing SQL insertion speed.
Move beginTransaction() and setTransactionSuccessful() outside of while loop and it will be way faster.
A new transaction is started for each item in the while() loop.
It might go a bit faster if you only have 1 transaction to do all your insertions.
Also, when your data is corrupt and String.split doesn't give you at least 2 items, then your transaction will not be ended properly due to an Exception being thrown.
Every time you insert a row in a table with indexes, the indexes have to be adjusted. That operation can be costly. Indexes are kept as b-trees and if you hit the rebalance point, you're bound to have a slowdown. One thing you can do to test this is to remove your indexes. You could also drop the indexes, insert, then re-create the indexes.
For those using JDBC (Java): to be sure, do you first set the autoCommit to FALSE?
I guess so, because you work with explicit transactions.
The performace gain I got by explicitly setting the autocommit off was over 1000 times!
So:
Class.forName("org.sqlite.JDBC");
String urlInput = "jdbc:sqlite:" + databaseFile;
databaseConnection = DriverManager.getConnection(urlInput);
databaseConnection.setAutoCommit( false);
And:
String sql = "INSERT INTO " + TABLE_NAME + " ( type, bi, ci, fvi, tvi, content_type) VALUES ('V',?,?,?,?,'rtf')";
PreparedStatement psi = databaseConnection.prepareStatement(sql);
for( Item item : items) {
psi.setInt(1, item.property1);
// ....
count = psi.executeUpdate();
}
databaseConnection.commit();
databaseConnection.setAutoCommit( true);
So, when somebody forgets this, this may have a huge effect.
Ok, I've got this Retrofit Call that receives a list of objects and insert the into a local SQLite database. I want to display a message saying that the operation was successful with a Ok button that when pressed opens a new activity.
How do I check if my Query has finished so I can show the message?
final ContactApi contactApi = retrofit.create(ContactApi.class);
Call<List<Contact>> callContact = contactApi.getContact(token);
callContact.enqueue(new Callback<List<Contact>>() {
#Override
public void onResponse(Response<List<Contact>> response, Retrofit retrofit) {
List<Contact> contactList = response.body();
if (contactList != null) {
try {
DBHelper dbHelper = new DBHelper(TokenActivity.this, token);
SQLiteDatabase conn = dbHelper.getWritableDatabase();
RepoContact repocontact = new RepoContact(conn);
// Inserts each contact into the database
for (Contatc c : contactList) {
repositorioCadastro.inserirCadastro(c);
Log.i("ACTTOKEN", "Contact insert ID: " + c.getId());
}
} catch (SQLiteException e) {
Log.i("ACTTOKEN", "Faillure on insert: " + e.getMessage());
}
}
wrap your code in try{...}finally{...} blocks with a listener ( beginTransactionWithListener(SQLiteTransactionListener transactionListener)), and use the transactionListner to check whether everything went well within the transaction, in addition to everything within the try/finally.
what you have is good, just try adding finally block..
something like this..
db.beginTransaction();
try {
...
db.setTransactionSuccessful();
} finally {
db.endTransaction();
}
You can try a different loop, something like this:
for(int i = 0; i < contactList.size(); i++) {
Contact c = contactList.get(i);
repositorioCadastro.inserirCadastro(c);
Log.i("ACTTOKEN", "Contact insert ID: " + c.getId());
if(i == (contactList.size() - 1)) {
// DO SOMETHING HERE
}
}
You may check insert statement return a long when query successfully executed then long value.
db.insert()
returns the row ID of the newly inserted row, or -1 if an error occurred
This question already has answers here:
Android SQLite database: slow insertion
(5 answers)
Closed 8 years ago.
I want to inset more than 3000 records in android SQLite but the following code take to much time for the data inserion
Here is my code.
public boolean addSale(Sale objSale) {
ContentValues values = new ContentValues();
values.put(SALE_BRANCH, objSale.getBranch());
values.put(SALE_SUPPLIER, objSale.getSupplier());
values.put(SALE_BUYER, objSale.getBuyer());
values.put(SALE_CAT1, objSale.getCat1());
values.put(SALE_CAT2, objSale.getCat2());
values.put(SALE_CAT3, objSale.getCat3());
values.put(SALE_CAT4, objSale.getCat4());
values.put(SALE_CAT5, objSale.getCat5());
values.put(SALE_CAT6, objSale.getCat6());
values.put(SALE_DESIGNO, objSale.getDesigNo());
values.put(SALE_ITEMSIZE, objSale.getItemSize());
values.put(SALE_SALEQTY, objSale.getSaleQty());
values.put(SALE_STOCKQTY, objSale.getStockQty());
values.put(SALE_FinalProduct, objSale.getFinalProduct());
values.put(SALE_PriceRange, objSale.getPriceRange());
values.put(SALE_CoreNonCore, objSale.getCoreNonCore());
values.put(SALE_Color, objSale.getColor());
values.put(SALE_GSLCode, objSale.getGSLCode());
values.put(SALE_Wanted, objSale.getWanted());
values.put(SALE_Pqty, objSale.getPqty());
values.put(SALE_MRP, objSale.getMRP());
values.put(SALE_PRate, objSale.getPRate());
SQLiteDatabase db = this.getWritableDatabase();
db.insert(TABLE_SALE, null, values);
db.close();
return true;
}
And this one is my Asynk Task. Where i fetch data from webservice and insert into SQLite
protected void onPostExecute(String result)
{
try {
String l = result.replace("\\", "");
l = l.replace("''", "");
String sdsd = l.substring(1, l.length() - 1);
JSONArray jsonArray = new JSONArray(sdsd);
Log.i("JSON", "Number of surveys in feed: " +jsonArray.length());
/*if(db.delSaleData()){
}*/
for (int i = 0; i < jsonArray.length(); i++)
{
JSONObject jsonObject = jsonArray.getJSONObject(i);
sl.setBranch(jsonObject.getString("Branch"));
sl.setSupplier(jsonObject.getString("Supplier"));
sl.setBuyer(jsonObject.getString("Buyer"));
sl.setCat1(jsonObject.getString("Cat1"));
sl.setCat2(jsonObject.getString("Cat2"));
sl.setCat3(jsonObject.getString("Cat3"));
sl.setCat4(jsonObject.getString("Cat4"));
sl.setCat5(jsonObject.getString("Cat5"));
sl.setCat6(jsonObject.getString("Cat6"));
sl.setDesigNo(jsonObject.getString("DesigNo"));
sl.setItemSize(jsonObject.getString("ItemSize"));
sl.setSaleQty(jsonObject.getString("SaleQty"));
sl.setStockQty(jsonObject.getString("StockQty"));
sl.setFinalProduct(jsonObject.getString("FinalProduct"));
sl.setPriceRange(jsonObject.getString("PriceRange"));
sl.setCoreNonCore(jsonObject.getString("CoreNonCore"));
sl.setColor(jsonObject.getString("Color"));
sl.setGSLCode(jsonObject.getString("GSLCode"));
sl.setWanted(jsonObject.getString("Wanted"));
sl.setPqty(jsonObject.getString("Pqty"));
sl.setMRP(jsonObject.getString("MRP"));
sl.setPRate(jsonObject.getString("PRate"));
if(db.addSale(sl))
{
//Toast.makeText(getApplicationContext(), " Insert.." , Toast.LENGTH_SHORT).show();
}
}
setData();
} catch (Exception e) {
e.printStackTrace();
}
dialog.dismiss();
setTableData("All");
}
use the SQLiteStatement for example:
private void bulkInsertRecords(String[] records) {
String sql = "INSERT INTO "+ SAMPLE_TABLE_NAME +" VALUES (?,?,?);";
SQLiteStatement statement = sampleDB.compileStatement(sql); //Este é o prepare
sampleDB.beginTransaction();
for (int i = 0; i<records.length; i++) {
statement.clearBindings();
statement.bindString(1, records[0]);
statement.bindString(2, records[1]);
statement.bindString(3, records[2]);
statement.execute();
}
sampleDB.setTransactionSuccessful();
sampleDB.endTransaction();
}
Don't open and close the database every time you want to do an insert. Open it once and then close it when exiting the app.
Use the Asyncclass. Do the operation onBackground, not in the main UI thread.
UPDATE
On your code, you insert the SQLite via onPost, not onBackground. onPost do things on UI thread, while onBackground do it on the separate (background) thread so it doesnt effect the UI. So move the operation code to onBackground.
I have created complied statement given below. Now my question is how to get resultset of the query.
Here is my code:
DataBaseHelper dbHelper=new DataBaseHelper(context);
dbHelper.createDataBase();
dbHelper.openDataBase();
SQLiteDatabase db = dbHelper.getWritableDatabase();
SQLiteStatement st=db.compileStatement("select taskid from task where taskdate=?");
st.bindString(1,"2011/09/05");
st.execute();
This works without any error. But I want the result set of the given query. Please help..
The result set isn't available, at least for now, in sqlite. It all depends on exactly what information you want from the ResultSet or ResultSetMetaData, etc, but there are other means of obtaining almost the same information.
You can get detailed information about the columns in a table with the following, used as if it were a SELECT, and the information about the columns will be presented:
pragma table_info(myTable) ;
See http://www.sqlite.org/pragma.html#pragma_table_info for more information.
If you want the information concerning a specific SELECT, you can get information from the resulting Cursor. See http://developer.android.com/reference/android/database/Cursor.html
For example, if you want the type of data for a column, you can use the getType() method in the newer versions of Android, or use a series of "get" functions to determine at least what type is readable, with this horrible code:
Cursor curs = db.rawQuery(sqlStr, null);
int numberOfColumns = curs.getColumnCount();
String []colNames = new String[numberOfColumns];
String []colTypes = new String[numberOfColumns];
for(int iCol=1; iCol<=numberOfColumns; iCol++) {
colNames[iCol-1] = curs.getColumnName(iCol-1);
colTypes[iCol-1] = null; //curs.getType(iCol);
}
while(curs.moveToNext()) {
// this code assumes that the first row has the same data types
// as the rest of the rows
for(int iCol=1; iCol<=numberOfColumns; iCol++) {
String colName = colNames[iCol-1];
String colType = colTypes[iCol-1];
if(colType==null) {
// determine column type
try {
curs.getString(iCol-1);
colType = colTypes[iCol-1] = "text";
} catch (Exception ignore) {
try {
curs.getLong(iCol-1);
colType = colTypes[iCol-1] = "integer";
} catch (Exception ignore1) {
try {
curs.getFloat(iCol-1);
colType = colTypes[iCol-1] = "real";
} catch (Exception ignore2) {
try {
curs.getBlob(iCol-1);
colType = colTypes[iCol-1] = "blob";
} catch (Exception ignore3) {
colType = colTypes[iCol-1] = "other";
}
}
}
}
}
if("text".equals(colType)) {
... curs.getString(iCol-1);
} else
if("real".equals(colType)) {
... curs.getDouble(iCol-1);
} else
if("integer".equals(colType)) {
... curs.getInt(iCol-1);
} else { // unknown type
... colType+"-"+curs.getString(iCol-1);
}
}
}
Other information is available in a similar manner, depending on your need.