Welcome, Guest: Register On Nairaland / LOGIN! / Trending / Recent / New
Stats: 3,150,013 members, 7,806,947 topics. Date: Wednesday, 24 April 2024 at 07:34 AM

Help With Sqlite Select - Programming - Nairaland

Nairaland Forum / Science/Technology / Programming / Help With Sqlite Select (1200 Views)

Help! Sqlite Database Not Updating After Using Bind_param / Anybody Here Know How One Can Password/encrypt Sqlite Database in C++? / Help With Android Sqlite (2) (3) (4)

(1) (Reply) (Go Down)

Help With Sqlite Select by Jinf: 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: 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: 8:59am On Apr 23, 2015
$
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));

1 Like

Re: Help With Sqlite Select by Jinf: 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;"; 

1 Like

Re: Help With Sqlite Select by Jinf: 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: 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: 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

1 Like

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: 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)

Why Is Java Programming So Important? / The 3 Ways To Fix "This Video Is Not Available In Your Country" In Youtube.. / Learning Python, Using Python At Work/college: Must-try Best Python Quizzes

(Go Up)

Sections: politics (1) business autos (1) jobs (1) career education (1) romance computers phones travel sports fashion health
religion celebs tv-movies music-radio literature webmasters programming techmarket

Links: (1) (2) (3) (4) (5) (6) (7) (8) (9) (10)

Nairaland - Copyright © 2005 - 2024 Oluwaseun Osewa. All rights reserved. See How To Advertise. 21
Disclaimer: Every Nairaland member is solely responsible for anything that he/she posts or uploads on Nairaland.