₦airaland Forum

Welcome, Guest: RegisterLoginWith GoogleTrendingRecentNew

Stats: 3,324,986 members, 8,419,835 topics. Date: Thursday, 04 June 2026 at 01:11 AM

Toggle theme

Help With Sqlite Select - Programming - Nairaland

Nairaland ForumScience/TechnologyProgrammingHelp With Sqlite Select (1522 Views)

1 Reply (Go Down)

Help With Sqlite Select by Jinf(op): 6:55am On Apr 23, 2015
Please can anyone help me on how to check if a column in sqlite table is empty before inserting data into that column.
Re: Help With Sqlite Select by Jinf(op): 8:51am On Apr 23, 2015
I've tried this but it gives this error: java.lang.NullPointerException

// Database
SQLiteDatabase db = openOrCreateDatabase("NeamLS.db", MODE_PRIVATE, null);

try {
// Create table

db.execSQL("CREATE TABLE IF NOT EXISTS NeamLoggedin (Status);"wink;

// Select

String count = "SELECT COUNT (*) FROM NeamLoggedin";
Cursor result = db.rawQuery(count,null);
result.moveToFirst();
int icount = result.getInt(0);
if(icount>0) {
String status = result.getString(0);
}
// insert
else {
db.execSQL("INSERT INTO NeamLoggedin Values('newuser');"wink;
String status = result.getString(0);
}
Toast.makeText (getBaseContext(), "Data: " +status.toString(), Toast.LENGTH_SHORT).show();

db.close();
}
catch (Exception e) { Toast.makeText (getBaseContext(), "Error: " +e.toString(), Toast.LENGTH_LONG).show(); }
Re: Help With Sqlite Select by GodMode:
$
Re: Help With Sqlite Select by lordZOUGA(m): 9:23am On Apr 23, 2015
jinf, I think you have to confirm that your table was created successfully first.

seems as if your create table query is wrong, should be something like
create table if not exists namedloggedin(status varchar(32));
Re: Help With Sqlite Select by Jinf(op): 9:42am On Apr 23, 2015
lordZOUGA:
jinf, I think you have to confirm that your table was created successfully first.

seems as if your create table query is wrong, should be something like
create table if not exists namedloggedin(status varchar(32));
Thanks. i did this now and it gives the same error
Re: Help With Sqlite Select by lordZOUGA(m): 9:48am On Apr 23, 2015
Jinf:
Thanks. i did this now and it gives the same error
Your select query is missing a semi-colon. should be
String count = "SELECT COUNT (*) FROM NeamLoggedin;"; 
Re: Help With Sqlite Select by Jinf(op): 10:03am On Apr 23, 2015
lordZOUGA:
Your select query is missing a semi-colon. should be
String count = "SELECT COUNT (*) FROM NeamLoggedin;"; 
Thanks lordZOUGA i really appreciate your help. i've corrected that but still the error persists.
Re: Help With Sqlite Select by Jinf(op): 10:07am On Apr 23, 2015
I think the error is from the if statement downwards. I just removed the if & else statement & i didn't get any error.
Re: Help With Sqlite Select by lordZOUGA(m): 10:25am On Apr 23, 2015
Jinf:
I think the error is from the if statement downwards. I just removed the if & else statement & i didn't get any error.
did your code work? were you able to insert an item into the database?
Re: Help With Sqlite Select by Jinf(op): 10:45am On Apr 23, 2015
lordZOUGA:
did your code work? were you able to insert an item into the database?
I've fixed the error. The if(icount > 0) should hv been if(+icount > 0). Thanks for your help i couldn't have fixed it without your help. It's working now. But please i need help with adding WHERE to the statement. I want it to be WHERE status is loggedin
Re: Help With Sqlite Select by lordZOUGA(m): 11:00am On Apr 23, 2015
Jinf:
I've fixed the error. The if(icount > 0) should hv been if(+icount > 0).
I don't really understand how that fixed your problem. icount and +icount should be the same and if you did if(++icount > 0), it should always evaluate to true.
Thanks for your help i couldn't have fixed it without your help. It's working now. But please i need help with adding WHERE to the statement. I want it to be WHERE status is loggedin
select count(*) from namedloggedin where status = "loggedin"

visit here for more on sqlite: http://www.tutorialspoint.com/sqlite/

I also think you are doing sqlite databases on android in a "hacky" way, you should at least go through the doc and understand why things are the way they are before proceeding. http://developer.android.com/training/basics/data-storage/databases.html
Re: Help With Sqlite Select by kudaisi(m): 11:07am On Apr 23, 2015
Jinf:
Please can anyone help me on how to check if a column in sqlite table is empty before inserting data into that column.
-You need to declare the variable status outside the if/else statement. So that it can be within the scope of this line Toast.makeText (getBaseContext(), "Data: " +status.toString(), Toast.LENGTH_SHORT).show(); or else you'll keep getting a NullPointerException.
-Secondly, the String status = result.getString(0); in the else will return the same result as the in the if because you didn't re-execute db.rawQuery. For you to get the new result after the db.execSQL("INSERT INTO NeamLoggedin Values('newuser')"wink statement you need run the count sql script again. Otherwise, it won't give you any error but you won't get the desired result.

Happy coding.
Re: Help With Sqlite Select by Jinf(op): 11:10am On Apr 23, 2015
lordZOUGA:
I don't really understand how that fixed your problem. icount and +icount should be the same and if you did if(++icount > 0), it should always evaluate to true.

select count(*) from namedloggedin where status = "loggedin"

visit here for more on sqlite: http://www.tutorialspoint.com/sqlite/

I also think you are doing sqlite databases on android in a "hacky" way, you should at least go through the doc and understand why things are the way they are before proceeding. http://developer.android.com/training/basics/data-storage/databases.html
Thanks bro. I'm actually new to android programming.
Re: Help With Sqlite Select by Nobody: 6:48am On Apr 25, 2015
lordZOUGA:
did your code work? were you able to insert an item into the database?
Did you got my mail?
Re: Help With Sqlite Select by sureshdasari: 4:42pm On May 14, 2021
I know, it's too late to reply but it will help anyone looking for detailed info on sqlite select statement. The following sqlite topics will help better to understand select statement in sqlite.

SQLite Select Statement

To learn more about sqlite, refer the following

SQLite Tutorial
1 Reply

Help! Sqlite Database Not Updating After Using Bind_paramAnybody Here Know How One Can Password/encrypt Sqlite Database in C++?Help With Android Sqlite234

Eliminate Render-blocking Javascript And CSS In Above-the-fold ContentHelp! Which Debit Card Work On Google Pay?So What Exactly Happened To All This Banks