将数据库与整数进行比较

时间:2011-07-01 08:08:59

标签: android database sqlite android-sqlite

我想获取位于外部存储中的数据库中的id列中的数据,并将其与来自互联网的数据库中的id列进行比较。从互联网下载数据库已经完成。我唯一的问题是我无法比较这两个值。我创建了从数据库中获取数据的代码:

try {
     int ct_id;
     String ct_name;
     try{
         HttpClient httpclient = new DefaultHttpClient();
         HttpPost httppost = new HttpPost("http://10.0.2.2/city.php");
         httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
         HttpResponse response = httpclient.execute(httppost);
         HttpEntity entity = response.getEntity();
         is = entity.getContent();
     }catch(Exception e){
         Log.e("log_tag", "Error in http connection"+e.toString());
     }
     //convert response to string
     try{
         BufferedReader reader = new BufferedReader(new InputStreamReader(is,"iso-8859-1"),8);
         sb = new StringBuilder();
         sb.append(reader.readLine() + "\n");
         String line="0";
         while ((line = reader.readLine()) != null) {
              sb.append(line + "\n");
         }
         is.close();
         result=sb.toString();
      }catch(Exception e){
            Log.e("log_tag", "Error converting result "+e.toString());
      }
      //paring data
      int max = 0;
      try{
          jArray = new JSONArray(result);
          JSONObject json_data=null;
          for(int i=0;i<jArray.length();i++){
                json_data = jArray.getJSONObject(i);
                ct_id=json_data.getInt("max(id)");
                ct_name=json_data.getString("date");
                max = ct_id;
          }
      }catch(JSONException e1){

      } catch (ParseException e1) {
            e1.printStackTrace();
      }
      URL url2 = new URL("http://10.0.2.2/xampp/uindictionaryse/images/kamus.db");
      final HttpURLConnection urlConnect = (HttpURLConnection) url2.openConnection();
      HttpURLConnection urlConnection2 = urlConnect;

      urlConnection2.setRequestMethod("GET");
      urlConnection2.setDoOutput(true);
      urlConnection2.connect();

      final File file = new File("/data/data/com.raj.dse/databases/dse");
      //and connect!
      int idex = 0;

      Cursor c = db.query("person", new String[] {"_id", "date"}, "max(id)", null, null, null, null);
      startManagingCursor(c);
      idex = c.getColumnIndex("_id");

      if(idex>=max){
            handler.post(new Runnable() {
                   @Override
                   public void run() {
                        state.setText("Database uptodate...");
                   }
            });
      }else{
          Builder builder = new Builder(null);
          builder.setTitle("Update"); // kalo ambil dari resources
          builder.setMessage("Your Database is out of date, do you want update?"); //disini yang ditambahkan
          //builder.setMessage("Exit Application?"); // ini yg di commentkan
          builder.setPositiveButton("Yes", new DialogInterface.OnClickListener({
                public void onClick(DialogInterface dialog, int which){
                    file.delete();
                     //this will be used to write the downloaded data into the file we created
                     FileOutputStream fileOutput = null;
                     try {
                          fileOutput = new FileOutputStream(file);
                      } catch (FileNotFoundException e) {
                            e.printStackTrace();
                      }
                      //this will be used in reading the data from the internet
                      InputStream inputStream = null;
                      try {
                           inputStream = urlConnect.getInputStream();
                      } catch (IOException e) {
                           e.printStackTrace();
                      }
                      //this is the total size of the file
                      int totalSize = urlConnect.getContentLength();
                      //variable to store total downloaded bytes
                      int downloadedSize = 0;
                      //create a buffer...
                      byte[] buffer = new byte[1024];
                      int bufferLength = 0; //used to store a temporary size of the buffer
                      //now, read through the input buffer and write the contents to the file
                      try {
                           while ((bufferLength = inputStream.read(buffer)) > 0 ) {
                                 //add the data in the buffer to the file in the file output stream (the file on the sd card
                                 fileOutput.write(buffer, 0, bufferLength);
                                 //add up the size so we know how much is downloaded
                                 downloadedSize += bufferLength;
                                 //this is where you would do something to report the prgress, like this maybe
                                 // updateProgress(downloadedSize, totalSize);
                            }
                       } catch (IOException e) {
                              e.printStackTrace();
                       }
                       handler.post(new Runnable() {
                             @Override
                             public void run() {
                                  state.setText("Database uptdated...");
                             }
                       });
                       try {
                            fileOutput.close();
                       } catch (IOException e) {
                            e.printStackTrace();
                       }
                  });
                  builder.setNegativeButton("No", null);
                  builder.create().show();
              }
        } catch (MalformedURLException e) {
             Toast.makeText(SplashScreen.this, "Error incorrect specification", Toast.LENGTH_SHORT).show();
             e.printStackTrace();
        } catch (IOException e) {
             Toast.makeText(SplashScreen.this, "Error occur", Toast.LENGTH_SHORT).show();
             e.printStackTrace();
        }

以下是LogCat中的输出:

07-01 10:27:07.275: ERROR/AndroidRuntime(1024): FATAL EXCEPTION: Thread-10 
07-01 10:27:07.275: ERROR/AndroidRuntime(1024): java.lang.NullPointerException 
07-01 10:27:07.275: ERROR/AndroidRuntime(1024): at com.raj.dse.SplashScreen$1.run(SplashScreen.java:177) 
07-01 10:27:07.275: ERROR/AndroidRuntime(1024): at java.lang.Thread.run(Thread.java:1019) 

根据LogCat,我在这里遇到了问题:

db.compileStatement(query);

我认为声明是正确的,我不明白问题出在哪里。关于如何解决这个问题的任何建议?

0 个答案:

没有答案