1

意图:从 SQLite 表中检索单个数据并在 TextView 中显示

案例:获取数据失败,无法在mainactivity中显示。

预期结果: TextView 在主要活动中显示当前总银行余额(例如总余额:$7600)

注意请在检索显示数据时向我展示 mainactiviy 的代码。在此先感谢(并为我的英语不好感到抱歉)。

package com.fa.money;
import java.util.ArrayList;
import java.util.HashMap;
import android.util.Log;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class DBController  extends SQLiteOpenHelper {
private static final String LOGCAT = null;

public DBController(Context applicationcontext) {
    super(applicationcontext, "androidsqlite.db", null, 1);
    Log.d(LOGCAT,"Created");
}

@Override
public void onCreate(SQLiteDatabase database) {
    String query, query2;
//  query = "CREATE TABLE tbBank ( bankID INTEGER PRIMARY KEY, bankname TEXT)";
    query = "CREATE TABLE tbBank ( bankID INTEGER PRIMARY KEY AUTOINCREMENT, bankname TEXT, baccID TEXT, baccBal TEXT, remarks TEXT)";
    query2 = "CREATE TABLE tbMain (mainID INTEGER PRIMARY KEY AUTOINCREMENT, bankbal TEXT, loanbal TEXT)";
    database.execSQL(query);
   database.execSQL(query2);

    Log.d(LOGCAT,"Bank Account Created");
}
@Override
public void onUpgrade(SQLiteDatabase database, int version_old, int current_version) {
    String query,query2;
    query = "DROP TABLE IF EXISTS tbBank";
    query2 = "DROP TABLE IF EXISTS tbMain";
    database.execSQL(query);
    database.execSQL(query2);
    onCreate(database);
}

public void addBank(HashMap<String, String> queryValues) {
    SQLiteDatabase database = this.getWritableDatabase();
    ContentValues values = new ContentValues();
    values.put("bankname", queryValues.get("bankname"));
    values.put("baccID", queryValues.get("bankaccid"));
    values.put("baccBal", queryValues.get("totalbal"));
    values.put("remarks", queryValues.get("remarks"));
    database.insert("tbBank", null, values);
    database.close();
}

public int updateBank(HashMap<String, String> queryValues) {
    SQLiteDatabase database = this.getWritableDatabase();    
    ContentValues values = new ContentValues();
    values.put("bankname", queryValues.get("bankname"));
    return database.update("tbBank", values, "bankID" + " = ?", new String[] { queryValues.get("bankID") });
    //String updateQuery = "Update  words set txtWord='"+word+"' where txtWord='"+ oldWord +"'";
    //Log.d(LOGCAT,updateQuery);
    //database.rawQuery(updateQuery, null);
    //return database.update("words", values, "txtWord  = ?", new String[] { word });
}

public void deleteBank(String id) {
    Log.d(LOGCAT,"delete");
    SQLiteDatabase database = this.getWritableDatabase();    
    String deleteQuery = "DELETE FROM  tbBank where bankID='"+ id +"'";
    Log.d("query",deleteQuery);     
    database.execSQL(deleteQuery);
}

public ArrayList<HashMap<String, String>> getAllbank() {
    ArrayList<HashMap<String, String>> wordList;
    wordList = new ArrayList<HashMap<String, String>>();
    String selectQuery = "SELECT * FROM tbBank";
    SQLiteDatabase database = this.getWritableDatabase();
    Cursor cursor = database.rawQuery(selectQuery, null);
    if (cursor.moveToFirst()) {
        do {
            HashMap<String, String> map = new HashMap<String, String>();
            map.put("bankID", cursor.getString(0));
            map.put("bankname", cursor.getString(1));
            wordList.add(map);
        } while (cursor.moveToNext());
    }


    return wordList;
}

public HashMap<String, String> getBankinfo(String id) {
    HashMap<String, String> wordList = new HashMap<String, String>();
    SQLiteDatabase database = this.getReadableDatabase();
    String selectQuery = "SELECT * FROM tbBank where bankID='"+id+"'";
    Cursor cursor = database.rawQuery(selectQuery, null);
    if (cursor.moveToFirst()) {
        do {
                //HashMap<String, String> map = new HashMap<String, String>();
            wordList.put("bankname", cursor.getString(1));
               //wordList.add(map);
        } while (cursor.moveToNext());
    }                   
return wordList;
}
//getbankbal - ERROR here !
public String getBankbal() {
      String bankbalresult;
      SQLiteDatabase database = this.getReadableDatabase();
      String selectQuery = "SELECT bankbal FROM tbMain";
      Cursor cursor = database.rawQuery(selectQuery, null);
      if (cursor.moveToFirst()) {
        do {
            bankbalresult.put("bankbal", cursor.getString(1));
        } while (cursor.moveToNext());
      }           
      return bankbalresult;
    } }
4

1 回答 1

11

我认为您应该尝试将 bankbal 声明为 double 而不是 text 。无论如何,如果您希望它是文本尝试这样做

Cursor cursor = database.rawQuery(selectQuery, null);
if(cursor!=null && cursor.getCount()>0)
{
      cursor.moveToFirst()
        do {
            bankbalresult = cursor.getString(0);
        } while (cursor.moveToNext());
      }           
}
      return bankbalresult;
    } }

// 在你希望它显示的活动中你可以做

 TextView tv = new TextView();
    DBController info = new DBController(this);
    info.open();
    String data = info.getBankBal();
    info.close();
    tv.setText(data);
于 2013-07-27T15:39:39.683 回答