我按照教程创建了一个SQLite数据库,并在其中为应用程序创建了表。但所有这些都发生在运行时。现在我正在寻找一种创建数据库和表的方法,以便将来可以重用它们。因此,相同的表格将使用插入应用程序的数据进行更新。 数据库和表格只会创建一次。
这是我用来在运行时创建数据库和表格的代码:
SQLiteHelperCourse courseDbHelper = new SQLiteHelperCourse(this);
SQLiteDatabase dbw = courseDbHelper.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(CourseEntry.COLUMN_NAME_COURSE_ID, "1");
values.put(CourseEntry.COLUMN_NAME_COURSE_NAME, "Android");
values.put(CourseEntry.COLUMN_NAME_CREDITS, "3");
values.put(CourseEntry.COLUMN_NAME_SEMESTER, "1st");
values.put(CourseEntry.COLUMN_NAME_YEAR, "3rd");
values.put(CourseEntry.COLUMN_NAME_COURSE_ID, "2");
values.put(CourseEntry.COLUMN_NAME_COURSE_NAME, "SBP");
values.put(CourseEntry.COLUMN_NAME_CREDITS, "3");
values.put(CourseEntry.COLUMN_NAME_SEMESTER, "1st");
values.put(CourseEntry.COLUMN_NAME_YEAR, "3rd");
values.put(CourseEntry.COLUMN_NAME_COURSE_ID, "3");
values.put(CourseEntry.COLUMN_NAME_COURSE_NAME, "IT Infrastructure");
values.put(CourseEntry.COLUMN_NAME_CREDITS, "3");
values.put(CourseEntry.COLUMN_NAME_SEMESTER, "1st");
values.put(CourseEntry.COLUMN_NAME_YEAR, "3rd");
dbw.insert(CourseEntry.TABLE_NAME, CourseEntry.COLUMN_NAME_NULLABLE, values);
SQLiteDatabase dbr = courseDbHelper.getReadableDatabase();
String[] projection = { CourseEntry.COLUMN_NAME_COURSE_NAME};
final ArrayList<String> list = new ArrayList<String>();
Cursor curs = dbr.query(CourseEntry.TABLE_NAME,
projection,
null,
null,
null,
null,
null);
curs.moveToFirst();
while(curs.moveToNext())
{
list.add(curs.getString(curs.getColumnIndexOrThrow(CourseEntry.COLUMN_NAME_COURSE_NAME)));
}
修改 我提供的代码是在活动的onCreate事件中调用的。
这是我的SQLiteHelper类代码:
public class SQLiteHelperCourse extends SQLiteOpenHelper {
public static final int DATABASE_VERSION = 1;
public static final String DATABASE_NAME = "AndroidApplication.db";
public SQLiteHelperCourse(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
private static final String TEXT_TYPE = " TEXT";
private static final String COMMA_SEP = ", ";
private static final String SQL_CREATE_ENTRIES =
"CREATE TABLE " + CourseEntry.TABLE_NAME + " (" + CourseEntry._ID + " INTEGER PRIMARY KEY, "
+ CourseEntry.COLUMN_NAME_COURSE_ID + TEXT_TYPE + COMMA_SEP
+ CourseEntry.COLUMN_NAME_COURSE_NAME + TEXT_TYPE + COMMA_SEP
+ CourseEntry.COLUMN_NAME_YEAR + TEXT_TYPE + COMMA_SEP
+ CourseEntry.COLUMN_NAME_SEMESTER + TEXT_TYPE + COMMA_SEP
+ CourseEntry.COLUMN_NAME_CREDITS + TEXT_TYPE + ")";
private static final String SQL_DELETE_ENTRIES = "DROP TABLE IF EXISTS " + CourseEntry.TABLE_NAME;
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(SQL_CREATE_ENTRIES);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL(SQL_DELETE_ENTRIES);
onCreate(db);
}
public void onDowngrade(SQLiteDatabase db, int oldVersion, int newVersion) {
onUpgrade(db, oldVersion, newVersion);
}
}
答案 0 :(得分:1)
假设您只想在创建数据库时将三个课程插入数据库:
将插入代码移动到数据库帮助程序onCreate()
。当数据库文件不存在时,它会被调用一次。使用提供的SQLiteDatabase
作为参数,而不是递归调用getWritableDatabase()
。
插入三次。 ContentValues
put()
使用相同的密钥覆盖任何值,因此基本上您只是插入第三个课程。