First sorry for my poor english :)
I click button but app close automaticly
I want if user exist database , read editttext "exist"
Database activity code:
public Boolean varmi(String KULLANICI) {
// TODO Auto-generated method stub
Cursor c = DB_Database.query(DATABASE_TABLOSU, kolonlar, K_ADI + "=" + KULLANICI, null, null, null, null);
if(c.moveToFirst()){
return true;
}
else{
return null;
}
}
Main activity code:
buttonGiris.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
String KULLANICI=editKullanici.getText().toString();
db.dbyiac();
if(db.varmi(KULLANICI) != null){
textBilgiler.setText("User exist");
}
else{textBilgiler.setText("User don't exist");}
db.dbyikapat();
}
String literals in SQL must be in single quotes. Better yet, use ? literal placeholder and argument binding:
Cursor c = DB_Database.query(DATABASE_TABLOSU, kolonlar, K_ADI + "=?", new String[] { KULLANICI }, null, null, null);
cursor.getColumnIndex(String columnName) returns -1 if, the column doesn't exist :) and call this method textBilgiler.setText("User don't exist"); :) kolay gele.
Related
what is wrong with this code
when i run it the program don,t response
public Cursor checkauth(String username,String password)
{
Ecommerce=getReadableDatabase();
// Cursor curser =Ecommerce.rawQuery("select * from customer where username = ? and password =?", new String []{username,password});
//String [] details={"id","custname","gender"};
Cursor c = Ecommerce.rawQuery("select id from customer where username = ? AND password = ?" , new String[] {username,password });
//Cursor cursor = Ecommerce.query(true, "customer",details,"username = ? AND password = ?", new String[] { username, password },null, null, null, null, null);
while(c != null)
{
c.moveToFirst();
}
return c;
}
in activity
public void onClick(View arg0) {
// TODO Auto-generated method stub
user_name=username.getText().toString();
passwords=password.getText().toString();
Cursor c=data.checkauth(user_name, passwords);
while(!c.isAfterLast())
{
username.setText(String.valueOf(c.getInt(0)));
c.moveToNext();
}
}
![enter image description here][1]
You're blocking your UI thread.
This loop never completes:
while(c != null)
{
c.moveToFirst();
}
The while condition is always true. The loop doesn't seem to be doing anything useful, you can probably just leave a plain c.moveToFirst() there.
Try with this..
String selectQuery = "Write select query" ;
SQLiteDatabase db = this.getWritableDatabase();
Cursor cursor = db.rawQuery(selectQuery, null);
boolean result = false;
if (cursor.moveToFirst())
{
do
{
result = true;
} while (cursor.moveToNext());
}
else
{
result = false;
}
Even though this question is answered already, I'd like to suggest a simple improvement. What you're doing is kind of useless. Your goal is to know whether or not a user exists and what his or her id is.
public int checkAuth(String username, String password) {
String sql = "select id from customer where username = ? AND password = ?";
Ecommerce = getReadableDatabase(); //field names never start with uppercase
Cursor c;
try {
c = Ecommerce.rawQuery(sql, new String[] { username, password });
if (c.moveToFirst()) //if the cursor has any records, this returns true, else false
return c.getInt(0); //so if a user exists with given params, we can return an id
else
return -1; //if not, we return -1, which means: user/pass-combination not found
}
finally { //but after the whole method, we need to do some things:
if (c != null && !c.isClosed())
c.close(); //close the cursor
closeDatabase(); //and close the database
}
}
This way you keep your UI code separated from your database code. A cursor shouldn't appear in UI-code. Which above modifications, the UI code looks a lot cleaner
int i = data.checkAuth(username, password);
if (i == -1) {
//alert of some kind
}
else
username.setText(String.valueOf(i));
I have a problem in my list view. when I pass My SQL query to ListView then list view show this in each row
*com.MyPackageName.MyAppName.MyClassName#Some Number (like 2c62a7d0) *
I dont know why
this is my Function that return everything
public List<AddedZekrView> getAllRecordList(){
ArrayList<AddedZekrView> addedZekrFields = new ArrayList<AddedZekrView>();
//Cursor cursor= database.query(ZekrdbOpenHelper.TABLE_ZEKR, allColumns, null, null, null, null, null);
Cursor cursor= database.query(ZekrdbOpenHelper.TABLE_ZEKR, allColumns, null, null, null, null, null);
if (cursor.getCount()>0) {
cursor.moveToFirst();
while (cursor.moveToNext()) {
Log.i("In while", cursor.getPosition()+"");
AddedZekrView addedZekrFieldNew=new AddedZekrView();
// you have to declare all fields such as **COLUMN_ID** in your addedZekrView activity
addedZekrFieldNew.ID=cursor.getLong(cursor.getColumnIndex(ZekrdbOpenHelper.COLUMN_ID));
addedZekrFieldNew.TITLE=cursor.getString(cursor.getColumnIndex(ZekrdbOpenHelper.COLUMN_TITLE));
addedZekrFieldNew.DESC=cursor.getString(cursor.getColumnIndex(ZekrdbOpenHelper.COLUMN_DESC));
addedZekrFieldNew.MAX_COUNT=cursor.getString(cursor.getColumnIndex(ZekrdbOpenHelper.COLUMN_MAX_COUNT));
addedZekrFieldNew.CREATE_DATE=cursor.getString(cursor.getColumnIndex(ZekrdbOpenHelper.COLUMN_CREATE_DATE));
addedZekrFieldNew.LAST_COUNT_DATE=cursor.getString(cursor.getColumnIndex(ZekrdbOpenHelper.COLUMN_LAST_COUNT_DATE));
addedZekrFieldNew.MODIFY_DATE=cursor.getString(cursor.getColumnIndex(ZekrdbOpenHelper.COLUMN_MODIFY_DATE));
addedZekrFieldNew.EXPIRATION_DATE=cursor.getString(cursor.getColumnIndex(ZekrdbOpenHelper.COLUMN_EXPIRATION_DATE));
addedZekrFieldNew.START_AZ=cursor.getString(cursor.getColumnIndex(ZekrdbOpenHelper.COLUMN_START_AZ));
Log.i("return cursor", addedZekrFieldNew.TITLE+ " " +addedZekrFieldNew.DESC);
addedZekrFields.add(addedZekrFieldNew);
}
}
//return List Object
return addedZekrFields;
}
and this is my java file:
public class AddedZekrView extends ListActivity {
public Long ID;
public String TITLE;
public String DESC;
public String MAX_COUNT;
public String CREATE_DATE;
public String CURENT_COUNT;
public String LAST_COUNT_DATE;
public String MODIFY_DATE;
public String EXPIRATION_DATE;
public String START_AZ;
ZekrDataSource datasource;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
//PreferenceActivity is a subclass of ListActivity (source), so you can either remove the setContentView() method call or add a subview with id android.R.list in added_zekr_view.xml
//I removed setContentView(R.layout.added_zekr_view);
//setContentView(R.layout.added_zekr_view);
datasource=new ZekrDataSource(this);
datasource.open();
List<AddedZekrView> addedZekrFields2 = datasource.getAllRecordList();
// if this is first time that we run App so maybe we dont have any data on it so we need to check that
//And call our list View again
if (addedZekrFields2.size()==0) {
Toast toast = Toast.makeText(this, "Nothing Founded.",Toast.LENGTH_LONG);
toast.show();
//datasource.insertRecord(null, null, null, null, null, null);
addedZekrFields2 = datasource.getAllRecordList();
}
ArrayAdapter<AddedZekrView> adapter = new ArrayAdapter<AddedZekrView>(this, android.R.layout.simple_expandable_list_item_1, addedZekrFields2 );
setListAdapter(adapter);
}
}
Can someone please help me to figure it out.
Thank you so much.
I Overwrite toString() and it solve my problem :)
I am developing a small app in which am inserting two values into the database(Phone Number and Address).
Am done with the insertion part. Now I would like to search for a corresponding address to a particular phone number. I have followed a tutorial from Androidhive and it has worked out fairly well. My problem is when I insert a particular phone number(which is already stored in the database) and hit search button, My TextView is not displaying the corresponding address. It just displays "name" in that text. Am preety sure i have not hardcoded the text "name" anywhere. And am not getting any particular error as well.
Here is the code for my buttonSearch:
buttonSearch.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
long phone_number=Long.parseLong(searchEditText.getText().toString());
String text=db.getAddress(phone_number);
fetchedAddressTextView.setText(text);
}
And this is the getAddress method defined in my Helper class:
public String getAddress(long phone_number) {
// TODO Auto-generated method stub
SQLiteDatabase db = this.getReadableDatabase();
try {
Cursor c=db.query(TABLE_CONTACTS, new String[]{
KEY_ADDRESS
},
KEY_PH_NO + "=" + phone_number,
null,
null,
null,
null);
if (c != null) {
c.moveToFirst();
}
}
catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return KEY_ADDRESS;
}
KEY_ADDRESS should return the address corresponding to the number I searched for in form of a string.
So any help with this problem is appreciated. Or any other pointers regarding simple fetching of stored values in the database(other then vogella or androidhive)are welcome.
Thanks in Advance.
Your code is returning column name instead of value. Please check below code after c.moveToFirst(); statement
public String getAddress(long phone_number) {
// TODO Auto-generated method stub
SQLiteDatabase db = this.getReadableDatabase();
try {
Cursor c=db.query(TABLE_CONTACTS, new String[]{
KEY_ADDRESS
},
KEY_PH_NO + "=" + phone_number,
null,
null,
null,
null);
if (c != null) {
c.moveToFirst();
return c.getString (c.getColumnIndex (KEY_ADDRESS));
}
}
catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return "";
}
I have database called wisherDB, and I have a table called tbltasks inside the database.
The Table simply has colmmn names of id, title, name, date, time, and type. I want to get id, name, and the time to relate to the current date, and I can access current date by a Calendar class, so that is not the problem.
The selection code is in a seperate class called DataAccess and code is mentioned below.
What I want to do is get the details from the query and display it on the tableview. I tried this without selection of date[where clause][that means select * from ...] that is working for this.
But with the selection, it's not showing the data.
DatabaseAccess class Select query:
public Cursor getTasktoDate(String Date) throws SQLException
{
Cursor mCursor=db.query(true, DATABASE_TABLE, new String[] {KEY_ROWID,KEY_TASKNAME,KEY_TASKTYPE}, KEY_TASKDATE+"="+ Date, null, null, null, null, null);
if(mCursor!=null)
{
mCursor.moveToFirst();
}
return mCursor;
}
And this is the code of the activity:
Cursor c=dba.getTasktoDate("2011/10/12");
if (c.moveToFirst())
{
do {
DisplayContact(c, tltodaytask);
} while (c.moveToNext());
}
dba.Close();
}
private void DisplayContact(Cursor c, TableLayout tltodaytask) {
// TODO Auto-generated method stub
String id=c.getString(0);
String tName=c.getString(1);
String tType=c.getString(2);
insertRow(tltodaytask,id,tName,tType);
}
private void insertRow(TableLayout tltodaytask, String id, String tName,
String tType) {
// TODO Auto-generated method stub
final TableRow newrow = new TableRow(this);
addTexttoRowswithValues(newrow, id);
addTexttoRowswithValues(newrow, tName);
addTexttoRowswithValues(newrow, tType);
tltodaytask.addView(newrow);
}
private void addTexttoRowswithValues(TableRow newrow, String text) {
// TODO Auto-generated method stub
TextView textview = new TextView(this);
textview.setWidth(115);
textview.setText(text);
newrow.addView(textview);
}
This method not working.
You have problem in
Cursor mCursor=db.query(true, DATABASE_TABLE,
new String[] {KEY_ROWID,KEY_TASKNAME,KEY_TASKTYPE},
KEY_TASKDATE+"="+ Date, null, null, null, null, null);
You should use
Cursor mCursor=db.query(true, DATABASE_TABLE,
new String[] {KEY_ROWID,KEY_TASKNAME,KEY_TASKTYPE},
KEY_TASKDATE+"= ?", new String[]{Date}, null, null, null, null);
hi frnds i want to fetch all the given name in contact list but each time when i cal l this method it returns first value of contact list in each time pls take a look below in my code below. how to i read next value when getName(long _id) method call each time....
public String [] getName(long _id)
{
String Given = null ;
String family = null ;
try {
String whereName = ContactsContract.Data.MIMETYPE + " = ?";
String[] whereNameParams = new String[] { ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE };
Cursor cursor = getContentResolver().query(ContactsContract.Data.CONTENT_URI, null, whereName, whereNameParams, ContactsContract.CommonDataKinds.StructuredName.GIVEN_NAME);
if(cursor != null) {
while (cursor.moveToNext()) {
// This would allow you get several email addresses
// if the email addresses were stored in an array
Given = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.StructuredName.GIVEN_NAME));
// family = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.StructuredName.FAMILY_NAME));
// TODO Auto-generated method stub
if(Given != null)
break;
}
}
} catch (Exception e) {
Log.i("test", "Exception " + e.toString());
} finally {
if(cursor != null) {
cursor.deactivate();
cursor.close();
}
}
//return emailid;
//return emailType;
Log.i("RETURN given name.....", Given);
return new String[] { Given};
}
The code as u posted it won't work in any case. You cannot declare a variable in a try{} block and use it in the finally{} block. But let's talk about your question first.
Looking at your code I see that u never make any use of the _id parameter thus always getting the same name. You need to check whether the id of the current row is the same as the id you get as parameter in getName(long _id). Something like this should do:
while (cursor.moveToNext()) {
if (cursor.getLong(cursor.getColumnIndexOrThrow(ContactsContract.CommonDataKinds.StructuredName._ID)) == _id) {
Given = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.StructuredName.GIVEN_NAME));
break;
}
}
Hopefully this helps!
And remember to put at least the declaration for cursor before the try{} block :)