您好我正在尝试创建一个显示来自sql文件的数据的ListView,我总是收到错误“错误复制数据库”或者我无法打开数据库
这是我的DBHelper
public class DBHelper extends SQLiteOpenHelper {
//The Android's default system path of your application database.
@SuppressLint("SdCardPath")
private static String DB_PATH = "/data/data/com.mobapp.dblist/databases/";
private static final String DB_NAME = "infodb";
public static final String TBL_GLOSSARY = "main_conference_items";
public static final String KEY_ID = "_id";
public static final String KEY_DAY_ID = "day_id";
public static final String KEY_TITLE = "title";
public static final String KEY_TIME = "time";
private SQLiteDatabase myDataBase;
private final Context myContext;
public DBHelper(Context context) {
super(context, DB_NAME, null, 1);
this.myContext = context;
}
public void createDataBase() throws IOException {
boolean dbExist = checkDataBase();
if(!dbExist)
{this.getReadableDatabase();
try {
copyDataBase();
} catch (IOException e) {
throw new Error("Error copying database");
}
}
}
SQLiteDatabase checkDB = null;
try{
String myPath = DB_PATH + DB_NAME;
checkDB = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY);
}catch(SQLiteException e){}
if(checkDB != null){
checkDB.close();
}
return checkDB != null ? true : false;
}
private void copyDataBase() throws IOException {
InputStream myInput = myContext.getAssets().open(DB_NAME);
String outFileName = DB_PATH + DB_NAME;
OutputStream myOutput = new FileOutputStream(outFileName);
byte[] buffer = new byte[1024];
int length;
while ((length = myInput.read(buffer))>0){
myOutput.write(buffer, 0, length);
}
myOutput.flush();
myOutput.close();
myInput.close();
}
public void openDataBase() throws SQLException {
String myPath = DB_PATH + DB_NAME;
myDataBase = SQLiteDatabase.openDatabase(myPath, null,
SQLiteDatabase.NO_LOCALIZED_COLLATORS | SQLiteDatabase.OPEN_READONLY);
}
@Override
public synchronized void close() {
if(myDataBase != null)
myDataBase.close();
super.close();
}
@Override
public void onCreate(SQLiteDatabase db) {
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
public Cursor getConferenceItems() {
Cursor localCursor =
this.myDataBase.query(TBL_GLOSSARY, new String[] {
KEY_ID,
KEY_DAY_ID,
KEY_TITLE,
KEY_TIME},
null,
null, null, null, null);
if (localCursor != null)
localCursor.moveToFirst();
return localCursor;
}
这是我的片段来显示我的列表视图
public class TabP extends Fragment {
DBHelper dbHelper;
private SimpleCursorAdapter dataAdapter;
public static final String TAG = "Tab_Programme";
// The desired columns to be bound
String[] columns = new String[] {
DataBaseHelper.KEY_ID,
DataBaseHelper.KEY_DAY_ID,
DataBaseHelper.KEY_TITLE,
DataBaseHelper.KEY_TIME
};
// the XML defined views which the data will be bound to
int[] to = new int[] {
R.id.row_id,
R.id.list_day,
R.id.title,
R.id.desc,
};
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View v =inflater.inflate(R.layout.tab_programme,container,false);
initialazeDatabase();
Cursor cursor = dbHelper.getConferenceItems();
dataAdapter = new SimpleCursorAdapter( getActivity(), R.layout.text_list_item_layout, cursor, columns, to, 0);
ListView listView = (ListView) v.findViewById(R.id.listView1);
// Assign adapter to ListView
listView.setAdapter(dataAdapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> listView, View view,
int position, long id) {
// Get the cursor, positioned to the corresponding row in the result set
Cursor cursor = (Cursor) listView.getItemAtPosition(position);
// Get the state's capital from this row in the database.
String countryCode =
cursor.getString(cursor.getColumnIndexOrThrow("title"));
Toast.makeText(getActivity().getApplicationContext(),
countryCode, Toast.LENGTH_SHORT).show();
}
});
return v;
}
public void initialazeDatabase()
{
dbHelper = new DBHelper(getActivity());
try {
dbHelper.createDataBase();
} catch (IOException ioe) {
throw new Error("Unable to create database");
}
try {
dbHelper.openDataBase();
} catch (SQLException sqle) {
throw sqle;
}
}
}
有人能帮帮我吗?我从昨天起就一直在尝试这个。非常感谢你。
答案 0 :(得分:0)
尝试更新此部分:
public DBHelper(Context context) {
super(context, DB_NAME, null, 1);
this.myContext = context;
}
到
public DBHelper(Context context) {
super(context, DB_NAME, null, 1);
this.myContext = context;
if(android.os.Build.VERSION.SDK_INT >= 17){
DB_PATH = context.getApplicationInfo().dataDir + "/databases/";
}
else
{
DB_PATH = "/data/data/" + context.getPackageName() + "/databases/";
}
}
答案 1 :(得分:0)
我发现了自己的错误。代码正常,我的错误是包名。我做了复制粘贴,这就是为什么我得到错误的包。现在它正在发挥作用。