本文介绍了如何发送的所有数据的SQLite在线服务器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 我的的将数据存储到SQLiteDatabase 的其存储到SD卡,现在我要所有的SQLite的数据发送到服务器即可。注:我得服务器数据库中的创建相同的字段以及(呈三角以SQLite的DB)为如: PersonName的 下面$ C $词用来检查,我能够将数据存储到服务器(用于测试目的 - 我接受的数据由用户进入的EditText ),然后的发送到服务器的和我是成功的在 字符串的URL =HTTP://localhost/ChurchData.php清单&LT;&的NameValuePair GT; PARAMS =新的ArrayList&LT;&的NameValuePair GT;();params.add(新BasicNameValuePair(sPersonName,editPersonName.getText()的toString())。);字符串resultServer = getHttpPost(URL,则params);Log.d(整个字符串::,+ resultServer);/*** 默认值 ***/strStatusID =0;STRERROR =;的JSONObject℃;尝试{C =新的JSONObject(resultServer);strStatusID = c.getString(StatusID);STRERROR = c.getString(信息);}赶上(JSONException E){// TODO自动生成catch块e.printStackTrace(); } // prepare保存数据 如果(strStatusID.equals(0)) {Toast.makeText(getApplicationContext(),!已经存在,Toast.LENGTH_LONG).show(); } 其他 {Toast.makeText(getApplicationContext(),数据上传成功!,Toast.LENGTH_SHORT).show(); } 返回true;}私人字符串getHttpPost(字符串URL, 清单&LT;&的NameValuePair GT; PARAMS){ StringBuilder的海峡=新的StringBuilder(); HttpClient的客户端=新DefaultHttpClient(); HttpPost httpPost =新HttpPost(URL); 尝试{httpPost.setEntity(新UrlEn codedFormEntity(PARAMS));HTT presponse响应= client.execute(httpPost);状态行状态行= response.getStatusLine();INT状态code = statusLine.getStatus code();如果(状态code == 200){//状态OKHttpEntity实体= response.getEntity();InputStream的内容= entity.getContent();读者的BufferedReader =新的BufferedReader(新的InputStreamReader(内容));串线;而((行= reader.readLine())!= NULL){str.append(线);}}其他{Log.e(日志,无法下载结果..);}}赶上(ClientProtocolException E){ e.printStackTrace();}赶上(IOException异常五){e.printStackTrace();} 返回str.toString();} 所以我可能知道, 我如何发送SQLite数据库记录到服务器的?我的数据库类是这样的: 公共类myDBClasss扩展SQLiteOpenHelper { //数据库版本 私有静态最终诠释DATABASE_VERSION = 2; //数据库名称 私有静态最后弦乐DATABASE_NAME =ChurchDB; //表名 私有静态最后弦乐TABLE_MEMBER =数据表; 公共myDBClasss(上下文的背景下){ //存储数据到SD卡 超(背景下,Environment.getExternalStorageDirectory() +文件分割符+ChurchData +文件分割符+ DATABASE_NAME,空,DATABASE_VERSION); } @覆盖 公共无效的onCreate(SQLiteDatabase DB){ // TODO自动生成方法存根 //创建表名 db.execSQL(CREATE TABLE+ TABLE_MEMBER + (PersonName的VARCHAR(100),+ PersonEmail VARCHAR(100),+ PersonTelephone VARCHAR(100),+ 通讯VARCHAR(100));); //复选框 Log.d(CREATE TABLE,创建表成功 - 班组长); } //插入数据 众长insertData(字符串strPersonName,字符串strPersonEmail,字符串strPersonTelephone,字符串strNewsletter){ // TODO自动生成方法存根 尝试{ SQLiteDatabase分贝; DB = this.getWritableDatabase(); //写数据 ContentValues瓦尔=新ContentValues(); Val.put(PersonName的strPersonName); Val.put(PersonEmail,strPersonEmail); Val.put(PersonTelephone,strPersonTelephone); Val.put(通讯,strNewsletter); //复选框 长行数= db.insert(TABLE_MEMBER,空,缬氨酸); db.close(); 返回行; //返回插入的行。 }赶上(例外五){ 返回-1; } } //更新数据 众长了updateData(字符串strPersonName,字符串strPersonEmail,字符串strPersonTelephone,字符串strNewsletter){ // TODO自动生成方法存根 尝试{ SQLiteDatabase分贝; DB = this.getWritableDatabase(); //写数据 ContentValues瓦尔=新ContentValues(); Val.put(PersonName的strPersonName); Val.put(PersonEmail,strPersonEmail); Val.put(PersonTelephone,strPersonTelephone); Val.put(通讯,strNewsletter); //复选框 长行数= db.update(TABLE_MEMBER,缬氨酸,PersonName的=?, 新的String [] {将String.valueOf(strPersonName)}); db.close(); 返回行; //返回更新的行。 }赶上(例外五){ 返回-1; } } //获取数据 公众的String [] selectData(字符串strPersonName){ // TODO自动生成方法存根 尝试{ 字符串arrData [] = NULL; SQLiteDatabase分贝; DB = this.getReadableDatabase(); //读取数据 光标光标= db.query(TABLE_MEMBER,新的String [] {*}, PersonName的=?, 新的String [] {将String.valueOf(strPersonName)},NULL,NULL,NULL,NULL); 如果(指针!= NULL) { 如果(cursor.moveToFirst()){ arrData =新的String [cursor.getColumnCount()]; arrData [0] = cursor.getString(0); arrData [1] = cursor.getString(1); arrData [2] = cursor.getString(2); arrData [3] = cursor.getString(3); //复选框 } } cursor.close(); db.close(); 返回arrData; }赶上(例外五){ 返回null; } } //检查使用PersonName的现场数据(S) 公共布尔存在(字符串strImageName){ SQLiteDatabase分贝; DB = this.getReadableDatabase(); //读取数据 光标光标= db.rawQuery(从数据表,其中PersonName的选择= 1? 新的String [] {strImageName}); 布尔存在=(cursor.getCount()大于0); cursor.close(); 返回的存在; } @覆盖 公共无效onUpgrade(SQLiteDatabase分贝,INT oldVersion,诠释静态网页){ // TODO自动生成方法存根 db.execSQL(DROP TABLE IF EXISTS+ TABLE_MEMBER); //重新建立对方法的onCreate 的onCreate(DB); }} 解决方案 我觉得简单的方法是,你所有的sqlite的数据转换成XML或JSON,然后只有一个HTTP请求是要求所有的数据发送到网络服务器。在网上服务器,你可以轻松地分析你的数据,你已经知道不管你用你的XML或JSON的结构。让说,你有你的数据库2场。 ID,名称。你有10个记录。您将您的所有记录成JSON。让说你查询你的数据库中的所有记录,现在光标对象将持有所有sqlite的数据。添加 getAllData()方法来检索所有数据库中的数据。 公共光标getAllData(){ 字符串selectQuery =选择*+ TABLE_MEMBER; SQLiteDatabase分贝= this.getReadableDatabase(); 光标光标= db.rawQuery(selectQuery,NULL); 返回游标;} 现在要做的, 光标光标= getAllData(); //光标保存所有数据的JSONObject jobj;JSONArray ARR =新JSONArray();cursor.moveToFIrst();而(cursor.moveToNext()){ jobj =新的JSONObject(); jboj.put(ID,cursor.getInt(ID)); jboj.put(名,cursor.getString(姓名)); arr.put(jobj);}jobj =新的JSONObject();jobj.put(数据,ARR);串ST = jboj.toString(); 现在只是让用字符串参数的HTTP调用和发送由这个字符串转换成的JSONObject到server.and在解析服务器。现在根据你的code,执行 列表&LT;&的NameValuePair GT; PARAMS =新的ArrayList&LT;&的NameValuePair GT;();params.add(新BasicNameValuePair(ALLDATA,ST));字符串resultServer = getHttpPost(URL,则params); I am storing data into SQLiteDatabase which is stored into SD Card, now i have to send all SQLite data to server.Note: I have created same fields to server database as well (simillar to SQLite DB) for an eg: PersonNameBelow code i used to check, am i able to store data to server (for testing purpose - i accepted data by user into edittext) and then sent to server, and i was successful in that.String url = "http://localhost/ChurchData.php";List<NameValuePair> params = new ArrayList<NameValuePair>();params.add(new BasicNameValuePair("sPersonName", editPersonName.getText().toString()));String resultServer = getHttpPost(url,params);Log.d("Entire string::", " " + resultServer);/*** Default Value ***/strStatusID = "0";strError = "";JSONObject c;try {c = new JSONObject(resultServer);strStatusID = c.getString("StatusID");strError = c.getString("Message");} catch (JSONException e) {// TODO Auto-generated catch blocke.printStackTrace(); } // prepare save data if(strStatusID.equals("0")) {Toast.makeText(getApplicationContext(), "Already Exist !", Toast.LENGTH_LONG).show(); } else {Toast.makeText(getApplicationContext(), "Data Uploaded Successfully!", Toast.LENGTH_SHORT).show(); } return true;}private String getHttpPost(String url, List<NameValuePair> params) { StringBuilder str = new StringBuilder(); HttpClient client = new DefaultHttpClient(); HttpPost httpPost = new HttpPost(url); try {httpPost.setEntity(new UrlEncodedFormEntity(params));HttpResponse response = client.execute(httpPost);StatusLine statusLine = response.getStatusLine();int statusCode = statusLine.getStatusCode();if (statusCode == 200) { // Status OKHttpEntity entity = response.getEntity();InputStream content = entity.getContent();BufferedReader reader = new BufferedReader(new InputStreamReader(content));String line;while ((line = reader.readLine()) != null) {str.append(line);}} else {Log.e("Log", "Failed to download result..");}} catch (ClientProtocolException e) { e.printStackTrace();} catch (IOException e) {e.printStackTrace();} return str.toString();}So may i know, How can i send SQLite database records to server ? My database class looks like this:public class myDBClasss extends SQLiteOpenHelper { // Database Version private static final int DATABASE_VERSION = 2; // Database Name private static final String DATABASE_NAME = "ChurchDB"; // Table Name private static final String TABLE_MEMBER = "DataTable"; public myDBClasss(Context context) { // to store data into SD Card super(context, Environment.getExternalStorageDirectory() + File.separator + "ChurchData" + File.separator + DATABASE_NAME, null, DATABASE_VERSION); } @Override public void onCreate(SQLiteDatabase db) { // TODO Auto-generated method stub // Create Table Name db.execSQL("CREATE TABLE " + TABLE_MEMBER + "(PersonName VARCHAR(100)," + " PersonEmail VARCHAR(100)," + " PersonTelephone VARCHAR(100)," + " Newsletter VARCHAR(100));"); // checkbox Log.d("CREATE TABLE","Create Table Successfully - classs"); } // Insert Data public long insertData(String strPersonName, String strPersonEmail, String strPersonTelephone, String strNewsletter) { // TODO Auto-generated method stub try { SQLiteDatabase db; db = this.getWritableDatabase(); // Write Data ContentValues Val = new ContentValues(); Val.put("PersonName", strPersonName); Val.put("PersonEmail", strPersonEmail); Val.put("PersonTelephone", strPersonTelephone); Val.put("Newsletter", strNewsletter); // checkbox long rows = db.insert(TABLE_MEMBER, null, Val); db.close(); return rows; // return rows inserted. } catch (Exception e) { return -1; } } // Update Data public long updateData(String strPersonName, String strPersonEmail, String strPersonTelephone, String strNewsletter){ // TODO Auto-generated method stub try { SQLiteDatabase db; db = this.getWritableDatabase(); // Write Data ContentValues Val = new ContentValues(); Val.put("PersonName", strPersonName); Val.put("PersonEmail", strPersonEmail); Val.put("PersonTelephone", strPersonTelephone); Val.put("Newsletter", strNewsletter); // checkbox long rows = db.update(TABLE_MEMBER, Val, "PersonName=?", new String[] { String.valueOf(strPersonName) }); db.close(); return rows; // return rows updated. } catch (Exception e) { return -1; } } // Fetch data public String[] selectData(String strPersonName) { // TODO Auto-generated method stub try { String arrData[] = null; SQLiteDatabase db; db = this.getReadableDatabase(); // Read Data Cursor cursor = db.query(TABLE_MEMBER, new String[] { "*" }, "PersonName=?", new String[] { String.valueOf(strPersonName) }, null, null, null, null); if(cursor != null) { if (cursor.moveToFirst()) { arrData = new String[cursor.getColumnCount()]; arrData[0] = cursor.getString(0); arrData[1] = cursor.getString(1); arrData[2] = cursor.getString(2); arrData[3] = cursor.getString(3); // checkbox } } cursor.close(); db.close(); return arrData; } catch (Exception e) { return null; } } // Check for data(s) using PersonName field public boolean exists(String strImageName) { SQLiteDatabase db; db = this.getReadableDatabase(); // Read Data Cursor cursor = db.rawQuery("select 1 from DataTable where PersonName= ?", new String[] { strImageName }); boolean exists = (cursor.getCount() > 0); cursor.close(); return exists; } @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { // TODO Auto-generated method stub db.execSQL("DROP TABLE IF EXISTS " + TABLE_MEMBER); // Re Create on method onCreate onCreate(db); }} 解决方案 I think easy way is that you convert all your sqlite data into xml or json and then only one http request is required to send all your data to online server. At online server you can easily parse your data as you already know the structure of your xml or json whatever you used.let say you have 2 fields in your database. ID , Name. you have 10 records. you convert all your records into json .let say you query your database for all records and now cursor object will hold all your sqlite data.add getAllData() method to retrieve all your database data.public Cursor getAllData() { String selectQuery = "Select * from "+TABLE_MEMBER; SQLiteDatabase db = this.getReadableDatabase(); Cursor cursor = db.rawQuery(selectQuery, null); return cursor;}now do,Cursor cursor = getAllData(); //cursor hold all your dataJSONObject jobj ;JSONArray arr = new JSONArray();cursor.moveToFIrst();while(cursor.moveToNext()) { jobj = new JSONObject(); jboj.put("Id", cursor.getInt("Id")); jboj.put("Name", cursor.getString("Name")); arr.put(jobj);}jobj = new JSONObject();jobj.put("data", arr);String st = jboj.toString();now simply make an http call with string parameter and send to server.and parse at server by converting this string into jsonobject.now according to your code, doList<NameValuePair> params = new ArrayList<NameValuePair>();params.add(new BasicNameValuePair("allData", st));String resultServer = getHttpPost(url,params); 这篇关于如何发送的所有数据的SQLite在线服务器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云! 09-02 20:03