SQL query not creating my columns? - android

I am trying to create a fitness app where the database saves a username and password.
then enters their details that saves to a second table. This is my dbHelper.
The error im getting is that my "Username Column does not exist"
But when i go and look at my tables using db browser for sqlite
it shows my tables created and data in my tables
UPDATE : I created 1 table to store all my data and now its not picking up still im getting "not set" from my display method
updated table
// Register table
public static final String COL_1 = "ID";
public static final String COL_2 = "Username";
public static final String COL_3 = "Password";
public static final String COL_4 = "Weight";
public static final String COL_5 = "Height";
public static final String COL_6 = "TargetWeight";
public static final String COL_7 = "TargetSteps";
display method
public String DisplayData(String username,String column)
{
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.rawQuery("SELECT * FROM "+ TABLE_NAME +" WHERE Username =?",new String[]{username});
if(cursor.moveToFirst()){
return cursor.getString(cursor.getColumnIndexOrThrow(column));
}else{
return "Not set";
}
}
Usage
public void setData() {
db = new dbHelper(this);
try {
userWeight.setText(db.DisplayData(Username, dbHelper.COL_4));
userHeight.setText(db.DisplayData(Username, dbHelper.COL_5));
userTargetWeight.setText(db.DisplayData(Username, dbHelper.COL_6));
userTargetSteps.setText(db.DisplayData(Username, dbHelper.COL_7));
} catch (Exception e) {
Toast.makeText(this, e.getMessage(), Toast.LENGTH_SHORT).show();
}
}

TABLE_NAME1 has no Username column. update your method as follow
public String DisplayData(String username, String column) {
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.rawQuery("SELECT * FROM "+ TABLE_NAME +" WHERE " + column + " = ? ", new String[]{username});
if (cursor != null && cursor.moveToFirst()){
return cursor.getString(cursor.getColumnIndexOrThrow(column));
} else {
return "Not set";
}
}
and use it like this
.DisplayData("admin", "Username");

Your issue, assuming that value of the 2nd argument of the Displaydata method is Username is that you are querying TABLE_NAME1 (profile_data) table, which doesn't have a column named Username.
Instead I believe you want to be querying the TABLE_NAME (register_table table) table so change :-
Cursor cursor = db.rawQuery("SELECT * FROM "+ TABLE_NAME1 +" WHERE Username = 'admin' ",null);
to :-
Cursor cursor = db.rawQuery("SELECT * FROM "+ TABLE_NAME +" WHERE Username = 'admin' ",null);
Additional re comment :-
Im not longer getting an error but its still not displaying the
correcting infomation . Im getting "Not set " from my displayData
method. from my if ELSE
Assuming that you have added data and getting the above then it is likely that Username does not equate to a row in the table. Try using the following version of DisplayData to debug :-
// Note changed to use recommended convenience query method
// Note closes cursor thus uses intermediate variable (rv) to allow close
public String DisplayData(String username,String column)
{
String rv = "Not set";
SQLiteDatabase db = this.getReadableDatabase();
//Cursor cursor = db.rawQuery("SELECT * FROM "+ TABLE_NAME +" WHERE Username =?",new String[]{username}); //<<<<< replaced
String whereclause = COL_2 + "=?";
String[] whereargs = new String[]{username};
Cursor cursor = db.query(TABLE_NAME,null,whereclause,whereargs,null,null,null);
//<<<<<<<<<< FOLLOWING CODE ADDED TO LOG DEBUG INFO >>>>>>>>>>
Log.d("DISPLAYDATAINFO","Display was called with Username as:- " +
username +
" for Column:- " +
column +
". The Cursor contains " +
String.valueOf(cursor.getCount()) +
" ."
);
//<<<<<<<<<< END OF ADDED DEBUG CODE >>>>>>>>>>
if(cursor.moveToFirst()){
rv = cursor.getString(cursor.getColumnIndexOrThrow(column));
}
cursor.close(); //<<<< SHOULD ALWAYS CLOSE CURSOR WHEN DONE WITH IT
return rv;
}
This should produce output in the log along the lines of :-
05-18 23:08:25.429 2926-2926/fitness.fitness D/DISPLAYDATAINFO: Display was called with Username as:- Fred for Column:- Weight. The Cursor contains 5 .
Display was called with Username as:- Fred for Column:- Height. The Cursor contains 5 .
Display was called with Username as:- Fred for Column:- TargetWeight. The Cursor contains 5 .
Display was called with Username as:- Fred for Column:- TargetSteps. The Cursor contains 5 .
Note 5 because when testing new data is inserted each run so the above indicates the 5th run.
Or in the case of nothing being found (your current issue) something like :-
05-18 23:11:40.342 2926-2926/fitness.fitness D/DISPLAYDATAINFO: Display was called with Username as:- Tom for Column:- Weight. The Cursor contains 0 .
Display was called with Username as:- Tom for Column:- Height. The Cursor contains 0 .
Display was called with Username as:- Tom for Column:- TargetWeight. The Cursor contains 0 .
Display was called with Username as:- Tom for Column:- TargetSteps. The Cursor contains 0 .
i.e. Cursor contains 0 = no rows exist for the given username (Tom in this case).
Check if the Username is as expected (note case of letters must match, in the above a row for tom exists but not for Tom hence 0 count for the cursor).
Column retrieval appears to be correct, However, still check that the columns in the output are as expected (can't see that they would not be).

Related

Sql Query to retrieve a particular data from particular column and row in android?

I want to fetch phone number linked to particular email in the database. I am not able to find the query for it or how
public String getContactNumber(String email){
SQLiteDatabase db = this.getReadableDatabase();
String query = "SELECT " + COLUMN_USER_MOBILE_NUMBER + " FROM " + TABLE_USER + " WHERE " + email + " = " + COLUMN_USER_EMAIL;
Cursor cursor = db.rawQuery(query,null);
//What to put here to extract the data.
String contact = cursor.getString(get);
cursor.close();
return contact;
}
to extract the data. Completely a beginner
Try this ..
public List<String> getMyItemsD(String emailData) {
List<String> stringList = new ArrayList<>();
SQLiteDatabase db = this.getReadableDatabase();
String selectQuery = "SELECT COLUMN_USER_MOBILE_NUMBER FROM " + USER_TABLE_NAME + " WHERE email= " + emailData;
Cursor c = db.rawQuery(selectQuery, null);
if (c != null) {
c.moveToFirst();
while (c.isAfterLast() == false) {
String name = (c.getString(c.getColumnIndex("Item_Name")));
stringList.add(name);
c.moveToNext();
}
}
return stringList;
}
public String getContactNumber(String email){
String contact = "";
SQLiteDatabase db = this.getReadableDatabase();
String query = "SELECT " + COLUMN_USER_MOBILE_NUMBER + " FROM " + TABLE_USER + " WHERE " + email + " = " + COLUMN_USER_EMAIL;
Cursor cursor = db.rawQuery(query,null);
if(cursor.getCount()>0) {
cursor.moveToNext();
contact = cursor.getString(cursor.getColumnIndex(COLUMN_USER_MOBILE_NUMBER));
}
//What to put here to extract the data.
cursor.close();
return contact;
}
From this method you get phone number value of that email which you pass any other method easily.
I'd suggest the following :-
public String getContactNumber(String email){
String contact = "NO CONTACT FOUND"; //<<<<<<<<<< Default in case no row is found.
SQLiteDatabase db = this.getWritableDatabase(); //<<<<<<<<<< Generally getReadable gets a writable database
String[] columns_to_get = new String[]{COLUMN_USER_MOBILE_NUMBER};
String whereclause = COLUMN_USER_EMAIL + "=?";
String[] whereargs = new String[]{email};
Cursor cursor = db.query(TABLE_USER,columns_to_get,whereclause,whereargs,null,null,null);
//What to put here to extract the data.
if (cursor.moveToFirst()) {
contact = csr.getString(csr.getColumnIndex(COLUMN_USER_MOBILE_NUMBER));
}
cursor.close();
return contact;
}
The above does assumes that there will only be 1 row per email (which is most likely).
Explanations
A default value is set so that you can easily tell if an invalid/non-existent email is passed (you'd check the return value if need be (might be easier to simply have "" and check the length as a check)).
getReadableDatabase has been replaced with getWritableDatabase as unless there are issues with the database a writable database will be returned, as per :-
Create and/or open a database. This will be the same object returned
by getWritableDatabase() unless some problem, such as a full disk,
requires the database to be opened read-only. In that case, a
read-only database object will be returned. If the problem is fixed, a
future call to getWritableDatabase() may succeed, in which case the
read-only database object will be closed and the read/write object
will be returned in the future.
getReadableDatabase
Note no real problem either way;
The recommended query method has been used instead of the rawQuery method. This has distinct advantages, it builds the underlying SQL and also offers protection against SQL injection (just in case the email passed is input by a user).
this version of the method takes 7 parameters :-
The table name as a string
The columns to be extracted as an array of Strings (aka String array). null can be all columns.
The where clause less the WHERE keyword with ?'s to represent arguments (see next). null if no WHERE clause.
The arguments to be applied (replace ?'s 1 for 1) as a String array. null if none or no WHERE clause.
The GROUP BY clause, less the GROUP BY keywords. null if no GROUP BY clause.
The HAVING clause, less the HAVING keyword. null if no HAVING clause.
The ORDER BY clause, less the ORDER BY keywords. null if no ORDER BY clause.
SQLiteDatabase - query
- Note there are 4 query methods (see link for the subtle difference, I believe this is the most commonly used)
The data extraction is the new code. When a Cursor is returned it is at a position BEFORE THE FIRST ROW, so you need to move to a valid row. So the moveToFirst* method is suitable (note that if a move cannot be made by a move method that it will return false, hence how you can say if (cursor.moveToFirst())). The data is then extracted from the appropriate column use the **getString method, which takes an int as an argumnet for the column offset (0 in this case). However, using hard coded values can lead to issues so the getColumnIndex method is used to get the offset according to the column name (-1 is returned if the named column is not in the Cursor).

SQLite Insert Query Multiple Database

I wrote the following code to insert some records into table from the table of another database.
But I'm unable to, even after executing a sql statement it shows that there are no records in the table.
public int copy_to_all_source_table(String dbpath,String backpath)
{
SQLiteDatabase db1 = this.getWritableDatabase();
//Opening App database(i.e. dbpath) and attaching it as "OLD"
db1.openDatabase(dbpath, null, SQLiteDatabase.OPEN_READWRITE);
String attach_old="ATTACH '"+ dbpath +"' AS OLD";
db1.execSQL(attach_old);
//Opening New File which is Student.db(i.e. dbpath) and attaching it as "NEW"
db1.openDatabase(backpath, null, SQLiteDatabase.OPEN_READWRITE);
String attach_new="ATTACH '"+ backpath +"' AS NEW";
db1.execSQL(attach_new);
// Getting count of records in table of "NEW"
String new_query =" SELECT * FROM 'NEW'.'"+ TABLE_CONTACTS +"'";
Cursor new_data = db1.rawQuery(new_query, null);
Integer new_count= new_data.getCount();
//INSERTING ALL RECORDS FROM TABLE OF NEW TO TABLE OF OLD
String insert_query ="INSERT INTO 'OLD'.'"+ TABLE_CONTACTS +"' SELECT * FROM 'NEW'.'"+ TABLE_CONTACTS +"'";
Cursor success_insert = db1.rawQuery(insert_query, null);
// Getting count of records in table of "NEW"
String after_insert_old_query =" SELECT * FROM 'OLD'.'"+ TABLE_CONTACTS +"'";
Cursor old_data = db1.rawQuery(after_insert_old_query, null);
Integer old_count= old_data.getCount();
}
RESULT:
new_count = 11
old_count = 0
So, no record has been inserted.
You are using rawQuery() to execute an INSERT command. Which will never work.
Use execSQL(), instead
Moreover, the last comment is misleading, because it says you want the count from the NEW table, but you are counting from the OLD one.
And, please, get rid of the string delimiter characters (').
I.e.:
this
String new_query =" SELECT * FROM 'NEW'.'"+ TABLE_CONTACTS +"'";
should be
String new_query = "SELECT * FROM NEW." + TABLE_CONTACTS;

How to select the remaining rows after selected some in Android SQLite?

I have a task in which i have to display the rows selected using a query in one tab and the remaining rows in another tab. I have displayed the selected rows using SimpleCursorAdapter. When i tried to display the remaining rows in next tab it throws error. I have tried NOT IN but it also doesn't work. I have tried NOT EXISTS also it shows all rows. Anyone who can answer please help. I have posted my code here. Thanks in advance.
This is the activity of first tab which displays selected rows
public class OnlineDevices extends Activity {
ListView listOnline;
DatabaseHelper databaseHelper;
String count;
int conut;
TextView tvOnlineCount;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_online_devices);
listOnline=(ListView)findViewById(R.id.listView3);
tvOnlineCount = (TextView) findViewById(R.id.textView59);
databaseHelper=new DatabaseHelper(this);
SQLiteDatabase db=databaseHelper.getReadableDatabase();
String date= DateFormat.getDateInstance().format(new Date());
String statusQuery="select rowid _id, deviceContact from statusTable where sentTime='"+date+"'";
Cursor cursor1=db.rawQuery(statusQuery,null);
if (cursor1.getCount()>0){
while (cursor1.moveToNext()){
String deviceNo=cursor1.getString(cursor1.getColumnIndex("deviceContact"));
String device=deviceNo.substring(2, 12);
String query="select rowid _id, userName, uSerialNo from bikeTable where uContactNo='"+device+"' AND userName IS NOT NULL";
Cursor cursor2=db.rawQuery(query, null);
SimpleCursorAdapter adapter=new SimpleCursorAdapter(this,R.layout.status_item,cursor2,new String[]{"userName","uSerialNo"},new int[]{R.id.textView51,R.id.textView52});
listOnline.setAdapter(adapter);
}
}
try {
conut = listOnline.getAdapter().getCount();
count = String.valueOf(conut);
tvOnlineCount.setText(count);
}catch (Exception e){
e.printStackTrace();
int i=0;
Toast.makeText(OnlineDevices.this,"No device is active",Toast.LENGTH_SHORT).show();
tvOnlineCount.setText(String.valueOf(i));
}
}
}
Activity for second tab which display the remaining rows are
public class OfflineDevices extends Activity {
ListView listOffline;
DatabaseHelper databaseHelper;
String deviceContact;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_offline_devices);
listOffline=(ListView)findViewById(R.id.listView4);
databaseHelper=new DatabaseHelper(this);
SQLiteDatabase db=databaseHelper.getReadableDatabase();
String date= DateFormat.getDateInstance().format(new Date());
String query="select rowid _id, deviceContact from statusTable where sentTime='"+date+"'";
Cursor cursor1=db.rawQuery(query,null);
if (cursor1.getCount()>0){
while (cursor1.moveToNext()) {
deviceContact = cursor1.getString(cursor1.getColumnIndex("deviceContact"));
}
String device=deviceContact.substring(2, 12);
String query2="select rowid _id, userName, uSerialNo from bikeTable where userName IS NOT NULL AND uContactNo='"+device+"'";
Cursor cursor3=db.rawQuery(query2,null);
String query1="select rowid _id,*from bikeTable where userName IS NOT NULL NOT IN('"+query2+"')";
Cursor cursor2=db.rawQuery(query1,null);
SimpleCursorAdapter adapter=new SimpleCursorAdapter(this,R.layout.status_item,cursor2,new String[]{"userName","uSerialNo"},new int[]{R.id.textView51,R.id.textView52});
listOffline.setAdapter(adapter);
}
}
}
You have syntax errors in your query. In theory the following should work:
String query2 = "SELECT userName FROM bikeTable WHERE userName IS NOT NULL "
+ "AND uContactNo = '" + device + "'";
String query1 = "SELECT * FROM bikeTable WHERE userName IS NOT NULL "
+ "AND userName NOT IN(" + query2 + ")";
Here are the differences:
Your original code has single quotes around query2 inside the parentheses. This makes SQLite treat it as a string literal instead of an inner query.
Since query2 is being used in a NOT IN expression, it needs to
have a single result column, and it seems like userName is the one
you are interested in.
I advise spending some time going through the SQLite language pages. I'm fairly certain you can actually get the results you want using a single query that has a JOIN instead of making one query, checking the result, then making another query.
As an aside, it's considered best practice in Android to load data from a database on a background thread. Typically this is done with the Loader framework. You should also be closing cursors when you are finished with them (not the ones you give to the adapters, but the ones you use just to check for an online device).

Android sqlite - cursor count not 0 on empty table

I have the following code in a bigger project:
final class DBlifetimeStatisticsHandler{ //implements DBvalueHandler<Cyclist, Double>{
private final String TAG = getClass().getName();
private static final boolean debug = true;
private final DBminMaxAvgHandler dbMinMaxAvgHandler = new DBminMaxAvgHandler();
// table name
private static final String TABLE_LIFETIME_STATISTICS = "lifetime_statistics";
// column names
private static final String KEY_LIFETIME_STATISTICS_ID = "lifetime_statistics_id";
private static final String KEY_MIN_MAX_AVG = "min_max_avg";
// table create statement
private static final String CREATE_TABLE = "CREATE TABLE "
+ TABLE_LIFETIME_STATISTICS + "("
+ KEY_LIFETIME_STATISTICS_ID + " LONG PRIMARY KEY NOT NULL,"
+ KEY_MIN_MAX_AVG + " LONG"
+ ")";
public void onCreateTable(SQLiteDatabase db) {
db.execSQL(CREATE_TABLE);
}
public void onUpgrade(SQLiteDatabase db) {
db.execSQL("DROP TABLE IF EXISTS " + TABLE_LIFETIME_STATISTICS);
onCreateTable(db);
}
public long addValue(SQLiteDatabase db, Statistics Statistics ) {
ContentValues values = new ContentValues();
long ID = getLatestID(db)+1;
values.put(KEY_STATISTICS_ID, ID);
... //not important to the question
}
private long getLatestID(SQLiteDatabase db){
String selectQuery = "SELECT MAX(" + KEY_STATISTICS_ID +") FROM " + TABLE_STATISTICS;
Cursor c = db.rawQuery(selectQuery, null);
c.moveToFirst();
int id = 0;
Log.e("count", String.valueOf(c.getCount()));
if (c.moveToFirst()){
...
}
return id;
}
}
After I updated the table it is created again. So when I try to add a new value I had problems cause it always jumped into the if clause because c.moveToFirst() always returned true.
So I tried to tried to check if c.getCount() would return true but sadly it does always return 1. So the question is: Why would it return 1 on an empty table? (I do use Questoid SQLite Browser and the table is really empty)
You use aggregate function MAX, so read documentation:
There are two types of simple SELECT statement - aggregate and non-aggregate queries. A simple SELECT statement is an aggregate query if it contains either a GROUP BY clause or one or more aggregate functions in the result-set.
An aggregate query without a GROUP BY clause always returns exactly one row of data, even if there are zero rows of input data.
It might be some kind of a buggy behavior when using MAX. Check this link too Android database (SQLite) returns a non-empty cursor from an empty table
this is my solution
public Boolean isNotEmpty(){
SQLiteDatabase db = this.getWritableDatabase();
Cursor cursor = db.rawQuery("SELECT * FROM " + TABLE_STATISTICS, null);
while (cursor.moveToNext() ) {
return true;
}
return false;
}
You are getting a result with one row in your Cursor because that is what you requested.
The result is a single column called MAX with a value that will be the max id of all the rows in your table. In your case of an empty table, this value is null.
I am using group by to resolve this. Please check my example :
SELECT COUNT(*) FROM " + TABLE_NAME + " WHERE isSynced=0 group by isSynced
I resolve this probme this way:
SELECT COUNT(*) AS numero, MAX(tagua_lagps) as tmp_max_lagps, MAX(tagua_logps) as tmp_max_logps, MIN(tagua_lagps) as tmp_min_lagps, MIN(tagua_logps) as tmp_min_logps FROM TAB_AGUA
On empty table, c.getCount(); gives 1 but values are NULL. But numero (c.getString(c.getColumnIndex("numero")) has a value of 0.
So rather than checking c.getCount() you must check the result of count(*).

Method to excute query and return results

App won't run - trying to execute query to print certain value
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,null);
if (cursor != null) {
cursor.moveToFirst();
}
return cursor;
}
Calling method from main
Cursor wow = db.trying("gold");
text = (TextView) findViewById(R.id.textView13);
text.setText((CharSequence) (wow));
At first. Since you are directly adding trying variables into statement, variable must be wrapped to single quotes or it's interpeted as column.
"SELECT quantity FROM " + TABLE_CONTACTS + " WHERE name= '" + vg + "'";
And second "big" problem, look here:
text.setText((CharSequence) (wow));
Here you are trying to cast Cursor to CharSequence but it's not possible. If you want to retrieve data from Cursor you have to use one from the getters methods of Cursor class in your case getString() method:
String quantity = wow.getString(0); // it returns your quantity from Cursor
text.setText(quantity);
Now it should works.
Recommendation:
I suggest you to an usage of parametrized statements which actually use placeholders in your queries. They provide much more safer way for adding and retrieving data to / from database.
Let's rewrite your code:
String q = "SELECT quantity FROM " + TABLE_CONTACTS + " WHERE name = ?";
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.rawQuery(q, new String[] {vg});
It works simply. Placeholder ? will be replaced with your string value.

Categories

Resources