This question already has answers here:
android.database.sqlite.SQLiteException: no such column
(5 answers)
Closed 6 years ago.
i am fetching and match value from database table column but its give exception no such column name in android butandroid.database.sqlite.SQLiteException: no such column:speech but speech is value in column but its give this error
i am using this code
public List<TextJson> getDetailtospinner(String text_spinner1) {
SQLiteDatabase db = this.getReadableDatabase();
List<TextJson> textList = null;
try{
textList = new ArrayList<TextJson>();
String[] columns = new String[]{KEY_ID,KEY_NAME};
Cursor cursor = db.query(TABLE_TEXTS, columns, KEY_NAME + "=" + text_spinner1, null, null, null, null);
//Cursor cursor = db.rawQuery("SELECT * FROM "+TABLE_TEXTS + " WHERE " + KEY_CATE + " = " + text_spinner1 , null);
int rows = cursor.getCount();
if (cursor != null) {
Log.e("0_0","0_0 1=>");
for(int i=0;i<rows;i++) {
while (cursor.moveToNext()) {
TextJson txtlist = new TextJson();
Log.e("0_0", ":( ==>");
txtlist.setId(cursor.getInt(0));
txtlist.setText_status(cursor.getString(1));
textList.add(txtlist);
}
}
}
db.close();
}catch (Exception e){
Log.e("error","is==> " +e);
}
return textList;
}
Enclose where condition value with ' if the values is not integer.
Cursor cursor = db.rawQuery("SELECT * FROM "+TABLE_TEXTS + " WHERE " + KEY_CATE + " = '" + text_spinner1 +"'", null);
Related
I am new to the android i am trying to get the cursor values related to the particular column value. but it's showing null.
public int getDashBoardCount() {
String countQuery = "SELECT * FROM " + DASHBOARD_TABLE + " WHERE " + VECHICAL_TYPE + " = " + 'c';
From here also i am getting no such column: c (code 1): , while compiling: SELECT * FROM dashboard_table WHERE vehicle_type = c
String countQuery = "SELECT * FROM " + DASHBOARD_TABLE;
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.rawQuery(countQuery, null);
int cnt = cursor.getCount();
cursor.close();
return cnt;
}
public DashBoardAvaCapModel getVechicalRelAva(String vechical_type) {
SQLiteDatabase db = null;
Cursor cursor = null;
DashBoardAvaCapModel dash = null;
try {
db = this.getReadableDatabase();
cursor = db.query(DASHBOARD_TABLE, new String[]{_ID,
AVAILABLE, CAPACITY, BOOKED, VECHICAL_TYPE}, VECHICAL_TYPE + "=?",
new String[]{String.valueOf(vechical_type)}, null, null, null, null);
int idkey = cursor
.getColumnIndex(_ID);
int available = cursor
.getColumnIndex(AVAILABLE);
int capactity = cursor
.getColumnIndex(CAPACITY);
int booked = cursor
.getColumnIndex(BOOKED);
int vechicaltype = cursor
.getColumnIndex(VECHICAL_TYPE);
if (cursor != null && cursor.moveToFirst())
dash = new DashBoardAvaCapModel(Long.valueOf(cursor.getString(idkey)),
cursor.getString(available), cursor.getString(capactity), cursor.getString(booked), cursor.getString(vechicaltype));
Log.e("dash", "" + cursor.getString(available));
} catch (final Exception ex) {
String exp = String.valueOf(ex);
Log.e("Databa", "" + exp);
} finally {
cursor.close();
db.close();
}
return dash;
}
After writing this line
cursor = db.query(DASHBOARD_TABLE, new String[]{_ID,
AVAILABLE, CAPACITY, BOOKED, VECHICAL_TYPE}, VECHICAL_TYPE + "=?",
new String[]{String.valueOf(vechical_type)}, null, null, null, null);
pls try to write
cursor.moveToFirst();
For first one you can use like
String countQuery = "SELECT * FROM " + DASHBOARD_TABLE + " WHERE " + VECHICAL_TYPE + " = '" + c + "'"
where c is any variable
Hope this will help you
Im trying to get the data of an entire column into a string array. My database contains two columns Id and Names. I want to read all the entries of the names column and put it into a array. Please help.
EDIT #1:
Im using the following code but i can get only one name with this code.
String query = "Select * FROM " + TABLE_APPS + " WHERE " + COLUMN_NAME + " = \"" + productname + "\"";
SQLiteDatabase db = this.getWritableDatabase();
Cursor cursor = db.rawQuery(query, null);
if (cursor.moveToFirst()) {
cursor.moveToFirst();
name = cursor.getString(1);
cursor.close();
} else {
name = null;
}
db.close();
int total=0;
Cursor csr=sdb.query("tablename", null, null,null,null,null,null);
csr.moveToFirst();
while(!csr.isAfterLast())
{
total++;
csr.moveToNext();
}
String strarray[] = new String[total];
Cursor csrs=sdb.query("tablename", null, null,null,null,null,null);
csrs.moveToFirst();
int aray=0;
while(!csrs.isAfterLast())
{
strarray[aray]=csrs.getString(1);
aray++;
csrs.moveToNext();
}
This question already has an answer here:
Method to excute query and return results
(1 answer)
Closed 9 years ago.
App won't run. This occurs recurrently:
04-05 21:29:09.570: E/AndroidRuntime(1069): Caused by: java.lang.IllegalArgumentException:
Cannot bind argument at index 1 because the index is out of range.
The statement has 0 parameters.
Main - Calling method
Cursor wow = db.trying("Gold");
text = (TextView) findViewById(R.id.textView13);
String quantity = wow.getString(0); //
text.setText(quantity);
DB Handler - Method
public Cursor trying(String vg){
String q = "SELECT quantity FROM " + TABLE_CONTACTS + " WHERE name=" + "'" + vg +"'";
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.rawQuery(q, new String[] {vg});
if (cursor != null) {
cursor.moveToFirst();
}
return cursor;
}
The problem is
String q = "SELECT quantity FROM " + TABLE_CONTACTS + " WHERE name=" + "'" + vg +"'";
In your query you had already specify the parameter for where condition .After that you are again passing it to query
Cursor cursor = db.rawQuery(q, new String[] {vg});
This makes the confusion .So try to change your query
String q = "SELECT quantity FROM " + TABLE_CONTACTS + " WHERE name = ?";
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.rawQuery(q, new String[] {vg});
OR you go for another approach
String q = "SELECT quantity FROM " + TABLE_CONTACTS + " WHERE name=" + "'" + vg +"'";
Cursor cursor = db.rawQuery(q, null);
if(cursor != null && cursor.getCount()>0){
cursor.moveToFirst();
//do your action
//Fetch your data
}
else {
Toast.makeText(getBaseContext(), "No records yet!", Toast.LENGTH_SHORT).show();
return;
}
Refer
Please let me know why my where clause isn't working. I tried using the query instead of rawquery but no luck.
try {
String categoryex = "NAME";
DBHelper dbHelper = new DBHelper(this.getApplicationContext());
MyData = dbHelper.getWritableDatabase();
Cursor c = MyData.rawQuery("SELECT * FROM " + tableName + where Category = '+categoryex'" , null);
if (c != null ) {
if (c.moveToFirst()) {
do {
String firstName = c.getString(c.getColumnIndex("Category"));
String age = c.getString(c.getColumnIndex("Text_Data"));
results.add( firstName + " Directions: " + age);
}while (c.moveToNext());
}
}
} catch (SQLiteException se ) {
Log.e(getClass().getSimpleName(), "Could not create or Open the database");
} finally {
if (MyData != null)
MyData.execSQL("DELETE FROM " + tableName);
MyData.close();
}
try... (you left out a double-quote before where.
Cursor c = MyData.rawQuery("SELECT * FROM " + tableName + " where Category = '" +categoryex + "'" , null);
I think you should use rawQuery in this form:
rawQuery("SELECT * FROM ? where Category = ?", new String[] {tableName, categoryex});
I think it's more secure this way.
Your quotes are buggered:
Cursor c = MyData.rawQuery("SELECT * FROM " + tableName + " where Category = '" + categoryex + "'" , null);
You also should read up on SQL injection attacks.
it will be more easy if you use this technique instead of rawQuery,its easy way change your table name, columns and where conditions accordingly.
public ArrayList<Invitees> getGroupMembers(String group_name) {
ArrayList<Invitees> contacts = new ArrayList<>();
SQLiteDatabase db = this.getReadableDatabase();
String[] projection = {COLUMN_CONTACT, COLUMN_PHONE_NUMBER};
String selection = COLUMN_GROUP_NAME + "=?";
String[] selectionArgs = {group_name};
Cursor cursor = db.query(GROUPS_TABLE_NAME, projection, selection, selectionArgs, null, null, null);
if (cursor.moveToFirst()) {
do {
Invitees invitees = new Invitees();
invitees.setUserName(cursor.getString(cursor.getColumnIndexOrThrow(COLUMN_CONTACT)));
invitees.setInviteePhone(cursor.getString(cursor.getColumnIndexOrThrow(COLUMN_PHONE_NUMBER)));
contacts.add(invitees);
} while (cursor.moveToNext());
}
return contacts;
}
This question already has an answer here:
Closed 10 years ago.
Possible Duplicate:
Fetch data in database table insert it if not exist else return the row id
I try to fetch if data exist in database table, if yes I should get the row id else I insert it in the table this my code
public int finddate(String date){
int id=0;
AndroidOpenDbHelper androidOpenDbHelperObj = new AndroidOpenDbHelper(this);
SQLiteDatabase sqliteDatabase = androidOpenDbHelperObj.getWritableDatabase();
Cursor cursor = sqliteDatabase.rawQuery("SELECT _id FROM " +
AndroidOpenDbHelper.TABLE_DATE + " where " +
AndroidOpenDbHelper.COLUMN_NAME_DATE + " = " + date,
null);
startManagingCursor(cursor);
if (cursor != null) {
if( cursor.moveToFirst() ) {
id = cursor.getInt(cursor.getColumnIndex("_id"));
}
} else {
id=0;
}
sqliteDatabase.close();
return id;
}
when i try with an existing item I get the result of this method =0 and the item is inserted in the table,
how can I do this?
The date is not being correctly escaped. The safest and cleanest way to resolve it is to pass the date value as a parameter, rather than embedding it in the query.
You can also tidy up the cursor management quite a bit. You don't need to check for a null cursor, as an exception will be thrown if the query fails. Also, you don't need to set id = 0 as you've already done that at the start. This is much simpler:
Cursor cursor = sqliteDatabase.rawQuery("SELECT _id FROM " +
AndroidOpenDbHelper.TABLE_DATE + " where " +
AndroidOpenDbHelper.COLUMN_NAME_DATE + " = ?",
new String[] { date } );
if ( cursor.moveToFirst() ) {
id = cursor.getInt(cursor.getColumnIndex("_id"));
}
cursor.close();
sqliteDatabase.close();
Your query should look like this:
Cursor cursor = sqliteDatabase.rawQuery("SELECT _id FROM " + AndroidOpenDbHelper.TABLE_DATE + " where "+ AndroidOpenDbHelper.COLUMN_NAME_DATE+ " =\'"+date+"\'", null);
startManagingCursor(cursor);
cursor.moveTofirst();
if (cursor.getCount() > 0) {
id = cursor.getInt(cursor.getColumnIndex("_id"));
}
} else {
id=0;
}