我是android開發中的新手。我必須將我們的本地數據庫與SQL Server同步。在android中的同步
可以通過SQLite完成同步,或者我們需要使用任何其他類似的CouchDB。
如果我使用沙發DB,那麼CouchDB與SQLServer同步。
請提供SYNC的任何教程。
在此先感謝。
我是android開發中的新手。我必須將我們的本地數據庫與SQL Server同步。在android中的同步
可以通過SQLite完成同步,或者我們需要使用任何其他類似的CouchDB。
如果我使用沙發DB,那麼CouchDB與SQLServer同步。
請提供SYNC的任何教程。
在此先感謝。
檢查解決方案由提供。這是一個演示Android應用程序的源代碼,它實現了MySQL服務器和Android設備上的SQLite數據庫之間的MySQL複製。
這裏我給出了Android應用程序中使用php的MySql(ex)微調器的本地數據庫連接。
我希望它對你有幫助。
請參閱本守則...
MainActivity.java
public class MainActivity extends Activity {
InputStream is=null;
String result=null;
String line=null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
try
{
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://10.0.2.2/spinner.php");
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
is = entity.getContent();
Log.e("Pass 1", "connection success ");
}
catch(Exception e)
{
Log.e("Fail 1", e.toString());
Toast.makeText(getApplicationContext(), "Invalid IP Address",Toast.LENGTH_LONG).show();
}
try
{
BufferedReader reader = new BufferedReader(new InputStreamReader(is,"iso-8859-1"),8);
StringBuilder sb = new StringBuilder();
while ((line = reader.readLine()) != null)
{
sb.append(line + "\n");
}
is.close();
result = sb.toString();
Log.e("Pass 2", "connection success ");
}
catch(Exception e)
{
Log.e("Fail 2", e.toString());
}
try
{
JSONArray JA=new JSONArray(result);
JSONObject json= null;
final String[] str1 = new String[JA.length()];
final String[] str2 = new String[JA.length()];
for(int i=0;i<JA.length();i++)
{
json=JA.getJSONObject(i);
str1[i] = json.getString("roll_no");
str2[i]=json.getString("name");
}
final Spinner sp = (Spinner) findViewById(R.id.spinner1);
List<String> list = new ArrayList<String>();
for(int i=0;i<str2.length;i++)
{
list.add(str2[i]);
}
Collections.sort(list);
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(getApplicationContext(),
android.R.layout.simple_spinner_item, list);
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
sp.setAdapter(dataAdapter);
sp.setOnItemSelectedListener(new OnItemSelectedListener()
{
@Override
public void onItemSelected(AdapterView<?> arg0, View arg1,int position, long id)
{
// TODO Auto-generated method stub
String item=sp.getSelectedItem().toString();
Toast.makeText(getApplicationContext(), item,Toast.LENGTH_LONG).show();
Log.e("Item",item);
}
@Override
public void onNothingSelected(AdapterView<?> arg0)
{
// TODO Auto-generated method stub
}
});
}
catch(Exception e)
{
Log.e("Fail 3", e.toString());
}
}
spinner.php
<?php
$host='127.0.0.1';
$uname='root';
$pwd='password';
$db='android';
$con = mysql_connect($host,$uname,$pwd) or die("connection failed");
mysql_select_db($db,$con) or die("db selection failed");
$r=mysql_query("select * from class",$con);
while($row=mysql_fetch_array($r))
{
$cls[]=$row;
}
print(json_encode($cls));
mysql_close($con);
?>
添加以下代碼AndroidManifest.xml中
<uses-permission android:name="android.permission.INTERNET"/>