如何检索在SqliteBrowser的android创建数据库的数据库中的数据 [英] How to retrieve data from database in android Creating database on SqliteBrowser
问题描述
我的 jokes.db 数据库。该数据库文件使用此文章进口的http://www.reigndesign.com/blog/using-your-own-sqlite-database-in-android-applications/.现在我有 Jokes.db 到 /data/data/a.b.c/database/Jokes.db 。在这个数据库中,我有两个表的 NepaliJokes 和 EnglishJokes 。所以现在我想以检索这个尼泊尔语和英语的笑话,并TextView的显示,但是我已经做了以下code,但couldnot背后如何从数据库中检索数据的更多想法。
I have jokes.db database. This database file imported from using this article., http://www.reigndesign.com/blog/using-your-own-sqlite-database-in-android-applications/. Now i have Jokes.db in to /data/data/a.b.c/database/Jokes.db. In this database , I have two table NepaliJokes and EnglishJokes. So Now I want to retrive this nepali and english jokes and display in to textview, But I have done following code but couldnot more idea behind How to retrieve data from database.
public class FunnyJokes extends Activity implements View.OnClickListener {
private SQLiteDatabase database;
@Override
protected void onCreate(Bundle savedInstanceState) {
Button back;
super.onCreate(savedInstanceState);
setContentView(R.layout.displayjoks1);
back = (Button) findViewById(R.id.back_btn);
back.setOnClickListener(this);
DataBaseHelper helper = new DataBaseHelper(this);
database = helper.getWritableDatabase();
loadJokes();
}
private void loadJokes() {
//jok=new ArrayList<String>();
/*Cursor c = database.query("SELECT title,body" +
" FROM " + 'tableName'
+ " WHERE category=1;",
null);*/
Cursor c = database.query("NepaliJokes",
null, null, null, null, null, null);
c.moveToPosition(0);
TextView tv= (TextView) findViewById(R.id.textView1);
tv.append(c.getString(1));
c.close();
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.back_btn:
FunnyJokes.this.finish();
break;
default:
break;
}
}
}
我也使用该数据库文件导入从DDMS-> fileExploer。我导入数据库文件从拉左文件浏览器部分的移动图标。
I also use the database file import from in DDMS->fileExploer. I import database file Pull mobile icon from left of file explorer section.
推荐答案
要读取表中的值:
-
创建一个游标从数据库中读取数据。
Create a cursor to read data from the database.
写了下面的函数。
public Cursor retrieveRecords(int category)
{
Cursor c = null;
c = db.rawQuery("select title,body from tablename where category=" +category, null);
return c;
}
现在从光标获取值。
Now get the values from the cursor.
public void getDataFromDatabase()
{
try
{
Cursor cursor = null;
db.OpenDatabase();
cursor = db.retrieveRecords();
if (cursor.getCount() != 0)
{
if (cursor.moveToFirst())
{
do
{
titleArrayList.add(cursor.getString(cursor.getColumnIndex("title")));
bodyArrayList.add(cursor.getString(cursor.getColumnIndex("body")));
} while (cursor.moveToNext());
}
db.closeDatabase();
}
cursor.close();
}
catch(Exception e)
{
e.printStackTrace();
}
}
这篇关于如何检索在SqliteBrowser的android创建数据库的数据库中的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!