[Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Android

Posted on 2013-01-03
3
Medium Priority
?
338 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 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

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

Question has a verified solution.

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

Displaying an arrayList in a listView using the default adapter is rarely the best solution. To get full control of your display data, and to be able to refresh it after editing, requires the use of a custom adapter.
If your app took Google’s lash recently, here are the 5 most likely reasons.
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.
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…
Suggested Courses

865 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