I tried using this code to retrieve the image from database which is stored in blob format
but this method does not work and my application crashes whenever this method is called
so how can I retrieve my image in byte or bitmap format from the database.
because I want to use that imageswitcher
My logcat shows
07-26 18:12:51.253: E/string1(15682): --Hatheesingh jain temple
07-26 18:12:51.844: E/SQLiteLog(15682): (1) near "jain": syntax error
07-26 18:12:51.884: E/AndroidRuntime(15682): FATAL EXCEPTION: main
07-26 18:12:51.884: E/AndroidRuntime(15682): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.starting.starting/com.starting.starting.detailpage}: android.database.sqlite.SQLiteException: near "jain": syntax error (code 1): , while compiling: SELECT * FROM database WHERE place_name =Hatheesingh jain temple
07-26 18:12:51.884: E/AndroidRuntime(15682): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2245)
07-26 18:12:51.884: E/AndroidRuntime(15682): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2299)
public byte[] get_image(String str){
//openDataBase();
Cursor c1 = checkDB.rawQuery("SELECT * FROM database WHERE place_name =" +str, null);
byte[] img = c1.getBlob(10);
return img;
//CloseDataBase();
}
After looking at your LogCat, it's SQL syntax error in your Query. You haven't wrapped the the string in single quotes so it is SQL Compile error, as clearly mentioned in LogCat
Change
Cursor c1 = checkDB.rawQuery("SELECT * FROM database WHERE place_name =" +str, null);
To
Cursor c1 = checkDB.rawQuery("SELECT * FROM database WHERE place_name ='" + str + "'", null);
Besides this, also call c1.moveToFirst() to avoid CursorIndexOutOfBoundsException
database is a reserved keyword in SQLite. You can not use it as a table name
Look at this SQLite reserved keywords
Also you need to call
c1.moveToFirst();
before
byte[] img = c1.getBlob(10);
or else you will get an CursorIndexOutOfBoundsException.
Related
I want to insert "women's cloth" in the database, so I use
String name = DatabaseUtils.sqlEscapeString("women's cloth");
but it gives out put like
"'Women''s Clothing'"
and while inserting app crashes and gives
E/SQLiteLog: (1) near ",": syntax error
--------- beginning of crash -----------------
07-12 12:42:19.628 22387-22521/com.i4ustores E/AndroidRuntime: FATAL EXCEPTION: Thread-8634
android.database.sqlite.SQLiteException: near ",": syntax error (code 1):
From your simple code, I guess you create SQL statement manually.
Try using String.format:
String query = String.format("SELECT * FROM %s WHERE %s = ?",
TABLE_NAME, SEARCH_KEY);
Read more at: Local Databases with SQLiteOpenHelper
Or try using PreparedStatement like below:
PreparedStatement pstmt = con.prepareStatement("UPDATE EMPLOYEES
SET SALARY = ? WHERE ID = ?");
pstmt.setBigDecimal(1, 153833.00)
pstmt.setInt(2, 110592)
Please be aware that the parameterIndex (i.e 1 and 2 in above code) start from 1.
I'm getting the following error while compiling a SELECT query:
Caused by: android.database.sqlite.SQLiteException: near "group": syntax error (code 1): , while compiling: SELECT group_id, logo FROM group WHERE group_name = 'Empty Group'
The query is created as:
c = database.query(TABLE_GROUP, new String[]{KEY_GROUPID, KEY_LOGO}, KEY_GROUPNAME + " = '" + description + "'", null, null, null, null);
with:
TABLE_GROUP = "group";
KEY_LOGO = "logo";
KEY_GROUPID = "group_id";
and the creation script for the table:
create table group
(group_id integer primary key autoincrement,
group_name text not null,
logo string);
Anyone knows what's wrong?
group is a SQLITE keyword and, as every reserved words, it can't use it as table/column name. To fix chose another name for your column. You can find a list of the SQLITE reserved keywords here
Anyone con find the error in these query?
String[] column = {"titolo","regista","cast","descrizione"};
String selection = "_id_film = ? ";
String[] selection_args = {Integer.toString(selectedfilm+1)};
Cursor cursor = db.query("T_FILM", column, selection,selection_args, null, null, null);
logcat says:
android.database.sqlite.SQLiteException: syntax error near"," (code 1) :, while compling SELECT titolo,regista,cast,descrizione FROM T_FILM WHERE _id_film=?
"Cast" is a reserved operation in SQLLite. So it's trying to interpret the column name "cast" as an operation. Your easiest answer is to change the column name to something else.
I am trying to run a select using sqllite. Its my first time using sqllite and I am getting this error over and over.
function ="Adding Event
CODE:
public List<Example> getExampleByFunctionList(String function)
{
List<Example> examplelist = new ArrayList<Example>();
String getQuery = "SELECT * FROM " + MySQLiteHelper.TABLE_EXAMPLE+
" where "+ MySQLiteHelper.COLUMN_EXAMPLE_FUNCTION +" = "+""+function+"";
Cursor cursor = database.rawQuery(getQuery, null);
cursor.moveToFirst();
while (!cursor.isAfterLast()) {
Example example = cursorToExample(cursor);
examplelist.add(example);
cursor.moveToNext();
}
cursor.close();
return examplelist;
}
ERROR:
02-12 12:37:29.218: E/AndroidRuntime(3165): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.tutorial/com.tutorial.ManageCalendar}: android.database.sqlite.SQLiteException: near "Events": syntax error: , while compiling: SELECT * FROM example where examplefunction = Adding Events
... +" = '"+function+"'";
^^^ ^^^
You need to quote string literals in SQL statements (regardless of whether they contain spaces or not).
Or use prepared statements and bind variables, which is much safer against SQL injection.
See: How do I use prepared statements in SQlite in Android?
I am trying to get column info from a cursor, and getting a frustrating error. I want to select all columns containing $SEARCH. Here is the code:
Bundle b = getIntent().getExtras();
SEARCH = b.getString("searchtext");
Cursor c = mDBHelper.getReadableDatabase().query("table", null, "name="+ SEARCH, null, null, null, null);
For some reason, the cursor is throwing a runtime exception. here is the error:
12-30 03:55:00.357: E/AndroidRuntime(1302): Caused by: android.database.sqlite.SQLiteException: near "CAP": syntax error: , while compiling: SELECT * FROM kroger WHERE name=john
Not sure why this is happening, there's probably a very simple error in my code, but I'm not sure what it is. THanks for your help!
"name="+SEARCH will become name=CAP, which isn't a valid SQL expression. The naïve solution is to wrap the term in single-quotes:
"name='" + SEARCH + "'"
But this is subject to SQL injection attacks. Use the argument-passing facilities to pass the search term in an injection-free manner:
query("table", null, "name=?", new String[] {SEARCH}, null, null, null);