I'm trying to retrieve the data of a particulate row in a sqlite database . and trying to display it to on a EditText but I'm getting this error , Kindly help me in solving the error !
Thanks in advance :)
here is the logcat data showing error
12-31 22:28:17.731: E/2.4(12285): 111111
12-31 22:28:17.741: E/2.5(12285): 111111
12-31 22:28:17.771: E/SQLiteLog(12285): (1) no such column: _id10
12-31 22:28:17.791: D/AndroidRuntime(12285): Shutting down VM
here is the code : ( of the function getName , which is being used to retrieve the data of column 1 ) [getting error after log.e("2.5".... ) ]
public String getName(long l) {
// TODO Auto-generated method stub
Log.e("2.4","111111");
String[] column = new String[]{KEY_ROWID, KEY_NAME, KEY_HOTNESS };
Log.e("2.5","111111");
Cursor c = ourDatabase.query(DATABASE_TABLE, column, KEY_ROWID + "" + l, null, null, null, null);
Log.e("2.6","111111");
if(c != null)
{
Log.e("2.7","111111");
c.moveToFirst();
Log.e("2.8","111111");
String name = c.getString(1);
Log.e("2.9","111111");
return name;
}
return null;
}
public String getHotness(long l)
{
Log.e("3.1","111111");
// TODO Auto-generated method stub
String[] column = new String[]{KEY_ROWID, KEY_NAME, KEY_HOTNESS };
Log.e("3.2","111111");
Cursor c = ourDatabase.query(DATABASE_TABLE, column, KEY_ROWID + "" + l, null, null, null, null);
Log.e("3.3","111111");
if(c != null)
{
Log.e("3.4","111111");
c.moveToFirst();
Log.e("3.5","111111");
String hotness = c.getString(2);
Log.e("3.6","111111");
return hotness;
}
return null;
}
And is the code of the activity which is calling this function
case R.id.bgetInfo:
String s = sqlRow.getText().toString();
long l = Long.parseLong(s);
HotOrNot hon = new HotOrNot(this);
hon.open();
String returnedName = hon.getName(l);
String returnedHotness = hon.getHotness(l);
hon.close();
Log.e("1.2","111111");
sqlName.setText(returnedName);
Log.e("1.3","111111");
sqlHotness.setText(returnedHotness);
Log.e("1.4","111111");
break;
I think you missed a equal sign at:
Cursor c = ourDatabase.query(DATABASE_TABLE, column, KEY_ROWID + "" + l, null, null, null, null);
Change to:
Cursor c = ourDatabase.query(DATABASE_TABLE, column, KEY_ROWID + " = " + l, null, null, null, null);
Related
I want to get data based on a fragment of the name , but why the error with a description of the missing return statement ?
public Cursor getName (String nama){
Cursor c = db.query(Konstanta.NAMA_TABEL, new String[]{
Konstanta.ID_ARTIS,
Konstanta.NAMA_ARTIS,
Konstanta.GENDER_ARTIS_PRIA,
Konstanta.GENDER_ARTIS_WANITA,
Konstanta.TGLAHIR}, Konstanta.NAMA_ARTIS + "LIKE '%" + nama + "%'",
null, null, null, null, null);
if (c != null){
c.moveToFirst();
return c;
}
}
Your method returns a result only if c is not null. It must return a result in all scenarios, i.e. even if c is null.
You can change
if (c != null){
c.moveToFirst();
return c;
}
to
if (c != null){
c.moveToFirst();
}
return c;
Every method which has a return value must return the described bean. You need to return null or throw a custom exception if c is not null.
public Cursor getName (String nama){
Cursor c = db.query(Konstanta.NAMA_TABEL, new String[]{
Konstanta.ID_ARTIS,
Konstanta.NAMA_ARTIS,
Konstanta.GENDER_ARTIS_PRIA,
Konstanta.GENDER_ARTIS_WANITA,
Konstanta.TGLAHIR}, Konstanta.NAMA_ARTIS + "LIKE '%" + nama + "%'",
null, null, null, null, null);
if (c != null){
c.moveToFirst();
return c;
}
throw new IllegalStateException("c is null");
}
add below line -
return null;
in your code block -
public Cursor getName (String nama){
Cursor c = db.query(Konstanta.NAMA_TABEL, new String[]{
Konstanta.ID_ARTIS,
Konstanta.NAMA_ARTIS,
Konstanta.GENDER_ARTIS_PRIA,
Konstanta.GENDER_ARTIS_WANITA,
Konstanta.TGLAHIR}, Konstanta.NAMA_ARTIS + "LIKE '%" + nama + "%'",
null, null, null, null, null);
if (c != null){
c.moveToFirst();
return c;
}
return null;
}
Your method must return Cursor type value but you are returning value only in a if loop
do this way :
public Cursor getName (String nama){
Cursor c =null;
c = db.query(Konstanta.NAMA_TABEL, new String[]{
Konstanta.ID_ARTIS,
Konstanta.NAMA_ARTIS,
Konstanta.GENDER_ARTIS_PRIA,
Konstanta.GENDER_ARTIS_WANITA,
Konstanta.TGLAHIR}, Konstanta.NAMA_ARTIS + "LIKE '%" + nama + "%'",
null, null, null, null, null);
if (c != null){
c.moveToFirst();
return c;
}
return c;
}
I would like to return a true / false value when the string match with the column of sqlite. How can I implement the search function?
How to use cursor to do searching?
public boolean searchLuggage(String code) {
ArrayList<String> luggageCheck = new ArrayList<String>();
SQLiteDatabase db = this.getReadableDatabase();
try {
Cursor cursor = db.query(true, TABLE_LUGGAGES, new String[] { your field list }, SearchColumnname + "='" + code + "'", null, null, null, null, null);
if (cursor != null) {
cursor.moveToFirst();
for (int i = 0; i < cursor.getCount(); i++) {
luggageCheck.add(cursor.getString(0));
cursor.moveToNext();
}
cursor.close();
return true;
}
return false;
} catch (Exception e) {
e.printStackTrace();
}
return true;
}
// Creating Tables
#Override
public void onCreate(SQLiteDatabase db) {
String CREATE_LUGGAGE_TABLE = "CREATE TABLE " + TABLE_LUGGAGES + "(" + KEY_NAME + " TEXT NOT NULL," + KEY_CODE + " TEXT NOT NULL," + KEY_NUMBER + " TEXT NOT NULL );";
db.execSQL(CREATE_LUGGAGE_TABLE);
}
Replace this
Cursor cursor = db.query(true, TABLE_LUGGAGES, new String[] { your field list }, SearchColumnname + "='" + code + "'", null, null, null, null, null);
with this
Cursor cursor = db.query(true, TABLE_LUGGAGES, null, SearchColumnname + "= ?", new String[] { code }, null, null, null, null);
I am working on an app which contains an Android SQLite database. I add and delete rows in this database. When I delete my row, it is based on the ROWID. But I found out, that despite I delete the row, the ROWID remains. So for example if I have 3 rows in my database and delete the 2nd row, my third row will not change to 2 it remains 3. Then if I add a row, it will be 4.
So my question is, how to do that when I delete a row, and add a new one then the number of the added row would equal to the deleted row. So for example if I delete the first row, than the database will show me next to the other rows 1,2,3... and not 2,3,4...
Sorry, maybe it is a little bit complicated, but I hope it makes sense.
Here is my code:
public HotOrNot open() throws SQLException{
ourHelper = new DbHelper(ourContext);
ourDatabase = ourHelper.getWritableDatabase();
return this;
}
public void close(){
ourHelper.close();
}
public long createEntry(String name, String hotness) {
// TODO Auto-generated method stub
ContentValues cv = new ContentValues();
cv.put(KEY_NAME, name);
cv.put(KEY_HOTNESS, hotness);
return ourDatabase.insert(DATABASE_TABLE, null, cv);
}
public String getData() {
// TODO Auto-generated method stub
String[] columns = new String[]{ KEY_ROWID, KEY_NAME, KEY_HOTNESS};
Cursor c = ourDatabase.query(DATABASE_TABLE, columns, null, null, null, null, null);
String result = "";
int iRow = c.getColumnIndex(KEY_ROWID);
int iName = c.getColumnIndex(KEY_NAME);
int iHotness = c.getColumnIndex(KEY_HOTNESS);
for (c.moveToFirst(); !c.isAfterLast(); c.moveToNext()){
result = result + c.getString(iRow) + " " + c.getString(iName) + " " + c.getString(iHotness) + "\n";
}
return result;
}
public String getName(long l) throws SQLException{
// TODO Auto-generated method stub
String[] columns = new String[]{ KEY_ROWID, KEY_NAME, KEY_HOTNESS};
Cursor c = ourDatabase.query(DATABASE_TABLE, columns, KEY_ROWID + "=" + l, null, null, null, null);
if(c != null){
c.moveToFirst();
String name = c.getString(1);
return name;
}
return null;
}
public String getHotness(long l) throws SQLException{
// TODO Auto-generated method stub
String[] columns = new String[]{ KEY_ROWID, KEY_NAME, KEY_HOTNESS};
Cursor c = ourDatabase.query(DATABASE_TABLE, columns, KEY_ROWID + "=" + l, null, null, null, null);
if(c != null){
c.moveToFirst();
String hotness = c.getString(2);
return hotness;
}
return null;
}
public void updateEntry(long lRow, String mName, String mHotness) throws SQLException{
// TODO Auto-generated method stub
ContentValues cvUpdate = new ContentValues();
cvUpdate.put(KEY_NAME, mName);
cvUpdate.put(KEY_HOTNESS, mHotness);
ourDatabase.update(DATABASE_TABLE, cvUpdate, KEY_ROWID + "=" + lRow, null);
}
public void deleteEntry(long lRow1) throws SQLException{
// TODO Auto-generated method stub
ourDatabase.delete(DATABASE_TABLE, KEY_ROWID + "=" + lRow1, null);
}
}
I know that it can't be done using ROWID, because it is unique for each row.
Could you please write the code how to solve this problem, because I am beginner in Android and also in SQLite.
Thanks in advance
SQLite keeps track of the largest ROWID that a table has ever held
using the special SQLITE_SEQUENCE table.
You can update SQLITE_SEQUENCE table-
UPDATE SQLITE_SEQUENCE SET seq = <n> WHERE name = <table_name>
n is the rowid - 1
Similar question.
i'm trying to read contacts with this code but it only gets Contacts but not contacts data.Please
tell me am i going on a wrong path or what is wrong with this.
Cursor contacts = getContentResolver().query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
while(contacts.moveToNext()) {
int contactIdColumnIndex = contacts.getColumnIndex(ContactsContract.Contacts._ID);
long contactId = contacts.getLong(contactIdColumnIndex);
System.out.println(contactId);
Cursor c = getContentResolver().query(Data.CONTENT_URI,
new String[] {Data._ID, Phone.NUMBER, Phone.TYPE},
Data.CONTACT_ID + "=?" + " AND "
+ Data.MIMETYPE + "='" + Phone.CONTENT_ITEM_TYPE + "'",
new String[] {String.valueOf(contactId)}, null);
//retrieving data
while(c.moveToNext()){
long Id=c.getLong(0);
String number=c.getString(1);
String type=c.getString(2);
String name=c.getString(3);
System.out.println(Id);
System.out.println(number);
System.out.println(type);
System.out.println(name);
}
c.close();
}
contacts.close();
This runs well when i debug this and it only print ContactIds only
1
2
3
4
but no data...(apologize for long question)
try this code
String number = "";
ContentResolver cr = getContentResolver();
Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI,null,null, null, null);
if (cur.getCount() > 0) {
while (cur.moveToNext()) {
String id = cur.getString(cur.getColumnIndex(ContactsContract.Contacts._ID));
String name = cur.getString(cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
if (Integer.parseInt(cur.getString(cur.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) {
Cursor pCur = cr.query(
ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
new String[]{ContactsContract.CommonDataKinds.Phone.NUMBER},
ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = ?",
new String[]{id}, null);
while (pCur.moveToNext()) {
for(int i=0;i<pCur.getColumnCount();i++)
number = pCur.getString(i);
}
pCur.close();
pCur = null;
}
}
}
cur.close();
cur = null;
cr = null;
currently i have a application which will retrieve all the contact
details and will display all the available Contact Names.But now i want to retrieve Home,work numbers.I search for this everywhere but i couldn't. how can
i achieve this? please help me.
Thanks.
I used this code to get Home Numbers.
Cursor c = getContentResolver().query(Data.CONTENT_URI,
new String[] {Data._ID, Phone.NUMBER, Data.MIMETYPE},
Data.RAW_CONTACT_ID + "=?" + " AND "
+ Data.MIMETYPE + "='" + Phone.TYPE_HOME + "'",
new String[] {String.valueOf(contactId)}, null);
But what i need is to get Home,Work,Mobile (with name,email address,etc) numbers using single query.
using this code it always returns me type=0
Cursor c = getContentResolver().query(Data.CONTENT_URI,
null,
Data.CONTACT_ID + "=?",
new String[] {String.valueOf(contactId)}, null);
while(c.moveToNext()){
int type = c.getInt(c.getColumnIndex(Phone.TYPE));
..
}
try this:
int type = mCursor.getInt(mCursor.getColumnIndex(Phone.TYPE));
the "type" value will be 1,2,3 or 4 where
TYPE_HOME = 1;
TYPE_MOBILE = 2;
TYPE_WORK = 3;
TYPE_OTHER = 7;
Ok finally i completed the task.here my code
String mobile = "";
String home="";
String work="";
String fax="";
String other="";
String disName="";
String pName="";
String fName="";
String lName="";
String sName="";
String mName="";
String postBox="";
String streat="";
String country="";
String emailAdd="";
ContentResolver cr = getContentResolver();
Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI,null,null, null, null);
while (cur.moveToNext()) {
String id = cur.getString(cur.getColumnIndex(ContactsContract.Contacts._ID));
disName = cur.getString(cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
///*
Cursor phones = cr.query(Phone.CONTENT_URI, null,
Phone.CONTACT_ID + " = " + id, null, null);
while (phones.moveToNext()) {
String number = phones.getString(phones.getColumnIndex(Phone.NUMBER));
int type = phones.getInt(phones.getColumnIndex(Phone.TYPE));
System.out.println("Numeber= "+number);
switch (type) {
case Phone.TYPE_HOME:
home=number;
break;
case Phone.TYPE_MOBILE:
mobile=number;
break;
case Phone.TYPE_WORK:
work=number;
break;
case Phone.TYPE_OTHER:
other=number;
break;
case Phone.TYPE_FAX_HOME:
fax=number;
break;
case Phone.TYPE_FAX_WORK:
fax=number;
break;
}
}
phones.close();
Cursor name = cr.query(Data.CONTENT_URI, null,
Data.CONTACT_ID + " ="+id +" AND "+Data.MIMETYPE+"='"+StructuredName.CONTENT_ITEM_TYPE+"'", null, null);
while(name.moveToNext()){
sName=name.getString(name.getColumnIndex(StructuredName.PREFIX));
fName=name.getString(name.getColumnIndex(StructuredName.GIVEN_NAME));
mName=name.getString(name.getColumnIndex(StructuredName.MIDDLE_NAME));
lName=name.getString(name.getColumnIndex(StructuredName.FAMILY_NAME));
sName=name.getString(name.getColumnIndex(StructuredName.SUFFIX));
System.out.println(mName);
}
Cursor address = cr.query(Data.CONTENT_URI, null,
Data.CONTACT_ID + " ="+id +" AND "+Data.MIMETYPE+"='"+StructuredPostal.CONTENT_ITEM_TYPE+"'", null, null);
while(address.moveToNext()){
postBox=address.getString(address.getColumnIndex(StructuredPostal.POBOX));
streat=address.getString(address.getColumnIndex(StructuredPostal.STREET));
mName=address.getString(address.getColumnIndex(StructuredPostal.COUNTRY));
System.out.println(postBox);
}
Cursor email = cr.query(ContactsContract.CommonDataKinds.Email.CONTENT_URI, null,
Data.CONTACT_ID + " ="+id , null, null);
while(email.moveToNext()){
emailAdd=email.getString(email.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA));
}
In where clause of your query use Phone.TYPE_HOME.. this will give you the desired nor..