Welcome, Guest: Register On Nairaland / LOGIN! / Trending / Recent / NewStats: 3,217,559 members, 8,034,641 topics. Date: Sunday, 22 December 2024 at 07:40 AM |
Nairaland Forum / Science/Technology / Programming / Help With Sqlite Select (1370 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)
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);" // 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');" 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: 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: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: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: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: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 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 1 Like |
Re: Help With Sqlite Select by kudaisi(m): 11:07am On Apr 23, 2015 |
Jinf: -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')" 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: Thanks bro. I'm actually new to android programming. |
Re: Help With Sqlite Select by Nobody: 6:48am On Apr 25, 2015 |
lordZOUGA: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)
Let's Code / Advanced Tic Tac Toe Online Game / 16 Interesting Secrets About Mark Zuckerberg
(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. 20 |