意图:从SQLite表中检索单个数据并在TextView中显示
案例:无法检索数据,也无法显示主要活动。
预期结果: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;
} }
答案 0 :(得分: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);