2013-07-08 39 views
0

我有一个SQLite数据库的网站数据(FTP地址,用户名,密码,端口,homedir,网址等)。我可以添加记录到表中,但似乎无法更新它们。SQLLite update()不起作用,logcat中没有错误。我如何诊断问题?

我创建了一个SiteManager活动,加载每一行并从每行创建一个WebSite对象。 WebSite的属性被加载到EditTexts中。该人员可以编辑属性,并且“更新”按钮应该更新表格行,但不是。 Logcat不会给出任何错误,所以我完全处于亏损状态,不知道从哪里开始。

public class SiteManager extends Activity { 
    private DBAdapter myDb; 

    private EditText siteManFTPAddress; 
    private EditText siteManFTPUsername; 
    private EditText siteManFTPPassword; 
    private EditText siteManFTPPort; 
    private EditText siteManURL; 
    private EditText siteManHome; 
    private ImageView favIcon; 
    public ListView site_list; 
    private Button openBtn; 
    private Button siteManUpdateBtn; 
    private int _rowId; 
    private String _name; 
    private String _remoteHomeDir; 
    private int _isLive; 
    private String _address; 
    private String _username; 
    private String _password; 
    private int _port; 
    private String _url; 
    private boolean _status = false; 
    private String siteFolder; 
    private List<WebSite> model = new ArrayList<WebSite>(); 
    private ArrayAdapter<WebSite> adapter; 



    public void onCreate(Bundle icicle) { 
     super.onCreate(icicle); 
     setContentView(R.layout.site_manager); 
     site_list = (ListView) findViewById(R.id.siteList); 
     adapter = new SiteAdapter(this, R.id.ftpsitename, R.layout.siterow, 
       model); 
     site_list.setAdapter(adapter); 

     addListeners(); 
     openDb(); 
     displayRecords(); 
    } 

    public void addListeners() { 
     siteManFTPAddress = (EditText) findViewById(R.id.siteManFTPAdd); 
     siteManFTPUsername = (EditText) findViewById(R.id.siteManFTPUser); 
     siteManFTPPassword = (EditText) findViewById(R.id.siteManFTPPass); 
     siteManFTPPort = (EditText) findViewById(R.id.siteManFTPPort); 
     siteManURL = (EditText) findViewById(R.id.siteManURL); 
     siteManHome = (EditText) findViewById(R.id.siteManHome); 

     site_list.setOnItemClickListener(new AdapterView.OnItemClickListener() { 

      @Override 
      public void onItemClick(AdapterView<?> parent, final View view, 
        int position, long id) { 
       File rootDir = new File(Environment 
         .getExternalStorageDirectory() + "/My Webs"); 
       final WebSite item = (WebSite) parent 
         .getItemAtPosition(position); 
       _name = item.getName(); 
       siteFolder = rootDir.toString() + "/" + _name; 
       _remoteHomeDir = item.getHomeDir(); 
       _isLive = item.isLive(); 

       String tmpaddress = item.getAddress(); 
       _address = tmpaddress; 
       siteManFTPAddress.setText(_address); 

       String tmpuser = item.getUsername(); 
       _username = tmpuser; 
       siteManFTPUsername.setText(_username); 

       String tmppass = item.getPassword(); 
       _password = tmppass; 
       siteManFTPPassword.setText(_password); 

       int tmpport = item.getPort(); 
       _port = tmpport; 
       String portString = Integer.toString(tmpport); 
       siteManFTPPort.setText(portString); 

       String tmpURL = item.getUrl(); 
       _url = tmpURL; 
       siteManURL.setText(_url); 

       String tmpHome = item.getHomeDir(); 
       _remoteHomeDir = tmpHome; 
       siteManURL.setText(_remoteHomeDir); 

      } 

     }); 
openBtn = (Button) findViewById(R.id.openSiteBtn); 
openBtn.setOnClickListener(new OnClickListener() { 

    @Override 
    public void onClick(View v) { 
     Intent returnResult = new Intent(); 

     returnResult.putExtra("siteopen", "siteopen"); 
     returnResult.putExtra("sitename", _name); 
     returnResult.putExtra("sitehome", siteFolder); 
     returnResult.putExtra("sitelive", _isLive); 
     returnResult.putExtra("siteremotehome", _remoteHomeDir); 
     returnResult.putExtra("siteaddress", _address); 
     returnResult.putExtra("siteusername", _username); 
     returnResult.putExtra("sitepassword", _password); 
     returnResult.putExtra("siteport", _port); 
     returnResult.putExtra("url", _url); 

     setResult(2, returnResult); 
     finish(); 

    } 
}); 
siteManUpdateBtn = (Button)findViewById(R.id.siteManFTPUpdate); 
siteManUpdateBtn.setOnClickListener(new OnClickListener() { 

    @Override 
    public void onClick(View v) { 
     _address = siteManFTPAddress.getText().toString(); 
     _username = siteManFTPUsername.getText().toString(); 
     _password = siteManFTPPassword.getText().toString(); 
     String port = siteManFTPPort.getText().toString(); 
     _port = Integer.parseInt(port); 
    Toast.makeText(SiteManager.this, "Update", Toast.LENGTH_LONG).show(); 

    myDb.updateRow(_rowId, _name, _name, _isLive, _address, _username, _password, _port, _url); 
    model.clear(); 
    adapter.notifyDataSetChanged(); 
    displayRecords(); 
    } 
}); 
    } 

    private void openDb() { 
     myDb = new DBAdapter(this); 
     myDb.open(); 
    } 

    @Override 
    protected void onDestroy() { 
     // TODO Auto-generated method stub 
     super.onDestroy(); 
     closeDb(); 
    } 

    private void closeDb() { 
     myDb.close(); 
    } 

    public void displayRecords() { 
     Cursor cursor = myDb.getAllRows(); 
     displayRecordSet(cursor); 
    } 

    protected void displayRecordSet(Cursor c) { 

     if (c.moveToFirst()) { 
      do { 
       int rowId = c.getInt(c.getColumnIndex(DBAdapter.KEY_ROWID)); 
       _rowId = c.getInt(rowId); 

       int keyNameIndex = c.getColumnIndex(DBAdapter.KEY_NAME); 
       _name = c.getString(keyNameIndex); 

       int keyHomeIndex = c.getColumnIndex(DBAdapter.KEY_HOME); 
       _remoteHomeDir = c.getString(keyHomeIndex); 

       int keyLiveIndex = c.getColumnIndex(DBAdapter.KEY_LIVE); 
       _isLive = c.getInt(keyLiveIndex); 

       int keyAddressIndex = c.getColumnIndex(DBAdapter.KEY_ADDRESS); 
       _address = c.getString(keyAddressIndex); 

       int keyUsernameIndex = c.getColumnIndex(DBAdapter.KEY_USERNAME); 
       _username = c.getString(keyUsernameIndex); 

       int keyPassIndex = c.getColumnIndex(DBAdapter.KEY_PASSWORD); 
       _password = c.getString(keyPassIndex); 

       int keyPortIndex = c.getColumnIndex(DBAdapter.KEY_PORT); 
       _port = c.getInt(keyPortIndex); 

       int keyUrlIndex = c.getColumnIndexOrThrow(DBAdapter.KEY_URL); 
       _url = c.getString(keyUrlIndex); 
       WebSite sitesFromDB = new WebSite(_rowId, _name, _remoteHomeDir, 
         _isLive, _address, _username, _password, _port, _url); 
       model.add(sitesFromDB); 
       adapter.notifyDataSetChanged(); 

       if(adapter.isEmpty()){ 

       } 
      } while (c.moveToNext()); 
     } 
     c.close(); 
    } 

    @Override 
    protected void onResume() { 
     // TODO Auto-generated method stub 
     super.onResume(); 
     adapter.notifyDataSetChanged(); 
    } 

    class SiteAdapter extends ArrayAdapter<WebSite> { 
     private final List<WebSite> objects; 
     private final Context context; 

     public SiteAdapter(Context context, int resource, 
       int textViewResourceId, List<WebSite> objects) { 
      super(context, R.id.sitename, R.layout.siterow, objects); 
      this.context = context; 
      this.objects = objects; 
     } 

     /** @return The number of items in the */ 
     public int getCount() { 
      return objects.size(); 
     } 

     public boolean areAllItemsSelectable() { 
      return false; 
     } 

     /** Use the array index as a unique id. */ 
     public long getItemId(int position) { 
      return position; 
     } 

     public View getView(int position, View convertView, ViewGroup parent) { 
      LayoutInflater inflater = (LayoutInflater) context 
        .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 

      View rowView = inflater.inflate(R.layout.siterow, parent, false); 
      TextView textView = (TextView) rowView.findViewById(R.id.sitename); 

      textView.setText(objects.get(position).getName()); 

      return (rowView); 
     } 

    } 

DBAdapter.java

public boolean updateRow(long rowId, String name, String homedir, 
      int islive, String address, String username, String password, 
      int port, String url) { 
     String where = KEY_ROWID + "=" + rowId; 

     /* 
     * CHANGE 4: 
     */ 
     // TODO: Update data in the row with new fields. 
     // TODO: Also change the function's arguments to be what you need! 
     // Create row's data: 
     ContentValues newValues = new ContentValues(); 
     newValues.put(KEY_NAME, name); 
     newValues.put(KEY_HOME, homedir); 
     newValues.put(KEY_LIVE, islive); 
     newValues.put(KEY_ADDRESS, address); 
     newValues.put(KEY_USERNAME, username); 
     newValues.put(KEY_PASSWORD, password); 
     newValues.put(KEY_PORT, port); 
     newValues.put(KEY_URL, url); 
     // newValues.put(KEY_PASSIVE, passive); 
     // Insert it into the database. 
     return db.update(DATABASE_TABLE, newValues, where, null) != 0; 
    } 

回答

1

价值_rowId只会被设定displayRecordSet方法,你通过从数据库中结果迭代内并设置_rowId

int rowId = c.getInt(c.getColumnIndex(DBAdapter.KEY_ROWID)); 
_rowId = c.getInt(rowId); 

这段代码似乎是相当随机的给我。首先您得到rowIdcolumnIndex,然后获得该特定行的索引,然后获得索引为rowId的列的值,然后从该值中设置_rowId字段。

我不知道SQLite数据库是否会如此令人讨厌,以至于如果在指定的列中没有任何值,只返回0,但这肯定会成为问题。

所以每次你得到的_rowId设定时间,它可能只是被设置为0,当您尝试更新行,其中rowId = 0什么也没有发生,在数据库中没有索引可以为0

official documentation about getInt(columnIndex)

+0

就是这样。我刚刚删除_rowId = c.getInt(rowId);并将rowId重命名为_rowId。这些实际上是新添加的,我从其他变量中复制/粘贴,没有考虑它在做什么。 – RapsFan1981

1

要诊断这样的问题,我通常会添加调试日志到应用程序。你可以在你的logcat中看到这些。 Log.d("tag", "there is something happening here: " + value);