我一直在寻找很长一段时间,但我找不到答案。我想在我的应用程序中复制我现有的Sqlite数据库test.db及其中的数据。因此,当用户从应用程序下载我的应用程序时,数据库随之随附。
现在我看到很多对教程this和this的引用。但它们都不适合我。
public class DatabaseAdapter extends SQLiteOpenHelper {
private static String dbPath= "data/data/test.test.test/databases/";
private static String dbName = "test";
private SQLiteDatabase applicationDatabase;
private final Context applicationContext;
public DatabaseAdapter(Context context) {
super(context, dbName , null, 3);
this. applicationContext = context;
}
public boolean checkDataBase(){
File dbFile = new File( dbPath + dbName);
return dbFile.exists();
}
public void copyDataBase() throws IOException{
try {
InputStream input = applicationContext .getAssets().open(dbName);
String outPutFileName= dbPath + dbName ;
OutputStream output = new FileOutputStream( outPutFileName);
byte[] buffer = new byte[1024];
int length;
while ((length = input.read(buffer))>0){
output.write(buffer, 0, length);
}
output.flush();
output.close();
input.close();
}
catch (IOException e) {
Log.v("error",e.toString());
}
}
public void openDataBase() throws SQLException{
String fullDbPath= dbPath + dbName;
applicationDatabase = SQLiteDatabase.openDatabase( fullDbPath, null,SQLiteDatabase.OPEN_READONLY);
}
@Override
public synchronized void close() {
if( applicationDatabase != null)
applicationDatabase .close();
super.close();
}
@Override
public void onCreate(SQLiteDatabase arg0) {
// TODO Auto-generated method stub
}
@Override
public void onUpgrade(SQLiteDatabase arg0, int arg1, int arg2) {
// TODO Auto-generated method stub
}
public void getit(){
this.getReadableDatabase().rawQuery("SELECT * FROM test", null);
}
}
我得到错误代码= 1没有这样的表。 如果我检查数据库是否已创建,但只能使用表android_metadata。
有人是一个有效的例子吗?
答案 0 :(得分:6)
编辑 - 这是我的代码有效。我添加了您发布的信息,但可能还有其他变量没有,所以您可能需要对其进行一些修改。
public class DatabaseAdapter extends SQLiteOpenHelper {
private Context mycontext;
private String DB_PATH = "data/data/test.test.test/databases/";
private static String DB_NAME = "test";
// the extension may be .sqlite
// or .db
public SQLiteDatabase myDataBase;
public DatabaseAdapter(Context context) {
super(context, DB_NAME, null, 1);
this.mycontext = context;
boolean dbexist = checkdatabase();
if (dbexist) {
} else {
System.out.println("Database doesn't exist");
try {
createdatabase();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public void createdatabase() throws IOException {
boolean dbexist = checkdatabase();
if (dbexist) {
} else {
this.getReadableDatabase();
try {
copydatabase();
} catch (IOException e) {
throw new Error("Error copying database");
}
}
}
private boolean checkdatabase() {
boolean checkdb = false;
try {
String myPath = DB_PATH + DB_NAME;
File dbfile = new File(myPath);
checkdb = dbfile.exists();
} catch (SQLiteException e) {
System.out.println("Database doesn't exist");
}
return checkdb;
}
private void copydatabase() throws IOException {
// Open your local db as the input stream
InputStream myinput = mycontext.getAssets().open(DB_NAME);
// Path to the just created empty db
@SuppressWarnings("unused")
String outfilename = DB_PATH + DB_NAME;
// Open the empty db as the output stream
OutputStream myoutput = new FileOutputStream(
"data/data/test.test.test/databases/test");
// transfer byte to inputfile to 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();
}
public void open() {
// Open the database
String mypath = DB_PATH + DB_NAME;
myDataBase = SQLiteDatabase.openDatabase(mypath, null,
SQLiteDatabase.OPEN_READWRITE);
}
public synchronized void close() {
myDataBase.close();
super.close();
}
@Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
}
}
答案 1 :(得分:0)
每张表中都有“_id”列吗?