i am now working on a little project on Android Studio. Here i want to insert some data to SQLite Database since the first the app run. Because, the app need the data to run (this is a Quiz app) and the question is stored in a CSV file. I have doing some stuff like this
public class DatabaseHelper extends SQLiteOpenHelper {
public static final String MyVillageSoftware = "MyVillageSoftware";
public static final String DATABASE_NAME = "Question.db";
public static final String table_Question = "table_question";
public static final String COL1 = "QuestId";
public static final String COL2 = "A";
public static final String COL3 = "B";
public static final String COL4 = "C";
public static final String COL5 = "D";
public static final String COL6 = "aExplain";
public static final String COL7 = "bExplain";
public static final String COL8 = "cExplain";
public static final String COL9 = "dExplain";
public static final String COL10 = "_id";
//TOL for transaction Coloumn
SQLiteDatabase db;
private static final String TAG = DatabaseHelper.class.getSimpleName();
public DatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, 1);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("Create table " + table_Question +
" (QuestId Integer PRIMARY KEY, " +
"A Text," +
"B Text," +
"C Text" +
"D Text," +
"aExplain Text," +
"bExplain Text," +
"cExplain Text," +
"dExplain Text," +
"_id Text)");
}
public void loadCSV2(){
String mCSVfile = "file.csv";
Context context;
AssetManager manager = context.getAssets();
InputStream inStream = null;
try {
inStream = manager.open(mCSVfile);
} catch (IOException e) {
e.printStackTrace();
}
BufferedReader buffer = new BufferedReader(new InputStreamReader(inStream));
String line = "";
db.beginTransaction();
try {
while ((line = buffer.readLine()) != null) {
String[] colums = line.split(",");
if (colums.length != 4) {
Log.d("CSVParser", "Skipping Bad CSV Row");
continue;
}
ContentValues cv = new ContentValues(3);
cv.put(COL1, colums[0].trim());
cv.put(COL2, colums[1].trim());
cv.put(COL3, colums[2].trim());
cv.put(COL4, colums[3].trim());
cv.put(COL5, colums[4].trim());
cv.put(COL6, colums[5].trim());
cv.put(COL7, colums[6].trim());
cv.put(COL8, colums[7].trim());
cv.put(COL9, colums[8].trim());
db.insert(table_Question, null, cv);
}
} catch (IOException e) {
e.printStackTrace();
}
db.setTransactionSuccessful();
db.endTransaction();
}
public List<String> getAllCategory() {
List<String> AllCategoryList = new ArrayList<String>();
List<String> AllCategIdList = new ArrayList<String>();
String selectQuery = "SELECT * FROM " + table_Question;
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.rawQuery(selectQuery, null);
if (cursor.moveToFirst()) {
do {
String Id = cursor.getString(0);
String A = cursor.getString(1);
String B = cursor.getString(2);
String C = cursor.getString(3);
String D = cursor.getString(4);
String aExplain = cursor.getString(5);
String bExplain = cursor.getString(6);
String cExplain = cursor.getString(7);
String dExplain = cursor.getString(8);
} while (cursor.moveToNext());
}
cursor.close();
db.close();
return AllCategoryList;
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS " + table_Question);
onCreate(db);
}
}
but i got error on this line :
AssetManager manager = context.getAssets();
it said
Error:(67, 32) error: variable context might not have been initialized
I don't know what happen, please master help me. I am so newbie on android programming.
Thanks Before.
Aucun commentaire:
Enregistrer un commentaire