I am using the query functions in order to build the SQL queries for my tables. Is there a way to see the actual query that is run? For instance log it somewhere?
So far the best I could do was to have a look at the cursor's member mQuery using a breakpoint. I'd love to output the queries automatically though. This member is of course not public and does not have a getter.
Just for the record, here is an implementation of the accepted answer.
/**
* Implement the cursor factory in order to log the queries before returning
* the cursor
*
* #author Vincent # MarvinLabs
*/
public class SQLiteCursorFactory implements CursorFactory {
private boolean debugQueries = false;
public SQLiteCursorFactory() {
this.debugQueries = false;
}
public SQLiteCursorFactory(boolean debugQueries) {
this.debugQueries = debugQueries;
}
#Override
public Cursor newCursor(SQLiteDatabase db, SQLiteCursorDriver masterQuery,
String editTable, SQLiteQuery query) {
if (debugQueries) {
Log.d("SQL", query.toString());
}
return new SQLiteCursor(db, masterQuery, editTable, query);
}
}
adb shell setprop log.tag.SQLiteStatements VERBOSE
Don't forget to restart your app after setting this property.
It is also possible to enable logging of execution time. More details are availabe here: http://androidxref.com/4.2.2_r1/xref/frameworks/base/core/java/android/database/sqlite/SQLiteDebug.java
You can apply your own SQLiteDatabase.CursorFactory to the database. (See the openDatabase parameters.) This will allow you to create your own subclass of Cursor, which keeps the query in an easily accessible field.
edit: In fact, you may not even have to subclass Cursor. Just have your factory's newCursor() method return a standard SQLiteCursor, but log the query before doing so.
adb shell setprop log.tag.SQLiteLog V
adb shell setprop log.tag.SQLiteStatements V
adb shell stop
adb shell start
Using an SQLiteQueryBuilder it's painfully simple. buildQuery() returns a raw sql string, which can then be logged:
SQLiteQueryBuilder qb = new SQLiteQueryBuilder();
qb.setTables(ExampleTable.TABLE_NAME);
String sql = qb.buildQuery(projection, selection, null, null, sortOrder, null);
Log.d("Example", sql);
So far the best I could do was to have a look at the cursor's member mQuery using a breakpoint. This member is of course not public and does not have a getter, hence, no way to output it. Any better suggestion?
If you are using SQLiteDatabase with it's standard methods as insert, update and delete custom CursorFactory will not be working.
I implemented my not very great but working solution based on SQLiteDatabase class. It just repeats logic of insert, update and delete methods but without statements and actually doing the logging of SQL statements.
public class SQLiteStatementsLogger {
private static final String TAG = SQLiteStatementsLogger.class.getSimpleName();
private static final String[] CONFLICT_VALUES = new String[]
{"", " OR ROLLBACK ", " OR ABORT ", " OR FAIL ", " OR IGNORE ", " OR REPLACE "};
public void logInsert(String table, String nullColumnHack, ContentValues values) {
logInsertWithOnConflict(table, nullColumnHack, values, 0);
}
public static void logInsertWithOnConflict(String table, String nullColumnHack,
ContentValues initialValues, int conflictAlgorithm) {
StringBuilder sql = new StringBuilder();
sql.append("INSERT");
sql.append(CONFLICT_VALUES[conflictAlgorithm]);
sql.append(" INTO ");
sql.append(table);
sql.append('(');
Object[] bindArgs = null;
int size = (initialValues != null && initialValues.size() > 0)
? initialValues.size() : 0;
if (size > 0) {
bindArgs = new Object[size];
int i = 0;
for (String colName : initialValues.keySet()) {
sql.append((i > 0) ? "," : "");
sql.append(colName);
bindArgs[i++] = initialValues.get(colName);
}
sql.append(')');
sql.append(" VALUES (");
for (i = 0; i < size; i++) {
sql.append((i > 0) ? ",?" : "?");
}
} else {
sql.append(nullColumnHack + ") VALUES (NULL");
}
sql.append(')');
sql.append(". (");
for (Object arg : bindArgs) {
sql.append(String.valueOf(arg)).append(",");
}
sql.deleteCharAt(sql.length()-1).append(')');
Log.d(TAG, sql.toString());
}
public static void logUpdate(String table, ContentValues values, String whereClause, String[] whereArgs) {
logUpdateWithOnConflict(table, values, whereClause, whereArgs, 0);
}
public static void logUpdateWithOnConflict(String table, ContentValues values,
String whereClause, String[] whereArgs, int conflictAlgorithm) {
StringBuilder sql = new StringBuilder(120);
sql.append("UPDATE ");
sql.append(CONFLICT_VALUES[conflictAlgorithm]);
sql.append(table);
sql.append(" SET ");
// move all bind args to one array
int setValuesSize = values.size();
int bindArgsSize = (whereArgs == null) ? setValuesSize : (setValuesSize + whereArgs.length);
Object[] bindArgs = new Object[bindArgsSize];
int i = 0;
for (String colName : values.keySet()) {
sql.append((i > 0) ? "," : "");
sql.append(colName);
bindArgs[i++] = values.get(colName);
sql.append("=?");
}
if (whereArgs != null) {
for (i = setValuesSize; i < bindArgsSize; i++) {
bindArgs[i] = whereArgs[i - setValuesSize];
}
}
if (!TextUtils.isEmpty(whereClause)) {
sql.append(" WHERE ");
sql.append(whereClause);
}
sql.append(". (");
for (Object arg : bindArgs) {
sql.append(String.valueOf(arg)).append(",");
}
sql.deleteCharAt(sql.length()-1).append(')');
Log.d(TAG, sql.toString());
}
public static void logDelete(String table, String whereClause, String[] whereArgs) {
StringBuilder sql = new StringBuilder("DELETE FROM " + table);
if (!TextUtils.isEmpty(whereClause)) {
sql.append(" WHERE " + whereClause);
sql.append(". (");
for (Object arg : whereArgs) {
sql.append(String.valueOf(arg)).append(",");
}
sql.deleteCharAt(sql.length()-1).append(')');
}
Log.d(TAG, sql.toString());
}
}
Be aware not to use the logger in release versions. It might increase time of queries executing.
You can check if the build is in debug mode with this code line:
0 != (getApplicationInfo().flags & ApplicationInfo.FLAG_DEBUGGABLE)
If it is for once off scenario, I would suggest injecting an error (e.g. type in expression like LIEK instead of LIKE!) and watch the Eclipse LogCat for any errors! HTH!
If you are using a ContentProvider to access the DB, this is how I got it logging the queries. Not a perfect solution, but it works for development
#Override
public boolean onCreate() {
dbHelper = new MySQLiteHelper(getContext());
database=dbHelper.getWritableDatabase();
if(!database.isReadOnly())
database.execSQL("PRAGMA foreign_keys=ON;");
return true;
}
SQLiteDatabase.CursorFactory cursorFactory = new SQLiteDatabase.CursorFactory() {
#Override
public Cursor newCursor(SQLiteDatabase db, SQLiteCursorDriver masterQuery, String editTable, SQLiteQuery query) {
Log.d(TAG, "Query: "+query);
return new SQLiteCursor(db, masterQuery, editTable, query);
}
};
#Override
public Cursor query(Uri uri, String[] projection, String selection,
String[] selectionArgs, String sortOrder) {
String table =getTableName(uri);
if(Constants.LOG_QUERIES){
database = SQLiteDatabase.openOrCreateDatabase(database.getPath(), cursorFactory);
}
Cursor cursor =database.query(table, projection, selection, selectionArgs, null, null, sortOrder);
cursor.moveToFirst();
return cursor;
}
It'll throw a DatabaseNotClosed exception, but you'll be able to see the query
Personnally I log text using java.util.Log and the Log.w("MYAPPNAME", "My text...") function. It shows up in the Log view of Eclipse and it can be filtered to output only the logs for "MYAPPNAME".
Related
I'm trying to get the value or data from the array that doesn't exists in the database.
public Cursor checkExistence(){
Cursor c=null;
String[] values={"headache","cold"};
SQLiteDatabase db= getReadableDatabase();
String query="SELECT * FROM "+TABLE_SYMPTOMS+" WHERE "+COLUMN_SYMP+" IN ("+toArrayRep(values)+")";
c=db.rawQuery(query,null);
Log.i("From Cursor","Cursor Count : " + c.getCount());
if(c.getCount()>0){
String val= c.getString()
Log.i("From Cursor","No insertion");
}else{
Log.i("From Cursor","Insertion");
}
db.close();
return c;
}
public static String toArrayRep(String[] in) {
StringBuilder result = new StringBuilder();
for (int i = 0; i < in.length; i++) {
if (i != 0) {
result.append(",");
}
result.append("'" + in[i] + "'");
}
return result.toString();
}
In the String values={"headache","cold"} ,headache exists but cold does not exist in the database. From the code above, the Cursor returns Count=1 which is count>0 hence i can't insert into table.I would like to know how i can independently check whether the individual data exists, and the one which doesn't exist will be inserted into table.So in this case, "Cold" would be able to be inserted into the table.
If you use a single query to check all values, then what you get is a list of existing values, and you still have to search in the original list for any differences.
It is simpler to check each value individually:
String[] values = { "headache", "cold" };
SQLiteDatabase db = getReadableDatabase();
db.beginTransaction();
try {
for (String value : values) {
long count = DatabaseUtils.queryNumEntries(db,
TABLE_SYMPTOMS, COLUMN_SYMP+" = ?", new String[] { value });
if (count == 0) {
ContentValues cv = new ContentValues();
cv.put(COLUMN_SYMP, value);
db.insert(TABLE_SYMPTOMS, null, cv);
}
}
db.setTransactionSuccessful();
} finally {
db.endTransaction();
}
You need check Cursor.moveToFirst()
True = Have records in cursor.
False = Dont have records.
Example my code:
return database.query( table.getNameTable(),
table.getColumns(),
table.getWhereSelectTableScript(),
null,
table.getGroupBySelectTableScript(),
table.getHavingSelectTableScript(),
table.getOrderBySelectTableScript(),
table.getLimitRecordsSelectTableScript());
See more here !
I use this method for retrieving my data
public String getdata() {
String[] columns= new String[]{RowId,RowBusinessName};
Cursor c=OurDatabase.query(TableName,columns,null,null,null,null,null);
String Result="";
int iRowId=c.getColumnIndex(RowId);
int iRowBusinessName=c.getColumnIndex(RowBusinessName);
for(c.moveToFirst();!c.isAfterLast();c.moveToNext()){
Result=Result+c.getString(iRowBusinessName)+"\n";
}
return Result;
}
How can I make it return structured data (id & business_name)?
I want to display every business_name in a single textview.
Please help
If I understand what you are trying to do, here is the solution if you want to get only 1 RowBusinessName returned as a String. (Hoping that your RowBusinessName is type String).
public String getdata(int rowId) {
String[] columns= new String[]{RowId,RowBusinessName};
Cursor cursor = db.query(TABLENAME, columns, RowId + "=?", new String[]{rowId + ""}, null, null, null, null);
String Result="";
if (cursor != null && cursor.moveToFirst()) {
// not required though
int rowId = cursor.getInt(cursor.getColumnIndexOrThrow(RowId));
String rowBusinessName = cursor.getString(cursor.getColumnIndexOrThrow(RowBusinessName));
result = rowBusinessName;
}
return result;
}
Now if you want a list of RowBusinessName, then you have to build a List<String> rather than appending it to Result. That's not really a good way!
public List<String> getAll() {
List<String> businessNameList = new ArrayList<String>();
String[] columns= new String[]{RowId,RowBusinessName};
Cursor c=OurDatabase.query(TableName,columns,null,null,null,null,null);
if (c != null && c.moveToFirst()) {
// loop until the end of Cursor and add each entry to Ticks ArrayList.
do {
String businessName = cursor.getString(cursor.getColumnIndexOrThrow(RowBusinessName));
if (businessName != null) {
businessNameList.add(businessName);
}
} while (c.moveToNext());
}
return businessNameList;
}
These are work around.
The appropriate answer would be to create an Object that holds id and businessName. That way, you build an object from DB and just return the entire Object.
I am deleting a database on Clicking a button and my delete method is as follows
public int deleteDatabase(String tableName) {
SQLiteDatabase db = database.getWritableDatabase();
String whereClause = null; // delete all rows
String[] whereArgs = { null };
int count = db.delete(tableName, whereClause, whereArgs);
db.close();
return count;
}
if (DatabaseHelper.getInstance(getApplicationContext())
.isTableExists(MY_TABLE)) {
Log.d(TAG, "Table exist, delete database");
deleteDatabase(MY_LIST_TABLE);
}
and the error is as follows: Too many bind arguments. 1 arguments were provided but the statement needs 0 arguments.
String whereClause = null; // delete all rows
String[] whereArgs = { null };
int count = db.delete(tableName, whereClause, whereArgs);
if your provide a whereArgs, you have to provide also a valid where clause (with the ?placeholder). Change it like
int count = db.delete(tableName, null, null);
I'm running an update query that compiles. But when I test the results, there seem to be no changes. Everything looks good to me, but clearly something is wrong. Can anyone see what I am missing. Pretty new to SQLite, so apologies if it's something simple. Thanks!
public static Boolean updateHealth (int unitVal, int oldValue, int newValue) {
String unit = Integer.toString(unitVal);
String oldVal = Integer.toString(oldValue);
String newVal = Integer.toString(newValue);
System.err.printf("old val: %s, new val: %s\n", oldVal, newVal);
SQLiteDatabase db = myDBOpenHelper.getWritableDatabase();
String where = UNIT_COLUMN + " = " + unit + " AND " + HEALTH_COLUMN + " = " + oldVal;
Cursor cursor = db.query(UNITS_TABLE, new String[] {UNIT_COLUMN}, where, null, null, null, null);
if (cursor != null) {
/* the record doesn't exist, cancel the operation */
return false;
}
ContentValues updatedValues = new ContentValues();
updatedValues.put(HEALTH_COLUMN, newVal);
/* SQL query clauses */
String whereArgs[] = null;
db.update(UNITS_TABLE, updatedValues, where, whereArgs);
return true;
}
The cursor is not null when no row is retrieved. So you have to replace the line if (cursor != null) { by if(!cursor.moveToNext()) {
By the way, you don't need to query the database before updating. You can do the update, see how many rows have been affected and return true if the number of affected rows is > 0, false otherwise. The number of affected rows is returned by the method update.
My goal
I want to insert multiple records into sqlite in batches (transactionally).
My question
I found the method android.content.ContentResolver.bulkInsert(..) interesting but the javadoc states:
This function make no guarantees about the atomicity of the insertions.
Why would android provide a method that is crippled ? Can you name me usecases for non-atomic insertions ? I am going to obviously going to override ContentProvider.bulkInsert(..) to ensure atomicity myself so I'm not sure why it is phrase like this.
We need to override the bulk insert method like following...
public class Provider extends ContentProvider {
public static final Uri URI = Uri.parse("content://com.example.android.hoge/");
#Override
public String getType(Uri uri) {
return null;
}
#Override
public boolean onCreate() {
return false;
}
#Override
public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) {
Helper helper = Helper.getInstance(getContext(), null);
SQLiteDatabase sdb = helper.getReadableDatabase();
Cursor cursor = sdb.query(
Table.TABLENAME,
new String[]{Table.ID, Table.DATA, Table.CREATED},
selection,
selectionArgs,
null,
null,
sortOrder,
null
);
return cursor;
}
#Override
public Uri insert(Uri uri, ContentValues values) {
Helper helper = Helper.getInstance(getContext(), null);
SQLiteDatabase sdb = helper.getWritableDatabase();
sdb.insert(Table.TABLENAME, null, values);
getContext().getContentResolver().notifyChange(uri, null);
return uri;
}
/**
* super.bulkInsert is implemented the loop of insert without transaction
* So we need to override it and implement transaction.
*/
#Override
public int bulkInsert(Uri uri, ContentValues[] values) {
Helper helper = Helper.getInstance(getContext(), null);
SQLiteDatabase sdb = helper.getWritableDatabase();
sdb.beginTransaction();
SQLiteStatement stmt = sdb.compileStatement(
"INSERT INTO `" + Table.TABLENAME + "`(`" + Table.DATA + "`, `" + Table.CREATED + "`) VALUES (?, ?);"
);
int length = values.length;
for(int i = 0; i < length; i++){
stmt.bindString(1, values[i].getAsString(Table.DATA));
stmt.bindLong(2, values[i].getAsLong(Table.CREATED));
stmt.executeInsert();
}
sdb.setTransactionSuccessful();
sdb.endTransaction();
return length;
}
#Override
public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) {
Helper helper = Helper.getInstance(getContext(), null);
SQLiteDatabase sdb = helper.getWritableDatabase();
int rows = sdb.update(Table.TABLENAME, values, selection, selectionArgs);
getContext().getContentResolver().notifyChange(uri, null);
return rows;
}
#Override
public int delete(Uri uri, String selection, String[] selectionArgs) {
Helper helper = Helper.getInstance(getContext(), null);
SQLiteDatabase sdb = helper.getWritableDatabase();
int rows = sdb.delete(Table.TABLENAME, selection, selectionArgs);
getContext().getContentResolver().notifyChange(uri, null);
return rows;
}
private static class Helper extends SQLiteOpenHelper {
static Helper INSTANCE = null;
private Helper(Context context, CursorFactory factory) {
super(context, Table.FILENAME, factory, Table.VERSION);
}
public static Helper getInstance(Context context, CursorFactory factory) {
if (INSTANCE == null) {
INSTANCE = new Helper(context, factory);
}
return INSTANCE;
}
#Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(
"CREATE TABLE `" + Table.TABLENAME + "`(" +
" `" + Table.ID + "` INTEGER PRIMARY KEY AUTOINCREMENT," +
" `" + Table.CREATED + "` INTEGER," +
" `" + Table.DATA + "` TEXT" +
");"
);
}
#Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
}
}
Use applyBatch() instead.
This allows you to perform many different operations in a transactional manner, however there is a performance hit for this fexibility.
The relevant documentation can be found here in the ContentResolver SDK documentation
I have provided a quick tutorial on using applybatch in the symantics of backReferences
I also recommend looking at this question which discusses overriding applyBatch
This function make no guarantees about the atomicity of the
insertions.
Correct me if I'm wrong but this is because we have no idea whether the given content provider overrides the bulkInsert() method unless it is our own provider. If the bulkInsert() method is not overriden, default implementation will iterate over the values and call insert(Uri, ContentValues) on each of them. It should be fine if you are using your own provider and know that you have implemented the bulkInsert() method like following example and use the endTransaction() method in finally block:
#Override
public int bulkInsert(Uri uri, ContentValues[] values) {
final SQLiteDatabase db = mOpenHelper.getWritableDatabase();
final int match = sUriMatcher.match(uri);
switch (match) {
case WEATHER:
db.beginTransaction();
int returnCount = 0;
try {
for (ContentValues value : values) {
normalizeDate(value);
long _id = db.insert(WeatherEntry.TABLE_NAME,
null, value);
if (_id != -1) {
returnCount++;
}
}
db.setTransactionSuccessful();
} finally {
db.endTransaction();
}
getContext().getContentResolver().notifyChange(uri, null);
return returnCount;
default:
return super.bulkInsert(uri, values);
}
}
First Add method for bulk insert in your content provider
#Override
public int bulkInsert(#NonNull Uri uri, #NonNull ContentValues[] values) {
switch (uriMatcher.match(uri)) {
case USERS:
for (ContentValues value : values) {
long rowID = sqLiteDatabase.insert(YOUR_TABLENAME, "", value);
if (rowID > 0) {
Uri _uri = ContentUris.withAppendedId(CONTENT_URI, rowID); //append ID into CONTENT_URI
getContext().getContentResolver().notifyChange(_uri, null);
return values.length; //return total number of data inserted
}
}
break;
}
return super.bulkInsert(uri, values);
}
Add below code in button click (Below is executed to insert bulk data)
String userName = editTextUserName.getText().toString();
String userCity = editTextUserCity.getText().toString();
Log.d("BulkInsert", "onClick: -------START------");
ContentValues[] contentValue = new ContentValues[5000];
for (int i = 0; i < 5000; i++) {
contentValue[i] = new ContentValues(); // initialize Array of content values
//store data in content values object
contentValue[i].put(UserModel.USER_CITY, userCity);
contentValue[i].put(UserModel.USER_NAME, userName);
contentValue[i].put(UserModel.USER_PINCODE, userPincode);
}
int count = getContentResolver().bulkInsert(YOUR_TABLE_URI, contentValue); //insert data
Log.d("BulkInsert", "onClick: " + count); //Display number of data inserted
Log.d("BulkInsert", "onClick: -------STOP------");