0

If i click a add button the values has to be stored in sqLite database. My problem is it stored the null values also how to avoid that one? help me friends

//creating database and table

    public class Database extends SQLiteOpenHelper {
            static int dbversion=1;
            static String dbname="expense.db";

            public Database(Context context)
                     {
                super(context, dbname, null, dbversion);
                // TODO Auto-generated constructor stub
            }

            @Override
            public void onCreate(SQLiteDatabase db) {
                db.execSQL("create table addsource(source text)");


//inserting values in database

    add.setOnClickListener(new OnClickListener() {

                    @Override
                    public void onClick(View v) {
                        // TODO Auto-generated method stub

                        Database db=new Database(Sett.this);
                        SQLiteDatabase sb=db.getReadableDatabase(); 

                        sb.execSQL("insert into addsource(source)values('"+sources.getText()+"')");
                        Toast.makeText(getApplicationContext(), "Registered Successfully",Toast.LENGTH_SHORT).show();

                    }
                });

            }
3
  • This code never inserts a NULL value. It might insert an empty string. Is it that that you want to avoid? Commented Aug 9, 2014 at 9:47
  • ya i want avoid the empty string Commented Aug 10, 2014 at 7:19
  • And what should happen when sources contains an empty string? Commented Aug 10, 2014 at 7:28

3 Answers 3

0

Why don't you test the value ?

if(sources != null){
    sb.execSQL("insert into addsource(source)values('"+sources.getText()+"')");
}
else{ 
    //insert what you want
}

Moreover, I don't think that you can insert null value, you might insert only an empty string with :

sources.getText()

So, instead of sources != null you can test sources.getText().length > 0 and insert what you want in case this is true.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for the response it still have the same problem
0

you can check values before inserting into database.Put values to strings and check the strings are not null on click of add button and then insert into table.

you can also use 'not null' in table creation

Creating table in sqlite android

1 Comment

Thanks for the response it still have the same problem
0

You have 3 ways:

1- add NOT NULL in create table :

CREATE TABLE your_table ( column_name INT NOT NULL, ....)

2- select the values that aren't null while using SELECT :

SELECT column_name FROM your_table WHERE column_name IS NOT NULL

3- check the variable value to see whether it is null or not:

if ( String.ValueOf(source) != null){   // for string example
      // add the source to the database
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.