实现从数据库sqlite中抽取问题显示的代码
时间: 2024-03-25 18:37:26 浏览: 23
以下是从SQLite数据库中获取问题并在Android应用程序中显示的基本代码:
首先,创建一个SQLiteOpenHelper子类,用于管理数据库:
```java
public class DatabaseHelper extends SQLiteOpenHelper {
private static final String DATABASE_NAME = "quiz.db";
private static final int DATABASE_VERSION = 1;
private static final String TABLE_NAME = "questions";
private static final String COLUMN_ID = "_id";
private static final String COLUMN_QUESTION = "question";
private static final String COLUMN_ANSWER = "answer";
public DatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
String query = "CREATE TABLE " + TABLE_NAME + " (" +
COLUMN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
COLUMN_QUESTION + " TEXT, " +
COLUMN_ANSWER + " TEXT" +
")";
db.execSQL(query);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
onCreate(db);
}
public Cursor getAllQuestions() {
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.rawQuery("SELECT * FROM " + TABLE_NAME, null);
return cursor;
}
}
```
接下来,在你的Activity中,调用getAllQuestions()方法来获取问题:
```java
public class QuizActivity extends AppCompatActivity {
private ListView listView;
private ArrayList<String> questionsList;
private ArrayAdapter<String> adapter;
private DatabaseHelper dbHelper;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_quiz);
listView = findViewById(R.id.list_view);
questionsList = new ArrayList<>();
adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, questionsList);
listView.setAdapter(adapter);
dbHelper = new DatabaseHelper(this);
Cursor cursor = dbHelper.getAllQuestions();
if (cursor.moveToFirst()) {
do {
String question = cursor.getString(cursor.getColumnIndex("question"));
questionsList.add(question);
} while (cursor.moveToNext());
}
adapter.notifyDataSetChanged();
}
@Override
protected void onDestroy() {
super.onDestroy();
if (dbHelper != null) {
dbHelper.close();
}
}
}
```
在这个例子中,我们将问题存储在一个ListView中,但你可以根据你的需求进行更改。注意在Activity生命周期结束时关闭数据库连接。
相关推荐
![application/x-zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![.zip](https://img-home.csdnimg.cn/images/20210720083646.png)
![application/x-zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)