我正在创建一个应用程序,在SQLite数据库中缓存它的数据,但我不想以编程方式创建我的数据库我已经使用SQLite浏览器创建了数据库,然后我将其导入我的资产文件夹,但每次我尝试对此数据库执行任何操作时,我都没有得到这样的表异常。
我知道有很多关于这个问题的问题,但是他们没有帮助我
这是我的SQLiteOpenHelper
public class MySQLiteHelper extends SQLiteOpenHelper {
public SQLiteDatabase myDataBase;
private final Context myContext;
private static final String DATABASE_NAME = "VadDB";
public static String DATABASE_PATH = "";
public static final int DATABASE_VERSION = 2;
// public static final int DATABASE_VERSION_old = 1;
// Constructor
public MySQLiteHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
if(android.os.Build.VERSION.SDK_INT >= 17){
DATABASE_PATH = context.getApplicationInfo().dataDir + "/databases/";
}
else
{
DATABASE_PATH = "/data/data/" + context.getPackageName() + "/databases/";
}
this.myContext = context;
}
// Create a empty database on the system
public void createDataBase() throws IOException {
// boolean dbExist = checkDataBase();
//
// if (dbExist) {
// Log.v("DB Exists", "db exists");
// // By calling this method here onUpgrade will be called on a
// // writeable database, but only if the version number has been
// // bumped
// // onUpgrade(myDataBase, DATABASE_VERSION_old, DATABASE_VERSION);
// }
boolean dbExist1 = checkDataBase();
if (!dbExist1) {
this.getReadableDatabase();
try {
this.close();
copyDataBase();
} catch (IOException e) {
throw new Error("Error copying database");
}
}
}
// Check database already exist or not
private boolean checkDataBase() {
boolean checkDB = false;
try {
String myPath = DATABASE_PATH + DATABASE_NAME;
File dbfile = new File(myPath);
checkDB = dbfile.exists();
} catch (SQLiteException e) {
}
return checkDB;
}
// Copies your database from your local assets-folder to the just created
// empty database in the system folder
private void copyDataBase() throws IOException {
String outFileName = DATABASE_PATH + DATABASE_NAME;
OutputStream myOutput = new FileOutputStream(outFileName);
InputStream myInput = myContext.getAssets().open(DATABASE_NAME);
byte[] buffer = new byte[1024];
int length;
while ((length = myInput.read(buffer)) > 0) {
myOutput.write(buffer, 0, length);
}
myInput.close();
myOutput.flush();
myOutput.close();
}
// delete database
public void db_delete() {
File file = new File(DATABASE_PATH + DATABASE_NAME);
if (file.exists()) {
file.delete();
}
}
public Cursor query(String table, String[] columns, String selection,
String[] selectionArgs, String groupBy, String having,
String orderBy) {
return myDataBase.query("data_table", null, null, null, null, null,
null);
}
public Cursor queryDatabase(String sql) {
Cursor cursor = myDataBase.rawQuery(sql, null);
return cursor;
}
// Open database
public void openDataBase() throws SQLException {
String myPath = DATABASE_PATH + DATABASE_NAME;
myDataBase = SQLiteDatabase.openDatabase(myPath, null,
SQLiteDatabase.OPEN_READWRITE);
}
public synchronized void closeDataBase() throws SQLException {
if (myDataBase != null)
myDataBase.close();
super.close();
}
@Override
public void onCreate(SQLiteDatabase db) {
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
if (newVersion > oldVersion) {
db_delete();
}
}
这就是我使用它的方式
MySQLiteHelper helper=new MySQLiteHelper(context);
SQLiteDatabase db1 = helper.getWritableDatabase();
System.out.println(db1.getPath());
for(int i=0;i<faqlist.size();i++){
ContentValues values = new ContentValues();
values.put("ModuleId", faqlist.get(i).getModuleId());
values.put("Title",faqlist.get(i).getTitle() );
values.put("Category", faqlist.get(i).getCategory());
values.put("CetegoryId", faqlist.get(i).getCategoryId());
values.put("Question", faqlist.get(i).getQuestion());
values.put("Answer", faqlist.get(i).getAnswer());
db1.insert("Faq1", null, values);
}
这是我在db browser中的数据库结构
没有成功。
提前谢谢。