我想将我的SQLite文件中的数据加载到我的地图片段中,sqlite文件中有一个表商城。我得错误找不到表" mall"。 请帮忙。
我有文件SQLiteHelper.java
这是我的MapsFragment代码
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View v = inflater.inflate(R.layout.fragment_maps, container, false);
// membuat DB
dbHelper = new SQLHelper(this.getActivity());
try {
dbHelper.createDataBase();
}catch (Exception e){
Toast.makeText(getActivity(), "Gagal", Toast.LENGTH_SHORT).show();
}
// Query untuk menampilkan semua Mall
final SQLiteDatabase db = dbHelper.getReadableDatabase();
cursor = db.rawQuery("SELECT * FROM mall", null);
cursor.moveToFirst();
// tampung nama mall
ArrayList<String> spinner_list_mall = new ArrayList<String>();
// Adapter spinner
ArrayAdapter<String> adapter_spinner_mall;
// nama-nama mall dimasukkan ke dalam array
spinner_list_mall.add("-- Pilih Mall --");
for (int i=0; i < cursor.getCount(); i++){
cursor.moveToPosition(i);
spinner_list_mall.add(cursor.getString(1).toString());
}
// masukkan list Mall ke spinner dropdown
Spinner spinner = (Spinner) v.findViewById(R.id.list_hotel);
adapter_spinner_mall = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_spinner_item, spinner_list_mall);
adapter_spinner_mall.setDropDownViewResource(android.R.layout.simple_dropdown_item_1line);
spinner.setAdapter(adapter_spinner_mall);
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
if (parent.getItemAtPosition(position).toString() != "-- Pilih Mall --"){
String mall = (String) parent.getItemAtPosition(position);
Toast.makeText(getActivity(), mall, Toast.LENGTH_SHORT).show();
}
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
这是我的SQLite java文件。我想加载一个文件sqlite3。
SQLiteHelper.java
public class SQLHelper extends SQLiteOpenHelper{
private static final String DATABASE_NAME = "go-mall.sqlite3";
private static final int DATABASE_VERSION = 3;
private static String DB_PATH = "/data/data/com.go_mall.mm.go_mall/databases/";
private Context myContext;
public SQLHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
myContext = context;
}
public void createDataBase() throws IOException {
if(DataBaseisExist()){
//do nothing - database already exist
Toast.makeText(myContext, "Database Sudah Ada", Toast.LENGTH_LONG).show();
}
else{
//By calling this method and empty database will be created into the default system path
//of your application so we are gonna be able to overwrite that database with our database.
this.getReadableDatabase();
try {
copyDataBase();
Toast.makeText(myContext, "Database Berhasil Diimport Dari Assets", Toast.LENGTH_LONG).show();
} catch (IOException e) {
throw new Error("Error copying database");
}
}
}
private boolean DataBaseisExist(){
SQLiteDatabase checkDB = null;
try{
String myPath = DB_PATH + DATABASE_NAME;
checkDB = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY);
}catch(SQLiteException e){
//database does't exist yet.
}
if(checkDB != null){
checkDB.close();
}
if(checkDB != null )return true ;else return false;
}
private void copyDataBase() throws IOException{
//Open your local db as the input stream
InputStream myInput = myContext.getAssets().open(DATABASE_NAME);
// Path to the just created empty db
String outFileName = DB_PATH + DATABASE_NAME;
//Open the empty db as the output stream
OutputStream myOutput = new FileOutputStream(outFileName);
//transfer bytes from the inputfile to the outputfile
byte[] buffer = new byte[1024];
int length;
while ((length = myInput.read(buffer))>0){
myOutput.write(buffer, 0, length);
}
//Close the streams
myOutput.flush();
myOutput.close();
myInput.close();
}
@Override
public void onCreate(SQLiteDatabase db) {
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
}
请帮忙。