Solved

Android

Posted on 2013-01-03
3
324 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 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 500 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

I recently asked a question (http://www.experts-exchange.com/Programming/Smartphones/Android/Q_28684946.html) about Computer Inventory applications for Mobile Devices.  I was specifically interested in an app I could use on my android phone.  The be…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
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…

895 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

11 Experts available now in Live!

Get 1:1 Help Now