?
Solved

Android

Posted on 2013-01-03
3
Medium Priority
?
333 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 38742931
What is your question?
0
 

Author Comment

by:codeoxygen
ID: 38743060
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 1500 total points
ID: 38743316
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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Once again I push the limits of my phone.  An introduction to the Android Google Now Launcher.
There is a lot to be said for protecting yourself and your accounts with 2 factor authentication.  I found to my own chagrin, that there is a big downside as well.
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.
This video demonstrates how to sync Microsoft Exchange Public Folders with smartphones using CodeTwo Exchange Sync and Exchange ActiveSync. To learn more about CodeTwo Exchange Sync and download the free trial, go to: http://www.codetwo.com/excha…

801 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