-2
這是我的數據庫處理類 `的Android的SQLite如何輸出數據庫的所有字段到ListView
public class myDBHandler extends SQLiteOpenHelper {
private static final int DATABASE_VERSION = 1;
private static final String DATABASE_NAME = "todoDB.db";
public static final String TABLE_TODO = "todo";
public static final String COLUMN_ID = "_id";
public static final String COLUMN_TODONAME = "todoName";
public static final String COLUMN_DESCRIPTION = "description";
public myDBHandler(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) {
super(context, DATABASE_NAME, factory, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
String query = "CREATE TABLE " + TABLE_TODO + "(" +
COLUMN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
COLUMN_TODONAME + " TEXT " +
COLUMN_DESCRIPTION + "TEXT" +
");";
db.execSQL(query);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS " + TABLE_TODO);
onCreate(db);
}
//Add a new row to the database
public void addTODO(TODO todo){
ContentValues values = new ContentValues();
values.put(COLUMN_TODONAME, todo.get_todoname());
values.put(COLUMN_DESCRIPTION,todo.get_description());
SQLiteDatabase db = getWritableDatabase();
db.insert(TABLE_TODO, null, values);
db.close();
}
//Delete a TO DO from the database
public void deleteTODO(String TODOName){
SQLiteDatabase db = getWritableDatabase();
db.execSQL("DELETE FROM " + TABLE_TODO + " WHERE " + COLUMN_TODONAME + "=\"" + TODOName + "\";");
}
public void editTODO(String TODOName, String newDescription){
SQLiteDatabase db = getWritableDatabase();
String queryUpdate = "UPDATE" + TABLE_TODO + "SET" + COLUMN_DESCRIPTION + "=\"" + newDescription + "WHERE" + COLUMN_TODONAME + "=\"" + TODOName + "\";";
db.execSQL(queryUpdate);
}
public String dbToString(){
String dbString = "";
SQLiteDatabase db = getWritableDatabase();
String query = "SELECT * FROM " + TABLE_TODO + " WHERE 1";
//Cursor points to a location in your results
Cursor c = db.rawQuery(query, null);
//Move to the first row in your results
c.moveToFirst();
//Position after the last row means the end of the results
while (!c.isAfterLast()) {
if (c.getString(c.getColumnIndex("todoName")) != null) {
dbString += c.getString(c.getColumnIndex("todoName"));
dbString += c.getString(c.getColumnIndex("description"));
dbString += "\n";
}
c.moveToNext();
}
db.close();
return dbString;
}
}
我怎麼能輸出的列todoName &描述列表視圖在一個單獨的活動,從而使select *語句顯示這些列。代碼在這裏:
'WHERE 1'是一樣的不把任何WHERE子句(這意味着**總是如此**)。因此,這完全沒有錯。但它很容易* SQL注入*。因此,這實際上不是可取的。 –
@Rotwang好吧,先生 –