Avatar of codeoxygen
codeoxygen

asked on 

Android

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
Android

Avatar of undefined
Last Comment
Peter Kwan
Avatar of kaufmed
kaufmed
Flag of United States of America image

What is your question?
Avatar of codeoxygen
codeoxygen

ASKER

i am unable to insert data in the above code, it say me as error on compiling inset into values ...
the table is created
ASKER CERTIFIED SOLUTION
Avatar of Peter Kwan
Peter Kwan
Flag of Hong Kong image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Android
Android

Android is a mobile operating system developed by Google, based on the Linux kernel and designed primarily for touchscreen mobile devices such as smartphones and tablets. Android's user interface is based on direct manipulation, using touch gestures that loosely correspond to real-world actions, such as swiping, tapping and pinching, to manipulate on-screen objects, along with a virtual keyboard for text input. In addition to touchscreen devices, variants of Android are also used on notebooks, game consoles, digital cameras, televisions, automobiles and other electronics. Applications are usually developed in Java programming language using the Android software development kit (SDK), but other development environments are also available, including Delphi, Ruby and Visual Studio (using C++).

7K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo