Solved

Android

Posted on 2013-01-03
3
322 Views
Last Modified: 2013-01-18
Activity.java

public class MainActivity extends Activity {
      Button bttncreate,bttninsert,bttnselect;
    EditText txtEmpName,txtEmpId;
    TextView txtResult;
      private Cursor cursor;
      public SQLiteDatabase database;
      public TextView EmployeeName;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        bttncreate=(Button)findViewById(R.id.button1);
                bttninsert=(Button)findViewById(R.id.button2);
                bttnselect=(Button)findViewById(R.id.button3);
                txtEmpName=(EditText)findViewById(R.id.txtEmpName);
                txtEmpId=(EditText)findViewById(R.id.txtEmpId);
                txtResult=(TextView)findViewById(R.id.txtOutput);
                DataHandler dh=new  DataHandler();
                DatabaseHelper dhh=new DatabaseHelper(this);
                database = dhh.getWritableDatabase();
               bttncreate.setOnClickListener(dh);
               bttninsert.setOnClickListener(dh);
               bttnselect.setOnClickListener(dh);
            }
   
class DataHandler implements OnClickListener
{

    @Override
    public void onClick(View view) {
        // TODO Auto-generated method stub
   
       
            Button b=(Button)view;
            if(b.equals(bttncreate))
            {
                try
                {
                    database.execSQL( "create table Employees(EmployeeName text primary key,EmployeeID integer not null )");
                    txtResult.setText("Table Created");
                }
                catch (Exception ex) {
                    // TODO: handle exception
                    txtResult.setText(ex.getMessage());
                }
            }
            if(b.equals(bttninsert))
            {
                try
                {
                    String EmployeeName="" + txtEmpName.getText();
                    EmployeeName=EmployeeName.replaceAll("'", "''").trim();
                    String EmployeeID="" + txtEmpId.getText();
                    EmployeeID=EmployeeID.replaceAll("'","''").trim();
                    database.execSQL("insert into Employees values(" + EmployeeName + "," + EmployeeID +")");
                    txtResult.setText("Data Inserted");
                }
                catch (Exception ex) {
                    // TODO: handle exception
                    txtResult.setText(ex.getMessage());
                }
            }
           
            if(b.equals(bttnselect))
            {
                try
                {
                      String EmployeeName="" + txtEmpName.getText();
                    EmployeeName=EmployeeName.replaceAll("'", "''").trim();
                   
               
                    String[] columns={"EmployeeID"};
                    String selection="EmployeeName='"+ EmployeeName + "'";
                   
                cursor=    database.query("Employee", columns, selection, null, null, null, null);
                if(cursor==null)
                {
                    txtResult.setText("No Data Found");
                    return;
                }
                if(cursor.moveToFirst())
                {
                int EmployeeID=cursor.getInt(0);
                txtEmpId.setText("" + EmployeeID);
                    txtResult.setText("Data Selected");
                }
                else
                    txtResult.setText("No Data Found");
                    cursor.close();
                }
                catch (Exception ex) {
                    // TODO: handle exception
                    txtResult.setText(ex.getMessage());
                }
            }
        }

}
        Helper.java

public class DatabaseHelper extends SQLiteOpenHelper {
      
      public DatabaseHelper(Context context) {
        super(context, "Employees", null, 1);
      }

 public void onCreate(SQLiteDatabase database) {
                    // TODO Auto-generated method stub
                   

                }

                @Override
                public void onUpgrade(SQLiteDatabase database, int oldversion, int newversion) {
                    // TODO Auto-generated method stub
                   
                }

            

      }
-i could not insert any record table gets created . help me experts
0
Comment
Question by:codeoxygen
3 Comments
 
LVL 74

Expert Comment

by:käµfm³d 👽
Comment Utility
What is your question?
0
 

Author Comment

by:codeoxygen
Comment Utility
i am unable to insert data in the above code, it say me as error on compiling inset into values ...
the table is created
0
 
LVL 16

Accepted Solution

by:
Peter Kwan earned 500 total points
Comment Utility
You are missing single quote in the following line:

database.execSQL("insert into Employees values(" + EmployeeName + "," + EmployeeID +")");


It should be

database.execSQL("insert into Employees values('" + EmployeeName + "'," + EmployeeID +")");
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

I tend toward trying the newest hardware and software.  Thiss sometimes works out to my benefit, and sometimes not.  Because I downloaded and installed Android 5.x (http://www.experts-exchange.com/articles/18084/Upgrading-to-Android-5-0-Lollipop.htm…
Introduction This article is a continuation of Part-1 as evident from the title. Please visit this page (http://www.experts-exchange.com/articles/18706/Backing-up-personal-data-on-your-Android-device-Part-1.html) to read the earlier Part -1. If y…
This video is in connection to the article "The case of a missing mobile phone (https://www.experts-exchange.com/articles/28474/The-Case-of-a-Missing-Mobile-Phone.html)". It will help one to understand clearly the steps to track a lost android phone.
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

7 Experts available now in Live!

Get 1:1 Help Now