我有一个简单的游戏,用户可以猜单词。现在,我正在考虑使用数据库来存储这些待猜测的单词。
我的问题是,网上的教程展示了如何创建数据库并将用户输入保存到该数据库。例如,他们在src中创建一个DBHelper.java,将其扩展到SQLiteOpenHelper,覆盖这些方法。回到一个特定的活动,创建一个DBHelper实例,然后创建数据库,打开可写的,插入用户输入,关闭数据库。
但我认为我只需要创建一个数据库,在其中插入单词,然后让我的应用程序从这个数据库中检索单词。
我只是想知道我的计划是否正确:1.在src中创建一个DBHelper.java,将类扩展到SQLiteOpenHelper2.定义所需的字符串,如数据库名称等。3.创建构造函数并重写onCreate和onUpgrade方法4.创建LOADWORDS方法这是我将单词插入数据库的地方。5.在我的主要活动(应用程序的第一个屏幕(上,我将创建DBHelper的一个实例,并调用onCreate和loadWords方法。
// you would want an onCreate and onUpgrade method for best practices,, here's a partial look of what you want...
public class DBManager extends SQLiteOpenHelper
{
static final String TAG = "DBManager";
static final String DB_NAME = "words.db";
static final int DB_VERSION = 1;
static final String TABLE = "words_table";
static final String C_ID = "id";
static final String C_WORD = "word";
public DBManager(Context context)
{
super(context, DB_NAME, null, DB_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db)
{
String sql = "CREATE TABLE " + TABLE + " ("
+ C_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, "
+ C_WORD + " TEXT)";
db.execSQL(sql);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
{
db.execSQL("DROP TABLE IF EXISTS " + TABLE);
onCreate(db);
}
//**** Code Insert Word and Retrieve Word Methods *****//
//**** End Code Insert Word and Retrieve Word Methods *****//
}