ANSWER SQLite的單細胞和顯示:請注意,我使用這兩種建議的組合以產生以下的代碼:抓取在單一的TextView
DBAdapter.java:
String name = "";
public String getName(long l) {
Cursor result = myDBHelper.getReadableDatabase().query(
DBAdapter.DATABASE_TABLE_NAME, //Name of your table
new String[] { DBAdapter.KEY_NAME }, //List of columns to return, as a String array. Just one in your case.
DBAdapter.KEY_ROWID + "=?", //The row selection criteria. Equivalent to saying "Where the key id is equal to..."
new String[] { String.valueOf(l) }, //...this value
null, //This parameter deals with grouping results. No need here, hence null.
null, //Relates to the above. Also null.
null //Orders results. There should just be one, so it's null here, but can be useful.
);
if (result.moveToFirst()) {
name = result.getString(result.getColumnIndex(DBAdapter.KEY_NAME));
}
return name;
CharacterInfo.java(NavigationDrawer親本):
DBAdapter myDb = new DBAdapter(this);
myDb.open();
String name = myDb.getName(1);
mTitle1 = name;
myDb.close();
雖然這產生了錯誤的環境中,應用程序崩潰由於"No such table"
錯誤(code 1)
這我還沒有解決。
END ANSWER
首先,我知道有很多這些類型的問題。我已經看了至少一打,似乎無法把頭圍住它,所以我發佈了一個問題,希望有人能幫我一個忙,並在提供方法的同時更詳細地向我解釋。
我想創建一個方法,爲我指定的KEY_ROWID
提取KEY_NAME
。 (我認爲這可以使用已經在DBAdapter
中的getRow()
方法來完成)。然後,將其轉換爲一個字符串以顯示它TextView
。
我需要知道如何在DBAdapter
命名getName
底部填寫方法一個完美的答案,讓我寫了下面的代碼在一個單獨的活動設置標題:mTitle1 = getName;
謝謝你非常重視你的時間。
我有一個非常好的DBAdapter
通過一個教程,我也跟着提供:
public class DBAdapter {
private static final String TAG = "DBAdapter"; //used for logging database version changes
// Field Names:
public static final String KEY_ROWID = "_id";
public static final String KEY_NAME = "name";
public static final String KEY_DATE = "date";
public static final String[] ALL_KEYS = new String[] {KEY_ROWID, KEY_NAME, KEY_DATE};
// Column Numbers for each Field Name:
public static final int COL_ROWID = 0;
public static final int COL_NAME = 1;
public static final int COL_DATE = 2;
// DataBase info:
public static final String DATABASE_NAME = "dbCharacters";
public static final String DATABASE_TABLE = "Character_Info";
// The version number must be incremented each time a change to DB structure occurs.
public static final int DATABASE_VERSION = 2;
//SQL statement to create database
private static final String DATABASE_CREATE_SQL =
"CREATE TABLE " + DATABASE_TABLE
+ " (" + KEY_ROWID + " INTEGER PRIMARY KEY AUTOINCREMENT, "
+ KEY_NAME + " TEXT NOT NULL, "
+ KEY_DATE + " TEXT"
+ ");";
private final Context context;
private DatabaseHelper myDBHelper;
private SQLiteDatabase db;
public DBAdapter(Context ctx) {
this.context = ctx;
myDBHelper = new DatabaseHelper(context);
}
// Open the database connection.
public DBAdapter open() {
db = myDBHelper.getWritableDatabase();
return this;
}
// Close the database connection.
public void close() {
myDBHelper.close();
}
// Add a new set of values to be inserted into the database.
public long insertRow(String name, String date) {
ContentValues initialValues = new ContentValues();
initialValues.put(KEY_NAME, name);
initialValues.put(KEY_DATE, date);
// Insert the data into the database.
return db.insert(DATABASE_TABLE, null, initialValues);
}
// Delete a row from the database, by rowId (primary key)
public boolean deleteRow(long rowId) {
String where = KEY_ROWID + "=" + rowId;
return db.delete(DATABASE_TABLE, where, null) != 0;
}
public void deleteAll() {
Cursor c = getAllRows();
long rowId = c.getColumnIndexOrThrow(KEY_ROWID);
if (c.moveToFirst()) {
do {
deleteRow(c.getLong((int) rowId));
} while (c.moveToNext());
}
c.close();
}
// Return all data in the database.
public Cursor getAllRows() {
String where = null;
Cursor c = db.query(true, DATABASE_TABLE, ALL_KEYS, where, null, null, null, null, null);
if (c != null) {
c.moveToFirst();
}
return c;
}
// Get a specific row (by rowId)
public Cursor getRow(long rowId) {
String where = KEY_ROWID + "=" + rowId;
Cursor c = db.query(true, DATABASE_TABLE, ALL_KEYS,
where, null, null, null, null, null);
if (c != null) {
c.moveToFirst();
}
return c;
}
// Change an existing row to be equal to new data.
public boolean updateRow(long rowId, String name, String date) {
String where = KEY_ROWID + "=" + rowId;
ContentValues newValues = new ContentValues();
newValues.put(KEY_NAME, name);
newValues.put(KEY_DATE, date);
// Insert it into the database.
return db.update(DATABASE_TABLE, newValues, where, null) != 0;
}
private static class DatabaseHelper extends SQLiteOpenHelper
{
DatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase _db) {
_db.execSQL(DATABASE_CREATE_SQL);
}
@Override
public void onUpgrade(SQLiteDatabase _db, int oldVersion, int newVersion) {
Log.w(TAG, "Upgrading application's database from version " + oldVersion
+ " to " + newVersion + ", which will destroy all old data!");
// Destroy old database:
_db.execSQL("DROP TABLE IF EXISTS " + DATABASE_TABLE);
// Recreate new database:
onCreate(_db);
}
}
public void getName (long id) {
}
}
這裏的PPartisan的建議:
public String getName(long l) {
Cursor result = myDBHelper.getReadableDatabase().query(
DBAdapter.DATABASE_NAME, //Name of your database
new String[] { DBAdapter.KEY_NAME }, //List of columns to return, as a String array. Just one in your case.
DBAdapter.KEY_ROWID + "=?", //The row selection criteria. Equivalent to saying "Where the key id is equal to..."
new String[] { String.valueOf(1) }, //...this value
null, //This parameter deals with grouping results. No need here, hence null.
null, //Relates to the above. Also null.
null //Orders results. There should just be one, so it's null here, but can be useful.
);
if (result.moveToFirst()) {
name = result.getString(result.getColumnIndex(DBAdapter.KEY_NAME));
}
return name;
Zahan的建議:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.character_info);
mCharacterInfoDrawerFragment = (CharacterInfo_Drawer_Fragment)
getSupportFragmentManager().findFragmentById(R.id.navigation_drawer);
openDB();
String name = DBAdapter.getName(1);
mTitle1 = name;
closeDB();
mCharacterInfoDrawerFragment.setUp(
R.id.navigation_drawer,
(DrawerLayout) findViewById(R.id.drawer_layout));
}
嘿,在'if語句'中出現''無法解析'KEY_NAME''符號的錯誤。我將附加的代碼發佈到OP中。 – Mikitz06
@ Mikitz06噢,對不起,那應該是'DBAdapter.KEY_NAME' - 我現在就改變它。 – PPartisan
結果在''不能解析器符號'DBAdapter'「'另外,它說有一個'返回語句'丟失。它應該是「返回名稱」嗎? – Mikitz06