Click here to Skip to main content
15,885,278 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
I am developing an apps project in which I want to create a database and add some details entered in editText into a table. On click of button 'OnBtnIncludeClick' I get an error stating 'Unfortunately, apps has stopped'. I am able to enter some text in editText.

Java
public class listmasteritems extends Activity implements OnClickListener{
public EditText editText;
    SQLiteDatabase db;
    String AccountName;

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_listmasteritems);
        editText = (EditText)findViewById(R.id.idRelationShip);
        editText.setOnClickListener(this);
    db=openOrCreateDatabase("myDb",MODE_PRIVATE,null);
    db.execSQL("CREATE TABLE IF NOT EXISTS TblAccountMaster (AccountId int primary key, AccountName varchar ) ");

    }

    public void onBtnIncludeClick( View v)
    {
        AccountName=editText.getText().toString();
        db.execSQL( "INSERT INTO TblAccountMaster VALUES('"+AccountName+"');");
        db.close();

    }
Posted
Updated 4-Mar-15 2:02am
v3
Comments
Richard MacCutchan 4-Mar-15 8:02am    
You need to look at the debug logs to see where the failure occurred, or step through the code in the debugger. You could also take a look at http://developer.android.com/reference/android/database/sqlite/SQLiteDatabase.html, to verify your database calls are correct.

Also, you should not call toStringon text values; they are already strings.
Richard Deeming 4-Mar-15 9:17am    
Your code is vulnerable to SQL Injection[^].

NEVER use string concatenation to build a SQL query. ALWAYS use a parameterized query.

1 solution

 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900