在我的应用程序的onCreate()
方法中,我使用网络服务器和SQLite
脚本将数据从MYSQL
数据库上传到PHP
数据库。
数据上传正确,但问题是每次应用程序启动时数据库都会填充duplicate
个数据。
如何添加validation
以确保如果数据库已填充了特定数据,然后do not
重新上传,则只上传new
数据?
OnCreate()代码:
// create a instance of SQLite Database
loginDataBaseAdapter = new LoginDataBaseAdapter(this);
loginDataBaseAdapter = loginDataBaseAdapter.open();
ConnectionDetector cd = new ConnectionDetector(LoginHome.this);
Boolean isInternetPresent = cd.CheckInternet(); // true or false
if (isInternetPresent) {
Cursor cur = loginDataBaseAdapter.getSinlgeEntry();
if (cur != null) {
cur.moveToFirst();
while (cur.isAfterLast() == false) {
String sessionId = cur.getString(cur
.getColumnIndex("sessionid"));
String game = cur.getString(cur.getColumnIndex("game"));
String name = cur.getString(cur.getColumnIndex("name"));
int avgAttention = Integer.parseInt(cur.getString(cur
.getColumnIndex("avgattention")));
int avgMediation = Integer.parseInt(cur.getString(cur
.getColumnIndex("avgmeditation")));
int maxAttention = Integer.parseInt(cur.getString(cur
.getColumnIndex("maxattention")));
int maxMediation = Integer.parseInt(cur.getString(cur
.getColumnIndex("maxmeditation")));
int score = Integer.parseInt(cur.getString(cur
.getColumnIndex("score")));
String date = cur.getString(cur.getColumnIndex("date"));
if (!sessionId.equals(null))
UploadDataAuto(sessionId, game, name,
avgAttention, avgMediation, maxAttention,
maxMediation,score, date);
// }
cur.moveToNext();
}
}
}
UploadDataAuto方法:
public void UploadDataAuto(String sessionId, String game, String name,
Integer score, Integer avgAttn, Integer avgMed, Integer maxAttn,
Integer maxMed, String date){
// Defined URL where to send data
String ServerUrl = "http://ry.net16.net/gameRegistration.php?sessionId="
+ sessionId
+ "&game="
+ game
+ "&name="
+ name
+ "&avgMed="
+ avgMed
+ "&maxMed="
+ maxMed
+ "&avgAttn="
+ avgAttn
+ "&maxAttn="
+ maxAttn
+ "&score="
+ score
+ "&date="
+ date
+ "";
LongOperation longOperation = new LongOperation();
longOperation.sessionId = sessionId;
longOperation.execute(ServerUrl);
}
私人班级长期操作:
private class LongOperation extends AsyncTask<String, String, Void> {
// Required initialization
private final HttpClient Client = new DefaultHttpClient();
private String Content;
private String Error = null;
private ProgressDialog Dialog = new ProgressDialog(LoginHome.this);
String data = "";
int sizeData = 0;
public String sessionId = "";
protected void onPreExecute() {
// NOTE: You can call UI Element here.
// Start Progress Dialog (Message)
Dialog.setMessage("Please wait.. Data is uploading .");
Dialog.show();
}
// Call after onPreExecute method
protected Void doInBackground(String... urls) {
/************ Make Post Call To Web Server ***********/
BufferedReader reader = null;
// Send data
try {
// Defined URL where to send data
URL url = new URL(urls[0]);
// Send POST data request
URLConnection conn = url.openConnection();
conn.setDoOutput(true);
OutputStreamWriter wr = new OutputStreamWriter(
conn.getOutputStream());
wr.write(data);
wr.flush();
// Get the server response
reader = new BufferedReader(new InputStreamReader(
conn.getInputStream()));
StringBuilder sb = new StringBuilder();
String line = null;
// Read Server Response
while ((line = reader.readLine()) != null) {
// Append server response in string
sb.append(line + "");
}
// Append Server Response To Content String
Content = sb.toString();
} catch (Exception ex) {
Error = ex.getMessage();
} finally {
try {
reader.close();
}
catch (Exception ex) {
}
}
/*****************************************************/
return null;
}
protected void onPostExecute(Void unused) {
// NOTE: You can call UI Element here.
// Close progress dialog
Dialog.dismiss();
if (Error != null) {
// uiUpdate.setText("Output : "+Error);
} else {
// Show Response Json On Screen (activity)
// uiUpdate.setText( Content );
/****************** Start Parse Response JSON Data *************/
String OutputData = "";
JSONObject jsonResponse;
try {
/******
* Creates a new JSONObject with name/value mappings from
* the JSON string.
********/
jsonResponse = new JSONObject(Content);
String result = jsonResponse.get("result").toString();
if (result.equals("true")) {
//loginDataBaseAdapter.deleteUploadedRecord(sessionId);
Toast.makeText(LoginHome.this,
"Data is successfully uploaded.",
Toast.LENGTH_LONG).show();
} else {
Toast.makeText(
LoginHome.this,
"Error while uploading. Please try again later.",
Toast.LENGTH_LONG).show();
}
}
/****************** End Parse Response JSON Data *************/
catch (JSONException e) {
e.printStackTrace();
}
}
}
}
答案 0 :(得分:0)
尝试将UNIQUE
索引应用于您的列,以避免重复输入,here就是这样。希望它有所帮助!
答案 1 :(得分:0)
我有这种情况,我认为这也可能对你有所帮助:
1)在数据库中添加一个名为uploaded
的字段,默认值为0
2)选择要上传uploaded = 0
3)使用您的JSON请求发送记录唯一ID
4)更改您的Web服务以在响应
中发回ID 5)当jsonResponse
从响应中获取ID并更新时,在results = true
中
记录。将字段uploaded
设置为1
这将确保所有记录都成功添加到您的数据库,并且只上传新记录。在maht0rz建议的服务器表中应用UNIQUE索引也是一个好主意。 希望有所帮助