这是我的 SQLBlog 类,getDataFunc 是我从数据库获取结果的函数
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;
public class SQLBlog {
public static final String KEY_ROWID = "_id";
public static final String KEY_NAME = "persons_name";
public static final String KEY_BLOG = "persons_blog";
private static final String DATABASE_NAME = "blog";
private static final String DATABASE_TABLE = "admin_blog";
private static final int DATABASE_VERSION = 1;
private DBhelper ourhelper;
private final Context ourcontext;
private SQLiteDatabase ourdatabase;
private static class DBhelper extends SQLiteOpenHelper{
public DBhelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
// TODO Auto-generated constructor stub
}
@Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
db.execSQL("Create table "+ DATABASE_TABLE +" (" + KEY_ROWID + " Integer primary key autoincrement, "+
KEY_NAME +" text not null, "+
KEY_BLOG +" text not null);");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
db.execSQL("drop table if exists "+ DATABASE_TABLE);
onCreate(db);
}
}
public SQLBlog(Context c){
ourcontext=c;
}
public SQLBlog open() throws SQLException{
ourhelper= new DBhelper(ourcontext);
ourdatabase= ourhelper.getWritableDatabase();
return this;
}
public void close(){
ourhelper.close();
}
public long createEntry(String name, String blog) {
// TODO Auto-generated method stub
ContentValues cv = new ContentValues();
cv.put(KEY_NAME, name);
cv.put(KEY_BLOG, blog);
return ourdatabase.insert(DATABASE_TABLE, null, cv);
}
public String getDataFunc() {
// TODO Auto-generated method stub
String columns[]= new String[]{KEY_ROWID, KEY_NAME, KEY_BLOG};
Cursor c = ourdatabase.query(DATABASE_TABLE, columns, null, null, null, null, null);
String result= "";
int iRow = c.getColumnIndex(KEY_ROWID);
int iName = c.getColumnIndex(KEY_NAME);
int iBlog = c.getColumnIndex(KEY_BLOG);
for(c.moveToFirst(); !c.isAfterLast(); c.moveToNext()){
result = result + c.getString(iRow) + " " + c.getString(iName) + " " + c.getString(iBlog) + "\n";
}
return result;
}
}
在这里,我使用该结果向我的用户显示博客
public class SQLLiteView extends Activity{
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.sqlview);
TextView tv =(TextView)findViewById(R.id.tvSQLinfo);
SQLBlog getdata = new SQLBlog(this);
getdata.open();
String data = getdata.getDataFunc();
getdata.close();
tv.setText(data);
}
}
但问题是我将数据作为字符串获取,但我想更动态地显示数据,例如......
嘿,伙计们,你好吗:由杰克发布
//这里会给出评论...
如何从我的数据库中获取数据并将其放在可点击的 ListView 上:Nicole 发布
//评论放在这里
我怎样才能做到这一点..需要帮助...我想我正在寻找某种类型的 foreach 循环,如 php ......它可以破坏数据..但是因为我使用 String 作为返回类型,所以我无法得到它..