I want to replicate my data in mysql from android sqlite database and any changes made in mysql should be reflected in sqlite. I have made the following app.There is only one activity. This is the code
public class Activity1 extends Activity {
EditText fname,lname,tele,addrs,dates,pins;
Spinner states;
Button addData,modifyData,viewData,deleteData,clearData;
SQLiteDatabase db;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_activity1);
fname = (EditText) findViewById(R.id.editText);
lname = (EditText) findViewById(R.id.editText2);
tele = (EditText) findViewById(R.id.editText3);
addrs = (EditText) findViewById(R.id.editText4);
dates = (EditText) findViewById(R.id.editText5);
pins = (EditText) findViewById(R.id.editText6);
addData =(Button) findViewById(R.id.button);
modifyData =(Button) findViewById(R.id.button3);
viewData =(Button) findViewById(R.id.button5);
deleteData =(Button) findViewById(R.id.button2);
clearData =(Button) findViewById(R.id.button4);
states =(Spinner) findViewById(R.id.spinner);
db=openOrCreateDatabase("UserDB", Context.MODE_PRIVATE, null);
db.execSQL("CREATE TABLE IF NOT EXISTS user(fname VARCHAR,lname VARCHAR,tele VARCHAR,addr VARCHAR,state VARCHAR,pin VARCHAR,dob VARCHAR);");
deleteData.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if(fname.getText().toString().trim().length()==0)
{
showMessage("Error", "Please enter first name");
return;
}
Cursor c=db.rawQuery("SELECT * FROM user WHERE fname='"+fname.getText()+"'", null);
if(c.moveToFirst())
{
db.execSQL("DELETE FROM user WHERE fname='"+fname.getText()+"'");
showMessage("Success", "Data Deleted");
}
else
{
showMessage("Error", "Invalid Name");
}
clearText();
}
});
addData.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if(fname.getText().toString().trim().length()==0||
lname.getText().toString().trim().length()==0||
tele.getText().toString().trim().length()==0 || addrs.getText().toString().trim().length()==0
|| dates.getText().toString().trim().length()==0 ||
pins.getText().toString().trim().length()==0)
{
showMessage("Error", "Please enter all values");
return;
}
db.execSQL("INSERT INTO user VALUES('"+fname.getText()+"','"+lname.getText()+"','"+tele.getText()+"','"+addrs.getText()+"','"+states.getSelectedItem().toString()+"','"+pins.getText()+"'" +
",'"+dates.getText()+"');");
showMessage("Success", "Record added");
clearText();
}
});
modifyData.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if(fname.getText().toString().trim().length()==0)
{
showMessage("Error", "Please enter first name");
return;
}
Cursor c=db.rawQuery("SELECT * FROM user WHERE fname='"+fname.getText()+"'", null);
if(c.moveToFirst())
{
db.execSQL("UPDATE user SET lname='"+lname.getText()+"',tele='"+tele.getText()+"',addrs='"+addrs.getText()+"',state='"+states.getSelectedItem().toString()+"',pin='"+pins.getText()+"',dob='"+dates.getText()+"' WHERE fname='"+fname.getText()+"');");
showMessage("Success", "Data Modified");
}
else
{
showMessage("Error", "Invalid Name");
}
clearText();
}
});
viewData.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Cursor c = db.rawQuery("SELECT * FROM user", null);
if (c.getCount() == 0) {
showMessage("Error", "No data found");
return;
}
StringBuffer buffer = new StringBuffer();
while (c.moveToNext()) {
buffer.append("First Name: " + c.getString(0) + "\n");
buffer.append("Last Name: " + c.getString(1) + "\n");
buffer.append("Telephone: " + c.getString(2) + "\n");
buffer.append("Address: " + c.getString(3) + "\n");
buffer.append("State: " + c.getString(4) + "\n");
buffer.append("Pincode: " + c.getString(5) + "\n");
buffer.append("date of birth: " + c.getString(6) + "\n\n\n");
}
showMessage("User Data", buffer.toString());
}
});
clearData.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
clearText();
}
});
}
This is the code snippet of symmetricDS
services I got from this tutorial
final String HELPER_KEY = "NotePadHelperKey";
// Register the database helper, so it can be shared with the SymmetricService
SQLiteOpenHelperRegistry.register(HELPER_KEY, mOpenHelper);Intent intent = new Intent(getContext(), SymmetricService.class);// Notify the service of the database helper key
intent.putExtra(SymmetricService.INTENTKEY_SQLITEOPENHELPER_REGISTRY_KEY, HELPER_KEY);
intent.putExtra(SymmetricService.INTENTKEY_REGISTRATION_URL, "http://ift.tt/1BtzTFC");
intent.putExtra(SymmetricService.INTENTKEY_EXTERNAL_ID, "android-simulator");
intent.putExtra(SymmetricService.INTENTKEY_NODE_GROUP_ID, "client");
intent.putExtra(SymmetricService.INTENTKEY_START_IN_BACKGROUND, true);Properties properties = new Properties();
// initial load existing notes from the Client to the Server
properties.setProperty(ParameterConstants.AUTO_RELOAD_REVERSE_ENABLED, "true");
intent.putExtra(SymmetricService.INTENTKEY_PROPERTIES, properties);getContext().startService(intent);
In tutorial its mentioned that the code snippet is to be added in onCreate What are the changes I should make to make the application work?
Aucun commentaire:
Enregistrer un commentaire